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 nn degree is defined as pn(x)=i=0naixi.(1)\tag{1} p_n(x) = \sum_{i = 0}^n a_i x^i.
  • Its coefficients can be stored in a vector or array as follow a=[an  an1    ai    a1  a0].(2)\tag{2} a = [a_n \ \ a_{n-1} \ \ \dots \ \ a_i \ \ \dots \ \ a_1 \ \ a_0 ].
  • Its symbolic representaion can be obtained from its coefficients using poly2sym function as follow.
    % test_poly2sym
    
    % define coefficients of a polynomial
    a = [1 -1 2 -2 3];
    
    % obtain its symbolic representation
    ps = poly2sym(a);
    
    % display it
    display(ps);
    
    whose result is
    >> test_poly2sym
    
    ps =
    
    x^4 - x^3 + 2*x^2 - 2*x + 3
    
    or x4x3+2x22x+3x^4 - x^3 + 2x^2 - 2x + 3 that is related to c=[1  1  2  2  3]c = [1 \ \ -1 \ \ 2 \ \ -2 \ \ 3].

derivative of pn(x)p_n(x) Link to heading

  • From Eqn (1) it can be obtained that its derivative with respect to xx is pn(x)=i=1ni ai xi1.(3)\tag{3} p_n '(x) = \sum_{i = 1}^n i \ a_i \ x^{i-1}.

  • If qm(x)=pn(x)q_m(x) = p '_n(x) then

    qm(x)=i=0mbixi,(4)\tag{4} q_ m(x) = \sum _{i = 0}^m b _i x^i,

    with

    b=[bm  bm1    bi    b1  b0].(5)\tag{5} b = [b_m \ \ b_{m-1} \ \ \dots \ \ b_i \ \ \dots \ \ b_1 \ \ b_0 ].

  • And the relation of elements between bb and aa is

    bi=(i+1)ai+1,    i=1, 2, , m,(6)\tag{6} b_i = (i+1) a_{i+1}, \ \ \ \ i = 1, \ 2, \ \dots, \ m,

    with m=n1m = n - 1.

polyder Link to heading

  • It derives the coefficients of a polynomial.
  • Suppose there is a polynomial p4(x)=x4+2x34x2+x5 p_4(x) = x^4 + 2 x^3 - 4 x^2 + x - 5 or a=[1  2  4  1  5]a = [1 \ \ 2 \ \ -4 \ \ 1 \ \ -5].
  • Then its derivative with respect to xx is q3(x)=4x3+6x28x+1 q_3(x) = 4 x^3 + 6 x^2 - 8 x + 1 or b=[4  6  8  1]b = [4 \ \ 6 \ \ -8 \ \ 1].
  • Notice the following lines of code.
    % 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));
    
    and the result
    >> test_polyder
    p(x) = [1, 2, -4, 1, -5]
    q(x) = [4, 6, -8, 1]
    
    which are previous
    • p4(x)=x4+2x34x2+x5p_4(x) = x^4 + 2 x^3 - 4 x^2 + x - 5, and
    • q3(x)=4x3+6x28x+1q_3(x) = 4 x^3 + 6 x^2 - 8 x + 1.

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
    
  • Code
    % display symbolic
    fprintf('p(x) = ')
    disp(poly2sym(a));
    fprintf('q(x) = ')
    disp(poly2sym(b));
    
    where a and b are as previously defined.