Curve fitting methods ppt

curve fitting numerical methods examples and curve fitting least square method ppt
Dr.MattWood Profile Pic
Dr.MattWood,United States,Teacher
Published Date:25-07-2017
Your Website URL(Optional)
Comment
6.094 Introduction to programming in MATLAB Lecture 3 : Solving Equations and Curve Fitting Danilo Šćepanović IAP 2008Homework 2 Recap • How long did it take? • Using min with matrices: » a=3 7 5;1 9 10; 30 -1 2; » b=min(a); % returns the min of each column » m=min(b); % returns min of entire a matrix » m=min(min(a)); % same as above » m=min(a(:)); % makes a a vector, then gets min •Common mistake: » m,n=find(min(a)); % think about what happens • How to make and run a function: save the file, then call it from the command window like any other function. No need to 'compile' or make it official in any other way¾¾¾¾ Systems of Linear Equations MATLAB makes linear • Given a system of linear equations algebra fun x+2y-3z=5 -3x-y+z=-8 x-y+z=0 • Construct matrices so the system is described by Ax=b » A=1 2 -3;-3 -1 1;1 -1 1; » b=5;-8;0; • And solve with a single line of code » x=A\b; x is a 3x1 vector containing the values of x, y, and z • The \ will work with square or rectangular systems. • Gives least squares solution for rectangular systems. Solution depends on whether the system is over or underdetermined.¾¾¾¾ More Linear Algebra • Given a matrix » mat=1 2 -3;-3 -1 1;1 -1 1; • Calculate the rank of a matrix » r=rank(mat); the number of linearly independent rows or columns • Calculate the determinant » d=det(mat); mat must be square if determinant is nonzero, matrix is invertible • Get the matrix inverse » E=inv(mat); if an equation is of the form Ax=b with A a square matrix, x=A\b is the same as x=inv(A)b¾¾¾ Matrix Decompositions • MATLAB has built-in matrix decomposition methods • The most common ones are » V,D=eig(X) Eigenvalue decomposition » U,S,V=svd(X) Singular value decomposition » Q,R=qr(X) QR decomposition¾¾ Exercise: Linear Algebra • Solve the following systems of equations: System 1: xy +=434 −+32 xy= System 2: 22 xy −=4 −+xy=3 34 xy +=2¾¾ ¾¾ Exercise: Linear Algebra • Solve the following systems of equations: System 1: » A=1 4;-3 1; xy +=434 » b=34;2; » rank(A) −+32 xy= » x=inv(A)b; System 2: » A=2 -2;-1 1;3 4; » b=4;3;2; 22 xy −=4 » rank(A) −+xy=3 rectangular matrix 34 xy +=2 » x1=A\b; gives least squares solution » error=abs(Ax1-b)¾ Polynomials • Many functions can be well described by a high-order polynomial • MATLAB represents a polynomials by a vector of coefficients if vector P describes a polynomial 3 2 ax +bx +cx+d P(1) P(2) P(3) P(4) 2 • P=1 0 -2 represents the polynomial x -2 3 • P=2 0 0 0 represents the polynomial 2x¾¾¾¾ Polynomial Operations • P is a vector of length N+1 describing an N-th order polynomial • To get the roots of a polynomial » r=roots(P) r is a vector of length N • Can also get the polynomial from the roots » P=poly(r) r is a vector length N • To evaluate a polynomial at a point » y0=polyval(P,x0) x0 is a single value; y0 is a single value • To evaluate a polynomial at many points » y=polyval(P,x) x is a vector; y is a vector of the same size¾¾ Polynomial Fitting • MATLAB makes it very easy to fit polynomials to data • Given data vectors X=-1 0 2 and Y=0 -1 3 » p2=polyfit(X,Y,2); finds the best second order polynomial that fits the points (-1,0),(0,-1), and (2,3) see help polyfit for more information » plot(X,Y,’o’, ‘MarkerSize’, 10); » hold on; » x = -3:.01:3; » plot(x,polyval(p2,x), ‘r’);Exercise: Polynomial Fitting 2 yx = • Evaluate for x=-4:0.1:4. • Add random noise to these samples. Use randn. Plot the noisy signal with . markers nd • Fit a 2 degree polynomial to the noisy data • Plot the fitted polynomial on the same plot, using the same x values and a red lineExercise: Polynomial Fitting 2 yx = • Evaluate for x=-4:0.1:4. » x=-4:0.1:4; » y=x.2; • Add random noise to these samples. Use randn. Plot the noisy signal with . markers » y=y+randn(size(y)); » plot(x,y,’.’); nd • Fit a 2 degree polynomial to the noisy data » p=polyfit(x,y,2); • Plot the fitted polynomial on the same plot, using the same x values and a red line » hold on; » plot(x,polyval(p,x),’r’)Outline (1) Linear Algebra (2) Polynomials (3) Optimization (4) Differentiation/Integration (5) Differential Equations¾ Nonlinear Root Finding • Many real-world problems require us to solve f(x)=0 •Can use fzero to calculate roots for any arbitrary function • fzero needs a function passed to it. • We will see this more and more as we delve into solving equations. • Make a separate function file » x=fzero('myfun',1) » x=fzero(myfun,1) 1 specifies a point close to where you think the root is Courtesy of The MathWorks, Inc. Used with permission.¾¾¾ Minimizing a Function • fminbnd: minimizing a function over a bounded interval » x=fminbnd('myfun',-1,2); myfun takes a scalar input and returns a scalar output myfun(x) will be the minimum of myfun for -1≤x ≤ 2 • fminsearch: unconstrained interval » x=fminsearch('myfun',.5) finds the local minimum of myfun starting at x=0.5¾ Anonymous Functions • You do not have to make a separate function file » x=fzero(myfun,1) What if myfun is really simple? • Instead, you can make an anonymous function » x=fzero((x)(cos(exp(x))+x2-1), 1 ); input function to evaluate » x=fminbnd((x) (cos(exp(x))+x2-1),-1,2);¾¾¾ Optimization Toolbox • If you are familiar with optimization methods, use the optimization toolbox • Useful for larger, more structured optimization problems • Sample functions (see help for more info) » linprog linear programming using interior point methods » quadprog quadratic programming solver » fmincon constrained nonlinear optimizationExercise: Min-Finding − x fxx =cos 4 sin 10xe () ( ) ( ) • Find the minimum of the function over the range –π to π. Use fminbnd. • Plot the function on this range to check that this is the minimum.Exercise: Min-Finding − x fxx =cos 4 sin 10xe () ( ) ( ) • Find the minimum of the function over the range –π to π. Use fminbnd. • Plot the function on this range to check that this is the minimum. • Make the following function: » function y=myFun(x) » y=cos(4x).sin(10x).exp(-abs(x)); • Find the minimum in the command window: » x0=fminbnd('myFun',-pi,pi); • Plot to check if it's right » figure; x=-pi:.01:pi; plot(x,myFun(x));Outline (1) Linear Algebra (2) Polynomials (3) Optimization (4) Differentiation/Integration (5) Differential Equations

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.