inline Link to heading
- Purpose: Define a function in the same
.m
file. - Note: Filename should not be
inline.m
, which could triggered error message. - Example is
func_inline.m
as follow.f = inline('x^2 + 2.34'); fprintf("x\tf(x)\n") for x = 0:4 fprintf('%.0f\t%.2f\n', x, f(x)) end
- Result
>> func_inline x f(x) 0 2.34 1 3.34 2 6.34 3 11.34 4 18.34
fzero Link to heading
- Purpose: Find root of a function with initial guess.
- Example is as follow.
f = inline('x^2 + 6.25'); x = fzero(f, 4); fprintf('Equation \n'); disp(f) fprintf('root = %0.2f', x)
- Result
Equation Inline function: f(x) = x^2 - 6.25 root = 2.50
polynomial Link to heading
- A polynomial of degree is defined as
- Its coefficients can be stored in a vector or array as follow
- Its symbolic representaion can be obtained from its coefficients using
poly2sym
function as follow.whose result is% test_poly2sym % define coefficients of a polynomial a = [1 -1 2 -2 3]; % obtain its symbolic representation ps = poly2sym(a); % display it display(ps);
or that is related to .>> test_poly2sym ps = x^4 - x^3 + 2*x^2 - 2*x + 3
derivative of Link to heading
From Eqn (1) it can be obtained that its derivative with respect to is
If then
with
And the relation of elements between and is
with .
polyder Link to heading
- It derives the coefficients of a polynomial.
- Suppose there is a polynomial or .
- Then its derivative with respect to is or .
- Notice the following lines of code.and the result
% test_polyder % define coefficients of a polynomial a = [1 2 -4 1 -5]; % derive it b = polyder(a); % display result fprintf('p(x) = ['); fprintf('%g, ', a(1:end-1)); fprintf('%g]\n', a(end)); fprintf('q(x) = ['); fprintf('%g, ', b(1:end-1)); fprintf('%g]\n', b(end));
which are previous>> test_polyder p(x) = [1, 2, -4, 1, -5] q(x) = [4, 6, -8, 1]
- , and
- .
wrap it all up Link to heading
- Result
p(x) = x^4 + 2*x^3 - 4*x^2 + x - 5 q(x) = 4*x^3 + 6*x^2 - 8*x + 1
- Codewhere
% display symbolic fprintf('p(x) = ') disp(poly2sym(a)); fprintf('q(x) = ') disp(poly2sym(b));
a
andb
are as previously defined.