SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
6.094

Introduction to programming in MATLAB

Lecture 3 : Solving Equations and Curve Fitting

Danilo Šćepanović
IAP 2008
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
• 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
Outline
(1)
(2)
(3)
(4)
(5)

Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
Systems of Linear Equations
• Given a system of linear equations
x+2y-3z=5
-3x-y+z=-8
x-y+z=0

MATLAB makes linear
algebra fun!

• 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=Ab;
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 A*x=b with A a square matrix,
x=Ab 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:

x + 4 y = 34
−3 x + y = 2

System 2:

2x − 2 y = 4
−x + y = 3
3x + 4 y = 2
Exercise: Linear Algebra
• Solve the following systems of equations:
System 1:

x + 4 y = 34
−3 x + y = 2

System 2:

2x − 2 y = 4
−x + y = 3
3x + 4 y = 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=Ab;
gives least squares solution

» error=abs(A*x1-b)
Outline
(1)
(2)
(3)
(4)
(5)

Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
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

ax3+bx2+cx+d
P(1)

P(2)

P(3)

P(4)

• P=[1 0 -2] represents the polynomial x2-2
• P=[2 0 0 0] represents the polynomial 2x3
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
• Evaluate

y = x2

for x=-4:0.1:4.

• Add random noise to these samples. Use randn. Plot the
noisy signal with . markers

• Fit a 2nd degree polynomial to the noisy data
• Plot the fitted polynomial on the same plot, using the same
x values and a red line
Exercise: Polynomial Fitting
2

• Evaluate y = x 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,’.’);
• Fit a 2nd 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)
(2)
(3)
(4)
(5)

Linear Algebra
Polynomials
Optimization
Differentiation/Integration
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))+x^2-1), 1 );
input

function to evaluate

» x=fminbnd(@(x) (cos(exp(x))+x^2-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 optimization
Exercise: Min-Finding
• Find the minimum of the function f ( x ) = cos ( 4 x ) sin (10 x ) e
over the range –π to π. Use fminbnd.
• Plot the function on this range to check that this is the
minimum.

−x
Exercise: Min-Finding
• Find the minimum of the function f ( x ) = cos ( 4 x ) sin (10 x ) e
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(4*x).*sin(10*x).*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));

−x
Outline
(1)
(2)
(3)
(4)
(5)

Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
Numerical Differentiation
1
0.8
• MATLAB can 'differentiate' numerically
0.6
» x=0:0.01:2*pi;
0.4
» y=sin(x);
0.2
» dydx=diff(y)./diff(x);
0

diff computes the first difference

-0.2
-0.4

• Can also operate on matrices
» mat=[1 3 5;4 8 6];
» dm=diff(mat,1,2)

-0.6
-0.8
-1

0

100

200

300

400

500

600

first difference of mat along the 2nd dimension, dm=[2 2;4 -2]
see help for more details
The opposite of diff is the cumulative sum cumsum

• 2D gradient
» [dx,dy]=gradient(mat);

700
Numerical Integration
• MATLAB contains common integration methods
• Adaptive Simpson's quadrature (input is a function)
» q=quad('myFun',0,10);
q is the integral of the function myFun from 0 to 10

» q2=quad(@(x) sin(x)*x,0,pi)
q2 is the integral of sin(x)*x from 0 to pi

• Trapezoidal rule (input is a vector)
» x=0:0.01:pi;
» z=trapz(x,sin(x));
z is the integral of sin(x) from 0 to pi

» z2=trapz(x,sqrt(exp(x))./x)
x
z2 is the integral of e x from 0 to pi
Outline
(1)
(2)
(3)
(4)
(5)

Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
ODE Solvers: Method
• Given a differential equation, the solution can be found by
integration:

Evaluate the derivative at a point and approximate by straight line
Errors accumulate!
Variable timestep can decrease the number of iterations
ODE Solvers: MATLAB
• MATLAB contains implementations of common ODE solvers
• Using the correct ODE solver can save you lots of time and
give more accurate results
» ode23
Low-order solver. Use when integrating over small intervals
or when accuracy is less important than speed

» ode45
High order (Runge-Kutta) solver. High accuracy and
reasonable speed. Most commonly used.

» ode15s
Stiff ODE solver (Gear's algorithm), use when the diff eq's
have time constants that vary by orders of magnitude
ODE Solvers: Standard Syntax
• To use standard options and variable time step
» [t,y]=ode45('myODE',[0,10],[1;0])
ODE integrator:
23, 45, 15s
ODE function

Initial conditions
Time range

• Inputs:
ODE function name (or anonymous function). This function
takes inputs (t,y), and returns dy/dt
Time interval: 2-element vector specifying initial and final
time
Initial conditions: column vector with an initial condition for
each ODE. This is the first input to the ODE function

• Outputs:
t contains the time points
y contains the corresponding values of the integrated
variables.
ODE Function
• The ODE function must return the value of the derivative at
a given time and function value
• Example: chemical reaction

10

Two equations

dA
= −10 A + 50 B
dt
dB
= 10 A − 50 B
dt

A

B
50

ODE file:

– y has [A;B]
– dydt has
[dA/dt;dB/dt]

Courtesy of The MathWorks, Inc. Used with permission.
ODE Function: viewing results
• To solve and plot the ODEs on the previous slide:
» [t,y]=ode45('chem',[0 0.5],[0 1]);
assumes that only chemical B exists initially

»
»
»
»
»
»
»

plot(t,y(:,1),'k','LineWidth',1.5);
hold on;
plot(t,y(:,2),'r','LineWidth',1.5);
legend('A','B');
xlabel('Time (s)');
ylabel('Amount of chemical (g)');
title('Chem reaction');
ODE Function: viewing results
• The code on the previous slide produces this figure
Chem reaction
1
A
B

0.9

Amount of chemical (g)

0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0

0.05

0.1

0.15

0.2

0.25
0.3
Time (s)

0.35

0.4

0.45

0.5
Higher Order Equations
• Must make into a system of first-order equations to use
ODE solvers
• Nonlinear is OK!
• Pendulum example:
&&
θ+

g
sin (θ ) = 0
L
&&
θ =−

g
sin (θ )
L

&
let θ = γ

γ& = −

g
sin (θ )
L

v ⎡θ ⎤
x=⎢ ⎥
⎣γ ⎦
v
&
dx ⎡θ ⎤
=⎢ ⎥
dt ⎣γ& ⎦
Courtesy of The MathWorks, Inc. Used with permission.
Plotting the Output
• We can solve for the position and velocity of the pendulum:
» [t,x]=ode45('pendulum',[0 10],[0.9*pi 0]);
assume pendulum is almost horizontal

»
»
»
»

plot(t,x(:,1));
hold on;
plot(t,x(:,2),'r');
legend('Position','Velocity');
8
Position
Velocity

6

Position in terms of
angle (rad)

Velocity (m/s)

4
2
0
-2
-4
-6
-8

0

1

2

3

4

5

6

7

8

9

10
Plotting the Output
• Or
»
»
»

we can plot in the phase plane:
plot(x(:,1),x(:,2));
xlabel('Position');
yLabel('Velocity');

• The phase plane is just a plot of one variable versus the
other:
8
6

Velocity is greatest
when theta=0

4

Velocity

2
0
-2

Velocity=0 when
theta is the greatest

-4
-6
-8
-3

-2

-1

0
Position

1

2

3
ODE Solvers: Custom Options
• MATLAB's ODE solvers use a variable timestep
• Sometimes a fixed timestep is desirable
» [t,y]=ode45('chem',[0:0.001:0.5],[0 1]);
Specify the timestep by giving a vector of times
The function value will be returned at the specified points
Fixed timestep is usually slower because function values
are interpolated to give values at the desired timepoints

• You can customize the error tolerances using odeset
» options=odeset('RelTol',1e-6,'AbsTol',1e-10);
» [t,y]=ode45('chem',[0 0.5],[0 1],options);
This guarantees that the error at each step is less than
RelTol times the value at that step, and less than AbsTol
Decreasing error tolerance can considerably slow the solver
See doc odeset for a list of options you can customize
Exercise: ODE
• Use ode45 to solve for y ( t ) on the range t=[0 10], with
initial condition y ( 0 ) = 10 and dy dt = −t y 10
• Plot the result.
Exercise: ODE
• Use ode45 to solve for y ( t ) on the range t=[0 10], with
initial condition y ( 0 ) = 10 and dy dt = −t y 10
• Plot the result.
• Make the following function
» function dydt=odefun(t,y)
» dydt=-t*y/10;
• Integrate the ODE function and plot the result
» [t,y]=ode45(‘odefun’,[0 10],10);
• Alternatively, use an anonymous function
» [t,y]=ode45(@(t,y) –t*y/10,[0 10],10);
• Plot the result
» plot(t,y);xlabel('Time');ylabel('y(t)');
Exercise: ODE
• The integrated function looks like this:
Function y(t), integrated by ode45

10

9
8

7

y(t)

6

5
4

3
2

1

0

0

1

2

3

4

5
Time

6

7

8

9

10
End of Lecture 3
(1)
(2)
(3)
(4)
(5)

Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
We're almost done!
MIT OpenCourseWare
http://ocw.mit.edu

6.094 Introduction to MATLAB®
January (IAP) 2010

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

Weitere ähnliche Inhalte

Was ist angesagt?

16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks QueuesIntro C# Book
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonAndrew Ferlitsch
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexityIntro C# Book
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scalaTongfei Chen
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersKimikazu Kato
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowAndrew Ferlitsch
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskellfaradjpour
 
digital signal-processing-lab-manual
digital signal-processing-lab-manualdigital signal-processing-lab-manual
digital signal-processing-lab-manualAhmed Alshomi
 
Accelerating Random Forests in Scikit-Learn
Accelerating Random Forests in Scikit-LearnAccelerating Random Forests in Scikit-Learn
Accelerating Random Forests in Scikit-LearnGilles Louppe
 
Tensor board
Tensor boardTensor board
Tensor boardSung Kim
 
Natural Language Processing (NLP)
Natural Language Processing (NLP)Natural Language Processing (NLP)
Natural Language Processing (NLP)Hichem Felouat
 
Tensor flow (1)
Tensor flow (1)Tensor flow (1)
Tensor flow (1)景逸 王
 
Gráficas en python
Gráficas en python Gráficas en python
Gráficas en python Jhon Valle
 
TensorFlow for IITians
TensorFlow for IITiansTensorFlow for IITians
TensorFlow for IITiansAshish Bansal
 
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Universitat Politècnica de Catalunya
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Alamgir Hossain
 

Was ist angesagt? (20)

Dsp manual
Dsp manualDsp manual
Dsp manual
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Whiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in PythonWhiteboarding Coding Challenges in Python
Whiteboarding Coding Challenges in Python
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scala
 
Dsp lab manual
Dsp lab manualDsp lab manual
Dsp lab manual
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to Tensorflow
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
digital signal-processing-lab-manual
digital signal-processing-lab-manualdigital signal-processing-lab-manual
digital signal-processing-lab-manual
 
Accelerating Random Forests in Scikit-Learn
Accelerating Random Forests in Scikit-LearnAccelerating Random Forests in Scikit-Learn
Accelerating Random Forests in Scikit-Learn
 
Numpy Talk at SIAM
Numpy Talk at SIAMNumpy Talk at SIAM
Numpy Talk at SIAM
 
Tensor board
Tensor boardTensor board
Tensor board
 
Natural Language Processing (NLP)
Natural Language Processing (NLP)Natural Language Processing (NLP)
Natural Language Processing (NLP)
 
Tensor flow (1)
Tensor flow (1)Tensor flow (1)
Tensor flow (1)
 
DSP Mat Lab
DSP Mat LabDSP Mat Lab
DSP Mat Lab
 
Gráficas en python
Gráficas en python Gráficas en python
Gráficas en python
 
TensorFlow for IITians
TensorFlow for IITiansTensorFlow for IITians
TensorFlow for IITians
 
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
Optimization (DLAI D4L1 2017 UPC Deep Learning for Artificial Intelligence)
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report
 

Andere mochten auch

Bio Chemistry MBBS Part 2 UHS Paper-2016
Bio Chemistry MBBS Part 2 UHS Paper-2016Bio Chemistry MBBS Part 2 UHS Paper-2016
Bio Chemistry MBBS Part 2 UHS Paper-2016Atiqa khan
 
Open Sources for Satellite Imagery 2016
Open Sources for Satellite Imagery 2016Open Sources for Satellite Imagery 2016
Open Sources for Satellite Imagery 2016Atiqa khan
 
Seep Concept_2016
Seep Concept_2016Seep Concept_2016
Seep Concept_2016Atiqa khan
 
Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...
Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...
Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...Atiqa khan
 
Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016
Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016
Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016Atiqa khan
 
Physiology MBBS Part 2 UHS Paper-2016
Physiology MBBS Part 2 UHS Paper-2016Physiology MBBS Part 2 UHS Paper-2016
Physiology MBBS Part 2 UHS Paper-2016Atiqa khan
 
Catalog of Earth Satellite Orbits_2017
Catalog of Earth Satellite Orbits_2017Catalog of Earth Satellite Orbits_2017
Catalog of Earth Satellite Orbits_2017Atiqa khan
 
Information about Admission in NUST Engineering_2016
Information about Admission in NUST Engineering_2016Information about Admission in NUST Engineering_2016
Information about Admission in NUST Engineering_2016Atiqa khan
 
Applied statistics and probability for engineers solution montgomery && runger
Applied statistics and probability for engineers solution   montgomery && rungerApplied statistics and probability for engineers solution   montgomery && runger
Applied statistics and probability for engineers solution montgomery && rungerAnkit Katiyar
 

Andere mochten auch (11)

Bio Chemistry MBBS Part 2 UHS Paper-2016
Bio Chemistry MBBS Part 2 UHS Paper-2016Bio Chemistry MBBS Part 2 UHS Paper-2016
Bio Chemistry MBBS Part 2 UHS Paper-2016
 
Open Sources for Satellite Imagery 2016
Open Sources for Satellite Imagery 2016Open Sources for Satellite Imagery 2016
Open Sources for Satellite Imagery 2016
 
Seep Concept_2016
Seep Concept_2016Seep Concept_2016
Seep Concept_2016
 
Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...
Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...
Instructions for Authors of IJRS (International Journal of Remote Sensing) fo...
 
Laplace transform
Laplace transformLaplace transform
Laplace transform
 
Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016
Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016
Islamic Studies-Ethics-Pak Studies MBBS Part 2 UHS Paper-2016
 
Physiology MBBS Part 2 UHS Paper-2016
Physiology MBBS Part 2 UHS Paper-2016Physiology MBBS Part 2 UHS Paper-2016
Physiology MBBS Part 2 UHS Paper-2016
 
Catalog of Earth Satellite Orbits_2017
Catalog of Earth Satellite Orbits_2017Catalog of Earth Satellite Orbits_2017
Catalog of Earth Satellite Orbits_2017
 
Information about Admission in NUST Engineering_2016
Information about Admission in NUST Engineering_2016Information about Admission in NUST Engineering_2016
Information about Admission in NUST Engineering_2016
 
Applied statistics and probability for engineers solution montgomery && runger
Applied statistics and probability for engineers solution   montgomery && rungerApplied statistics and probability for engineers solution   montgomery && runger
Applied statistics and probability for engineers solution montgomery && runger
 
EE201_Assignment2
EE201_Assignment2EE201_Assignment2
EE201_Assignment2
 

Ähnlich wie Mit6 094 iap10_lec03

Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicosKmilo Bolaños
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxDevaraj Chilakala
 
Lesson3.2 a basicdifferentiationrules
Lesson3.2 a basicdifferentiationrulesLesson3.2 a basicdifferentiationrules
Lesson3.2 a basicdifferentiationrulesNico Reyes
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................nourhandardeer3
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine LearningBig_Data_Ukraine
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptximman gwu
 
Optimization Techniques.pdf
Optimization Techniques.pdfOptimization Techniques.pdf
Optimization Techniques.pdfanandsimple
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...The1 Uploader
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functionsjoellivz
 
AOT2 Single Variable Optimization Algorithms.pdf
AOT2 Single Variable Optimization Algorithms.pdfAOT2 Single Variable Optimization Algorithms.pdf
AOT2 Single Variable Optimization Algorithms.pdfSandipBarik8
 

Ähnlich wie Mit6 094 iap10_lec03 (20)

Lec3
Lec3Lec3
Lec3
 
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Advanced matlab codigos matematicos
Advanced matlab codigos matematicosAdvanced matlab codigos matematicos
Advanced matlab codigos matematicos
 
INTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptxINTRODUCTION TO MATLAB presentation.pptx
INTRODUCTION TO MATLAB presentation.pptx
 
Lesson3.2 a basicdifferentiationrules
Lesson3.2 a basicdifferentiationrulesLesson3.2 a basicdifferentiationrules
Lesson3.2 a basicdifferentiationrules
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
 
Optimization tutorial
Optimization tutorialOptimization tutorial
Optimization tutorial
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Optimization Techniques.pdf
Optimization Techniques.pdfOptimization Techniques.pdf
Optimization Techniques.pdf
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
EPE821_Lecture3.pptx
EPE821_Lecture3.pptxEPE821_Lecture3.pptx
EPE821_Lecture3.pptx
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
MATLABgraphPlotting.pptx
MATLABgraphPlotting.pptxMATLABgraphPlotting.pptx
MATLABgraphPlotting.pptx
 
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
AOT2 Single Variable Optimization Algorithms.pdf
AOT2 Single Variable Optimization Algorithms.pdfAOT2 Single Variable Optimization Algorithms.pdf
AOT2 Single Variable Optimization Algorithms.pdf
 

Kürzlich hochgeladen

ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 

Kürzlich hochgeladen (20)

ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 

Mit6 094 iap10_lec03

  • 1. 6.094 Introduction to programming in MATLAB Lecture 3 : Solving Equations and Curve Fitting Danilo Šćepanović IAP 2008
  • 2. 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 • 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
  • 4. Systems of Linear Equations • Given a system of linear equations x+2y-3z=5 -3x-y+z=-8 x-y+z=0 MATLAB makes linear algebra fun! • 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=Ab; 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.
  • 5. 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=Ab is the same as x=inv(A)*b
  • 6. 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
  • 7. Exercise: Linear Algebra • Solve the following systems of equations: System 1: x + 4 y = 34 −3 x + y = 2 System 2: 2x − 2 y = 4 −x + y = 3 3x + 4 y = 2
  • 8. Exercise: Linear Algebra • Solve the following systems of equations: System 1: x + 4 y = 34 −3 x + y = 2 System 2: 2x − 2 y = 4 −x + y = 3 3x + 4 y = 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=Ab; gives least squares solution » error=abs(A*x1-b)
  • 10. 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 ax3+bx2+cx+d P(1) P(2) P(3) P(4) • P=[1 0 -2] represents the polynomial x2-2 • P=[2 0 0 0] represents the polynomial 2x3
  • 11. 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
  • 12. 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--’);
  • 13. Exercise: Polynomial Fitting • Evaluate y = x2 for x=-4:0.1:4. • Add random noise to these samples. Use randn. Plot the noisy signal with . markers • Fit a 2nd degree polynomial to the noisy data • Plot the fitted polynomial on the same plot, using the same x values and a red line
  • 14. Exercise: Polynomial Fitting 2 • Evaluate y = x 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,’.’); • Fit a 2nd 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’)
  • 16. 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.
  • 17. 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
  • 18. 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))+x^2-1), 1 ); input function to evaluate » x=fminbnd(@(x) (cos(exp(x))+x^2-1),-1,2);
  • 19. 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 optimization
  • 20. Exercise: Min-Finding • Find the minimum of the function f ( x ) = cos ( 4 x ) sin (10 x ) e over the range –π to π. Use fminbnd. • Plot the function on this range to check that this is the minimum. −x
  • 21. Exercise: Min-Finding • Find the minimum of the function f ( x ) = cos ( 4 x ) sin (10 x ) e 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(4*x).*sin(10*x).*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)); −x
  • 23. Numerical Differentiation 1 0.8 • MATLAB can 'differentiate' numerically 0.6 » x=0:0.01:2*pi; 0.4 » y=sin(x); 0.2 » dydx=diff(y)./diff(x); 0 diff computes the first difference -0.2 -0.4 • Can also operate on matrices » mat=[1 3 5;4 8 6]; » dm=diff(mat,1,2) -0.6 -0.8 -1 0 100 200 300 400 500 600 first difference of mat along the 2nd dimension, dm=[2 2;4 -2] see help for more details The opposite of diff is the cumulative sum cumsum • 2D gradient » [dx,dy]=gradient(mat); 700
  • 24. Numerical Integration • MATLAB contains common integration methods • Adaptive Simpson's quadrature (input is a function) » q=quad('myFun',0,10); q is the integral of the function myFun from 0 to 10 » q2=quad(@(x) sin(x)*x,0,pi) q2 is the integral of sin(x)*x from 0 to pi • Trapezoidal rule (input is a vector) » x=0:0.01:pi; » z=trapz(x,sin(x)); z is the integral of sin(x) from 0 to pi » z2=trapz(x,sqrt(exp(x))./x) x z2 is the integral of e x from 0 to pi
  • 26. ODE Solvers: Method • Given a differential equation, the solution can be found by integration: Evaluate the derivative at a point and approximate by straight line Errors accumulate! Variable timestep can decrease the number of iterations
  • 27. ODE Solvers: MATLAB • MATLAB contains implementations of common ODE solvers • Using the correct ODE solver can save you lots of time and give more accurate results » ode23 Low-order solver. Use when integrating over small intervals or when accuracy is less important than speed » ode45 High order (Runge-Kutta) solver. High accuracy and reasonable speed. Most commonly used. » ode15s Stiff ODE solver (Gear's algorithm), use when the diff eq's have time constants that vary by orders of magnitude
  • 28. ODE Solvers: Standard Syntax • To use standard options and variable time step » [t,y]=ode45('myODE',[0,10],[1;0]) ODE integrator: 23, 45, 15s ODE function Initial conditions Time range • Inputs: ODE function name (or anonymous function). This function takes inputs (t,y), and returns dy/dt Time interval: 2-element vector specifying initial and final time Initial conditions: column vector with an initial condition for each ODE. This is the first input to the ODE function • Outputs: t contains the time points y contains the corresponding values of the integrated variables.
  • 29. ODE Function • The ODE function must return the value of the derivative at a given time and function value • Example: chemical reaction 10 Two equations dA = −10 A + 50 B dt dB = 10 A − 50 B dt A B 50 ODE file: – y has [A;B] – dydt has [dA/dt;dB/dt] Courtesy of The MathWorks, Inc. Used with permission.
  • 30. ODE Function: viewing results • To solve and plot the ODEs on the previous slide: » [t,y]=ode45('chem',[0 0.5],[0 1]); assumes that only chemical B exists initially » » » » » » » plot(t,y(:,1),'k','LineWidth',1.5); hold on; plot(t,y(:,2),'r','LineWidth',1.5); legend('A','B'); xlabel('Time (s)'); ylabel('Amount of chemical (g)'); title('Chem reaction');
  • 31. ODE Function: viewing results • The code on the previous slide produces this figure Chem reaction 1 A B 0.9 Amount of chemical (g) 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.05 0.1 0.15 0.2 0.25 0.3 Time (s) 0.35 0.4 0.45 0.5
  • 32. Higher Order Equations • Must make into a system of first-order equations to use ODE solvers • Nonlinear is OK! • Pendulum example: && θ+ g sin (θ ) = 0 L && θ =− g sin (θ ) L & let θ = γ γ& = − g sin (θ ) L v ⎡θ ⎤ x=⎢ ⎥ ⎣γ ⎦ v & dx ⎡θ ⎤ =⎢ ⎥ dt ⎣γ& ⎦ Courtesy of The MathWorks, Inc. Used with permission.
  • 33. Plotting the Output • We can solve for the position and velocity of the pendulum: » [t,x]=ode45('pendulum',[0 10],[0.9*pi 0]); assume pendulum is almost horizontal » » » » plot(t,x(:,1)); hold on; plot(t,x(:,2),'r'); legend('Position','Velocity'); 8 Position Velocity 6 Position in terms of angle (rad) Velocity (m/s) 4 2 0 -2 -4 -6 -8 0 1 2 3 4 5 6 7 8 9 10
  • 34. Plotting the Output • Or » » » we can plot in the phase plane: plot(x(:,1),x(:,2)); xlabel('Position'); yLabel('Velocity'); • The phase plane is just a plot of one variable versus the other: 8 6 Velocity is greatest when theta=0 4 Velocity 2 0 -2 Velocity=0 when theta is the greatest -4 -6 -8 -3 -2 -1 0 Position 1 2 3
  • 35. ODE Solvers: Custom Options • MATLAB's ODE solvers use a variable timestep • Sometimes a fixed timestep is desirable » [t,y]=ode45('chem',[0:0.001:0.5],[0 1]); Specify the timestep by giving a vector of times The function value will be returned at the specified points Fixed timestep is usually slower because function values are interpolated to give values at the desired timepoints • You can customize the error tolerances using odeset » options=odeset('RelTol',1e-6,'AbsTol',1e-10); » [t,y]=ode45('chem',[0 0.5],[0 1],options); This guarantees that the error at each step is less than RelTol times the value at that step, and less than AbsTol Decreasing error tolerance can considerably slow the solver See doc odeset for a list of options you can customize
  • 36. Exercise: ODE • Use ode45 to solve for y ( t ) on the range t=[0 10], with initial condition y ( 0 ) = 10 and dy dt = −t y 10 • Plot the result.
  • 37. Exercise: ODE • Use ode45 to solve for y ( t ) on the range t=[0 10], with initial condition y ( 0 ) = 10 and dy dt = −t y 10 • Plot the result. • Make the following function » function dydt=odefun(t,y) » dydt=-t*y/10; • Integrate the ODE function and plot the result » [t,y]=ode45(‘odefun’,[0 10],10); • Alternatively, use an anonymous function » [t,y]=ode45(@(t,y) –t*y/10,[0 10],10); • Plot the result » plot(t,y);xlabel('Time');ylabel('y(t)');
  • 38. Exercise: ODE • The integrated function looks like this: Function y(t), integrated by ode45 10 9 8 7 y(t) 6 5 4 3 2 1 0 0 1 2 3 4 5 Time 6 7 8 9 10
  • 39. End of Lecture 3 (1) (2) (3) (4) (5) Linear Algebra Polynomials Optimization Differentiation/Integration Differential Equations We're almost done!
  • 40. MIT OpenCourseWare http://ocw.mit.edu 6.094 Introduction to MATLAB® January (IAP) 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.