MIT6_094IAP10_lec03

# MIT6_094IAP10_lec03 - 6.094 Introduction to programming in...

This preview shows pages 1–12. Sign up to view the full content.

6.094 Introduction to programming in MATLAB Danilo Š ć epanovi ć IAP 2008 Lecture 3 : Solving Equations and Curve Fitting

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Homework 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 •C o m m o n m i s t a k e : » [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
Outline (1) Linear Algebra (2) Polynomials (3) Optimization (4) Differentiation/Integration (5) Differential Equations

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Systems of Linear Equations • Given a system of linear equations ¾ 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. MATLAB makes linear algebra fun!
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 A*x=b with A a square matrix, x=A\b is the same as x=inv(A)*b

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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: ¾ System 2: 43 4 32 xy += −+ = 22 4 3 34 2 −= =

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Exercise: Linear Algebra • Solve the following systems of equations: ¾ System 1: ¾ System 2: 43 4 32 xy += −+ = 22 4 3 34 2 −= = » A=[1 4;-3 1]; » b=[34;2]; » rank(A) » x=inv(A)*b; » A=[2 -2;-1 1;3 4]; » b=[4;3;2]; » rank(A) ¾ rectangular matrix » x1=A\b; ¾ gives least squares solution » error=abs(A*x1-b)
Outline (1) Linear Algebra (2) Polynomials (3) Optimization (4) Differentiation/Integration (5) Differential Equations

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 ax 3 +bx 2 +cx+d • P=[1 0 -2] represents the polynomial x 2 -2 • P=[2 0 0 0] represents the polynomial 2x 3 P(1) P(2) P(3) P(4)
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) ¾

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## MIT6_094IAP10_lec03 - 6.094 Introduction to programming in...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online