The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 4 : Polynomial Edward Cheung Room W311g 2008.

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

Lecture 5.
1 Eng. Mohamed El-Taher Eng. Ahmed Ibrahim. 2 1.FUNCTION SUMMARY polyfun  Polynomial functions are located in the MATLAB polyfun directory. For a complete.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 ~ Curve Fitting ~ Least Squares Regression Chapter.
Polynomials and Polynomial Functions
Matrix Algebra Matrix algebra is a means of expressing large numbers of calculations made upon ordered sets of numbers. Often referred to as Linear Algebra.
Introduction to Matlab:
CITS2401 Computer Analysis & Visualisation
ES 240: Scientific and Engineering Computation. InterpolationPolynomial  Definition –a function f(x) that can be written as a finite series of power functions.
Variance and covariance M contains the mean Sums of squares General additive models.
Chapter 4 Roots of Equations
Matlab Matlab is a powerful mathematical tool and this tutorial is intended to be an introduction to some of the functions that you might find useful.
Review of Matrix Algebra
Computer Graphics Recitation The plan today Least squares approach  General / Polynomial fitting  Linear systems of equations  Local polynomial.
Absolute Value: A number’s distance from zero on a number line. A number’s absolute value is nonnegative.
Vectors and Matrices Class 17.1 E: Ch. 5.
Matrix Approach to Simple Linear Regression KNNL – Chapter 5.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
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.
Matlab intro The Environment
Algebra Review. Polynomial Manipulation Combine like terms, multiply, FOIL, factor, etc.
Unit 3: Lesson 1: 3.1 Polynomial Basics (4-1)
 1999 BG Mobasseri1 8/29/2015 MATLAB GRAPHICS - PART II ADVANCED PLOTTING June 4, ‘99.
Computational Methods in Physics PHYS 3437 Dr Rob Thacker Dept of Astronomy & Physics (MM-301C)
Eigenvectors and Eigenvalues
ENGR-25_Arrays-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Chemistry 330 The Mathematics Behind Quantum Mechanics.
Introduction to MATLAB for Engineers, Third Edition Chapter 6 Model Building and Regression PowerPoint to accompany Copyright © The McGraw-Hill Companies,
MECN 3500 Inter - Bayamon Lecture 9 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt.
4-5, 4-6 Factor and Remainder Theorems r is an x intercept of the graph of the function If r is a real number that is a zero of a function then x = r.
Advanced Topics- Polynomials
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5B Model.
ECE 4115 Control Systems Lab 1 Spring 2005
Solving DEs etc. Laplace transforming derivatives Etc.
Curve-Fitting Regression
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
Regression Regression relationship = trend + scatter
Lecture 10 2D plotting & curve fitting Subplots Other 2-D Plots Other 2-D Plots Curve fitting © 2007 Daniel Valentine. All rights reserved. Published by.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers Chapter 5B Model.
LINEAR ALGEBRA Matrix analysis Linear equations Eigenvalues and singular values Matrix functions.
Scientific Computing General Least Squares. Polynomial Least Squares Polynomial Least Squares: We assume that the class of functions is the class of all.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Introduction to MATLAB Session 5 Simopekka Vänskä, THL 2010.
2D Routines in 3D. Outline Announcements –HW II--due Friday. 5PM HW1 & Cookie Grids & Meshes Representing f(x,y) Lines & Surfaces in 3D Survey.
6.094 Introduction to programming in MATLAB Danilo Šćepanović IAP 2008 Lecture 3 : Solving Equations and Curve Fitting.
Numerical Analysis 3D Plots. A numerical method is a technique for computing a numerical approximation of the solution to a mathematical problem.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Chapter 1 Polynomial and Rational Functions Copyright © 2014, 2010, 2007 Pearson Education, Inc Dividing Polynomials; Remainder and Factor Theorems.
Polynomials, Curve Fitting and Interpolation. In this chapter will study Polynomials – functions of a special form that arise often in science and engineering.
11/6/ :55 Graphics II Introduction to Parametric Curves and Surfaces Session 2.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 4 Chapter 15 General Least Squares and Non- Linear.
Algebra and Calculus 2-1 Copyright © Genetic Computer School 2007 Lesson 2 Graphs and Estimation (Part I)
EEE 242 Computer Tools for Electrical Engineering
Session III Plotting in MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU MATLAB Tutorials.
Introduction to Vectors and Matrices
EEE 244-3: MATRICES AND EQUATION SOLVING
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
12-1 Organizing Data Using Matrices
Polynomials Functions
CHE 391 T. F. Edgar Spring 2012.
Matrices Definition: A matrix is a rectangular array of numbers or symbolic elements In many applications, the rows of a matrix will represent individuals.
Vectorized Code, Logical Indexing
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
Linear regression Fitting a straight line to observations.
Eigenvalues and Eigenvectors
Introduction to Vectors and Matrices
EEE 244-3: MATRICES AND EQUATION SOLVING
Chapter 2 Determinants.
Multiple linear regression
Presentation transcript:

The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 4 : Polynomial Edward Cheung Room W311g 2008

2 Polynomial A polynomial is a function and can be written as follows:- Where f(x) is a function of x The degree or order of a polynomial is n, the highest power of x. a i = 1, 2,…,n, n+1 are coefficients of the polynomial

3 Operations On Polynomials A polynomial can be described with a row vector in MatLab The coefficients become the elements of the vector starting from the highest power of x Interested operations are:-  Get roots from polynomial  Get polynomial from root  Perform arithmetic operations  Addition, subtraction, multiplication, division  Visualisation

4 Root Finding Functions >>help polyfun  Functions for Interpolation and polynomials. roots() – roots of polynomial function fzero() – find root for continous function of single variable  Usage :- x = fzero(fun,x0,options)  fzero looks for a point where fun change sign and defines a zero as a point where the function crosses the x-axis. and not applicable for zeros of even multiplicity

5 Example  Calculate zero of cosine between 0 & -  >> x = % -pi/2 >> fzero('(x-1)^2',0) % root of even multiplicity search may fail % returns NaN = not a number % a value or symbol produced as a result of invalid input operands. E.g. 0/0, inf/inf

6 Polynomial Roots Consider the following cubic equation >>p=[ ]; % i/p coefficient row vector r=roots(p) % find roots for p(x) r = % o/p root as a column vector % r are the values of x that make p(x)=0

7 Find Imaginary Roots >> r2=roots([ ]) r2 = i i i i Solve:-

8 Coefficient of Polynomial from Roots p=Poly(r)  Return a row vector p such that the elements of p are the coefficients of a polynomial with roots given by r >> r=[ ]; p=poly(r) p =

9 Characteristic Polynomial For any square matrix A, we can write:-  is called the eigenvalue of A (eigenvert in German means own value or characteristic value)  x is called the eigenvector (x≠0) p( ) Is called the characteristic polynomial of A Eigenvalues of matrix A can be computed by solving the equation

10 Example on Eigenvalues Suppose we want to find the eigenvalues of a matrix X >> X=[8 -4;2 2]; px=poly(X) %Find characteristic polynomial of X px = >> roots(px) % solve for px give eigenvalues of X ans = >> eig(X) % use eigenvalue function on X ans = % same result as above 6 4

11 Polynomial Evaluation >> p=[ ]; polyval(p,3) ans = 33 >> x=-5:0.1:5; y=polyval(p,x); plot(x,y) grid Example:- Plot & evaluate the following polynomial at x=3.

12 Polynomial Arithmetic Addition & subtraction of polynomial follows the addition and subtraction of vectors  You should check the order of matrix and patch zero on missing terms to avoid error caused by unequal length Multiplication of polynomial by a scalar is just scalar multiplication Multiplication of two polynomials can be done by the convolve function; conv() Division of two polynomials can be done by the deconvolve function; deconv()

13 Convolution & Deconvolution >> u=[1 2 3]; % u(s) defined v=[4 5 6]; % v(s) defined w=conv(u,v) % multiple u(s) & v(s) w = >> [q,r]=deconv(w,v) % q=quotient, r=remainder q = r = Consider the following polynomials:-

14 Partial Fraction Expansion [r,p,k] = residue(b,a) finds the residues, poles, and direct term of a partial fraction expansion of the ratio of two polynomials, b(s) and a(s), of the form: [b,a] = residue(r,p,k) converts the partial fraction expansion back to the polynomials with coefficients in b and a. If there are no multiple roots, then: If p j =…=p (j+m-1) is a pole of multiplicity m, then the expansion include terms of the form

15 Example on Partial Fraction Expansion >> b=[ ]; >> a=[ ]; >> [r,p,k]=residue(b,a) r = p = k = [] The direct term coefficient vector k is empty because length(a) > length(b)

16 Example on Partial Fraction Expansion (Cont.) Given r,p & k, convert the partial fraction expression to factional polynomial. >> [b2,a2]=residue(r,p,k) b2 = a2 =

17 Data Analysis & Curve Fitting Curve fitting is a technique to model data with an equation Better than interpolation In curve fitting, one need to find a smooth curve to best fit the data. Best fit is interpreted as minimizing the sums of squared errors at the data points. (least squares method) >> eqn=polyfit(X,Y,N)  Fits a polynomial of degree N to data described by the vector X & Y, where X is the independent variable. Returns a row vector of length N+1 as the coefficients in the order of descending power

18 Example on Linear Regression >> x=0.1:0.1:10; % create data x y=5+1.5*x; % create data y y1=y+randn(1,100); % perturbation y1 plot(x,y1,'*')

19 Example on Linear Regression (cont.) eq=polyfit(x,y1,1) % eq = >> y2=polyval(eq,x); plot(x,y1,'x',x,y2,'m') legend('data','fit') The data can be Described by the polynomial

20 Example on Non-linear Regression Fit non-linear data with higher order polynomial >> x=0:0.2:1.4; y=cos(x); plot(x,y,'x')

21 Example on Non-linear Regression (cont.) >> eqn1=polyfit(x,y,2) % eqn1 = % >> x1=0:0.01:1.4; >> y1=polyval(eqn1,x1); >> hold on >> line1=plot(x1,y1,'g');

22 Example on Non-linear Regression (cont.) >> delete(line1) >> eqn2=polyfit(x,y,3) % eqn2 = % >> y2=polyval(eqn2,x1); >> line2=plot(x1,y2,'g');

23 3-D Line Plots >> t=-3*pi:0.1:3*pi; >> plot3(t,sin(t),cos(t)) >> grid on

24 3-D Line Plots (cont.) Different Views >> view([1,0,0]) >> view([0,1,0]) >> view([0,0,1])

25 Surface Mesh Plots If z=f(x,y) represents a surface on xyz axes First use [X,Y] = meshgrid(x,y) to generate a grid of points in the xy plane and then evaluate the function z If x=[xmin:spacing:xmax] & y=[ymin:spacing:ymax], meshgrid will generate the coordinates of a rectangular grid with one corner at (xmin, ymin) and the opposite corner at (xmax, ymax). [X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x)  Usage>> [X,Y]=meshgrid(min:spacing:max); Calculate z & use mesh(X,Y,Z) to plot the surface Not desirable to use too small spacing because of difficulty for visualize and X,Y matrices can become too large

26 Example – meshgrid generation >> [X,Y]=meshgrid(-2:2) X = Y = plot(X,Y,'rx'),axis([ ])

27 Mesh Plot Example >>Z=sin(sqrt(X.^2+Y.^2)+eps)./(sqrt(X.^2+Y.^2)+eps ); mesh(X,Y,Z)

28 Mesh Plot Examples >> [X,Y]=meshgrid(-2:0.2:2); Z=sin(sqrt(X.^2+Y.^2) )./(sqrt(X.^2+Y.^2) ); mesh(X,Y,Z) >> [X,Y]=meshgrid(-2:0.1:2); >> Z=X.*exp(-((X-Y.^2).^2+Y.^2)); >> mesh(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z')

29 Contour, Mesh & Surface Plots >> [X,Y]=meshgrid(-2:0.2:2); Z=sin(X).*sin(Y).*exp(-X.^2-Y.^2); contour(X,Y,Z,10)

30 Other 3-D Plots contour(x,y,z) – creates a contour plot mesh(x,y,z) – creates a 3D mesh surface plot meshc(x,y,z) – same as mesh but draws a contour plot under the surface meshz(x,y,z) – same as mesh but draws a series of vertical reference lines under the surface surf(x,y,z) – creates a shaded 3D mesh surface plot surfc(x,y,z) –same as surf but draws a contour plot under the surface waterfall(x,y,z) – same as mesh but draw mesh lines in one direction only

31 Waterfall Plot Example