Numerical Operations S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Polynomial Manipulations
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 2 Polynomial Topics n Entering Polynomials Entering Polynomials n Roots Roots n Multiplication Multiplication n Evaluation Evaluation n Curve Fitting Curve Fitting
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 3 Entering Polynomials n In Matlab, a polynomial is represented by a row vector of it’s coefficients in descending order n Is entered as: » p=[ ] n Zero term coefficients MUST be included n For example, the polynomial
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 4 Roots To find the roots of a polynomial use roots n Roots are always column vectors » p=[ ]; » r=roots(p) r = i i
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 5 Mutliplication Use the conv (convolution) command to evaluate the product of two polynomials: » a=[ ];b=[ ]; » c=conv(a,b) c =
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 6 Evaluation polyval is used to evaluate the polynomial p(x) between -1 and 3 » x=linspace(-1,3);% 100 data points » p=[ ];% Coefficients » v=polyval(p,x);% Evaluate » plot(x,v); » title('x^3 +4x^2 -7x -10'); » xlabel('x');
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 7 Evaluation Plot
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 8 Curve Fitting polyfit implements a least squares curve fitting algorithm » x=[0:0.1:1]; » y=[ ]; » n=2; % Order of Fit » p=polyfit(x,y,n)
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 9 Polyfit Polyfit returns coefficients of curve fit solution » p = polyfit(x,y,n) p =
Numerical Operations: U of M-Dearborn ECE Department Introduction to MATLAB and its Toolboxes Polynomials 10 Compare Solutions » xi=linspace(0,1,100); » z=polyval(p,xi); » plot(x,y,'-o',xi,z,':'); » xlabel('x'); » ylabel('y=f(x)'); » title('Second Order Curve Fitting'); n Least Mean Squared (LMS) Error Minimization