Solving ODEs in Matlab. BP205 M.Tremont 1.30.2009



Similar documents
To get started, type one of these: helpwin, helpdesk, or demo. For product information, type tour or visit

Numerical Methods for Differential Equations

Numerical Methods for Differential Equations

Numerical Methods in MATLAB

Cooling and Euler's Method

Homework 2 Solutions

Matlab Practical: Solving Differential Equations

PSTricks. pst-ode. A PSTricks package for solving initial value problems for sets of Ordinary Differential Equations (ODE), v0.7.

A Visualization System and Monitoring Tool to Measure Concurrency in MPICH Programs

THE MATLAB ODE SUITE

Chapter 7 Nonlinear Systems

Tutorial on Using Excel Solver to Analyze Spin-Lattice Relaxation Time Data

Derive 5: The Easiest... Just Got Better!

The Fourth International DERIVE-TI92/89 Conference Liverpool, U.K., July Derive 5: The Easiest... Just Got Better!

CD-ROM Appendix E: Matlab

Spline Toolbox Release Notes

Math 241, Exam 1 Information.

Time domain modeling

Ordinary Differential Equations

A First Course in Elementary Differential Equations. Marcel B. Finan Arkansas Tech University c All Rights Reserved

Euler s Method and Functions

PROGRAMMING FOR CIVIL AND BUILDING ENGINEERS USING MATLAB

Mark Howell Gonzaga High School, Washington, D.C.

15.1. Exact Differential Equations. Exact First-Order Equations. Exact Differential Equations Integrating Factors

Experiment 9. The Pendulum

Introduction to Matlab

INTRODUCTION (Syllabus, Numerical Methods & Computational Tools)

Introduction to ODE solvers and their application in OpenFOAM

Computación I: intro to Matlab. Francesca Maria Marchetti

Appendix H: Control System Computational Aids

Numerical Methods with Excel/VBA:

Introduction to Logistic Regression

Experimental Identification an Interactive Online Course

r (t) = 2r(t) + sin t θ (t) = r(t) θ(t) + 1 = 1 1 θ(t) Write the given system in matrix form x = Ax + f ( ) sin(t) x y z = dy cos(t)

Control and Simulation. in LabVIEW

Time Series Analysis

EDUMECH Mechatronic Instructional Systems. Ball on Beam System

Numerical Methods for Solving Systems of Nonlinear Equations

CS Software Engineering for Scientific Computing. Lecture 16: Particle Methods; Homework #4

Lecture Notes to Accompany. Scientific Computing An Introductory Survey. by Michael T. Heath. Chapter 10

Numerical Methods with Excel/VBA:

Numerical Methods for Differential Equations

Numerical Solution of Differential Equations

Tutorial 2: Using Excel in Data Analysis

NESTML Tutorial. I.Blundell, M.J.Eppler, D.Plotnikov. Software Engineering RWTH Aachen.

Beginner s Matlab Tutorial

Numerical Solution of Delay Differential Equations

Simple Harmonic Motion

Determination of Acceleration due to Gravity

Section 3.7. Rolle s Theorem and the Mean Value Theorem. Difference Equations to Differential Equations

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial VI

Numerical Solution of Differential Equations

MatLab - Systems of Differential Equations

Aim : To study how the time period of a simple pendulum changes when its amplitude is changed.

Below is a very brief tutorial on the basic capabilities of Excel. Refer to the Excel help files for more information.

Contrôle dynamique de méthodes d approximation

MATLAB Tutorial. Chapter 6. Writing and calling functions

WEEK #3, Lecture 1: Sparse Systems, MATLAB Graphics

The mhr model is described by 30 ordinary differential equations (ODEs): one. ion concentrations and 23 equations describing channel gating.

Controller Design in Frequency Domain

EE 402 RECITATION #13 REPORT

Sensor Performance Metrics

Introduction to Numerical Methods and Matlab Programming for Engineers

8. Linear least-squares

Solutions to Homework 5

Introduction to the Finite Element Method

2008 AP Calculus AB Multiple Choice Exam


Signal Processing First Lab 01: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

ANIMATED PHASE PORTRAITS OF NONLINEAR AND CHAOTIC DYNAMICAL SYSTEMS

3.2 Sources, Sinks, Saddles, and Spirals

Command-induced Tracking Jitter Study I D. Clark November 24, 2009

SOLUTION OF SHIFF SYSTEMS BY USING DIFFERENTIAL TRANSFORM METHOD

Student name: Earlham College. Fall 2011 December 15, 2011

Simple Vibration Problems with MATLAB (and Some Help from MAPLE) Original Version by Stephen Kuchnicki

AP Physics C. Oscillations/SHM Review Packet

Lin s Concordance Correlation Coefficient

SIXTY STUDY QUESTIONS TO THE COURSE NUMERISK BEHANDLING AV DIFFERENTIALEKVATIONER I

Unit - 6 Vibrations of Two Degree of Freedom Systems

General Theory of Differential Equations Sections 2.8, , 4.1

2+2 Just type and press enter and the answer comes up ans = 4

Plot and Solve Equations

Web-based Supplementary Materials for Bayesian Effect Estimation. Accounting for Adjustment Uncertainty by Chi Wang, Giovanni

Dynamic Process Modeling. Process Dynamics and Control

Average rate of change of y = f(x) with respect to x as x changes from a to a + h:

A Brief Review of Elementary Ordinary Differential Equations

Adding vectors We can do arithmetic with vectors. We ll start with vector addition and related operations. Suppose you have two vectors

Experiment #1, Analyze Data using Excel, Calculator and Graphs.

Understanding Poles and Zeros

Using Microsoft Excel Built-in Functions and Matrix Operations. EGN 1006 Introduction to the Engineering Profession

Regression Analysis: A Complete Example

Review Solutions MAT V (a) If u = 4 x, then du = dx. Hence, substitution implies 1. dx = du = 2 u + C = 2 4 x + C.

PENDULUM PERIODS. First Last. Partners: student1, student2, and student3

AP Calculus BC 2006 Free-Response Questions

PHYS 2425 Engineering Physics I EXPERIMENT 9 SIMPLE HARMONIC MOTION

Predictor Coef StDev T P Constant X S = R-Sq = 0.0% R-Sq(adj) = 0.

2. Simple Linear Regression

MATLAB and Big Data: Illustrative Example

Transcription:

Solving ODEs in Matlab BP205 M.Tremont 1.30.2009

- Outline - I. Defining an ODE function in an M-file II. III. IV. Solving first-order ODEs Solving systems of first-order ODEs Solving higher order ODEs

What are we doing when numerically solving ODE s? Numerical methods are used to solve initial value problems where it is difficult to obtain exact solutions An ODE is an equation that contains one independent variable (e.g. time) and one or more derivatives with respect to that independent variable. In the time domain, ODEs are initial-value problems, so all the conditions are specified at the initial time t = 0. dy = t y y(0) =1 y(t) = t 2 +1 Matlab has several different functions (built-ins) for the numerical solution of ODEs. These solvers can be used with the following syntax: [outputs] = function_handle(inputs) [t,state] = solver(@dstate,tspan,ics,options) An array. The solution of the ODE (the values of the state at every time). Matlab algorithm (e.g., ode45, ode23) Handle for function containing the derivatives Vector that specifiecs the interval of the solution (e.g., [t0:5:tf]) A vector of the initial conditions for the system (row or column)

What are we doing when numerically solving ODE s? y t 0 y(t) * y(t 0 ) * * * * * * t We know t 0 and y(t 0 ) and we know the slope of y(t), dy/ =f(t,y). We don t know y(t) for any values of t other than t 0. Integrators compute nearby value of y(t) using what we already know and repeat. Higher order numerical methods reduce error at the cost of speed: Euler s Method - 1st order expansion Midpoint method - 2nd order expansion Runge-Kutta - 4th order expansion

Solver Accuracy Description ode45 ode23 Medium Low This should be the first solver you try Less accurate than ode45 Runge-Kutta (4,5) formula ode113 Low to high For computationally intensive problems ode15s Low to medium Use if ode45 fails because the problem is stiff* *No precise definition of stiffness, but the main idea is that the equation includes some terms that can lead to rapid variation in the solution.

Defining an ODE function in an M-file [t,state] = ode45(@dstate,tspan,ics,options) 1. Define tspan, ICs and options in one file (e.g. call_dstate.m), which sets up ode45 2. Define your constants and derivatives in another file (e.g. dstate.m) or as a function dstate within the call file 3. Run call_dstate.m 4. Analyze the results as a plot of state vs. t

II. Solving first-order ODEs Example: dy = y'(t) = "y(t) # $y(t)2 y(0) =10 1 function [t,y] = call_dstate() 2- ts pan = [0 9]; % set tim e inte rval 3- y0 = 10; % set ini tia l condi tio n 4 % dstate evaluates r.h.s. of the ode 5- [t,y] = ode45( @dstate,tspan,y0); 6- plot(t,y) 7- disp ([t,y]) % displays t and y(t) 8 function dy = dstate (t,y) 9- alpha=2; gamma=0.0001; 10- dy = alpha* y-gamma *y^2; 11- end 12- end Save as call_dstate.m in some directory, and cd to that directory in the matlab GUI

Matlab ode45 s numerical solution dy = y'(t) = "y(t) # $y(t)2 y(0) =10 At t = 9, have we reached steady state? lim t"># y(t) = $ % = 20,000 From the command line: EDU>> [t, y] = call_dstate; EDU>> steady_state = y(end) steady_state = 1.9999e+04

III. Solving systems of first-order ODEs van der Pol equations in relaxation oscillation: dy 1 = y 2 y 1 (0) = 0 dy 2 =1000(1" y 1 2 )y 2 " y 1 y 2 (0) =1 This is a system of ODEs because we have more than one derivative with respect to our independent variable, time. This is a stiff system because the limit cycle has portions where the solution components change slowly alternating with regions of very sharp change - so we will need ode15s. This is a example from mathworks, a great resource @ mathworks.com or the software manual. This time we ll create separate files for the call function (call_osc.m) and the ode function (osc.m)

III. Solving systems of first-order ODEs van der Pol equations in relaxation oscillation: dy 1 = y 2 y 1 (0) = 0 dy 2 =1000(1" y 1 2 )y 2 " y 1 y 2 (0) =1 To simulate this system, create a function osc containing the equations. Method 1: preallocate space in a column vector, and fill with derivative functions 1 function dy = osc(t,y) 2- dy = zeros(2,1); % this creates an empty column 3 4-5- 6 7 %vector that you can fill with your two derivatives: dy(1) = y(2); dy(2) = 1000*(1 - y(1)^2)*y(2) - y(1); %In this case, y(1) is y1 and y(2) is y2, and dy(1) %is dy1/ and dy(2) is dy2/. 8-end Save as osc.m in the same directory as before.

III. Solving systems of first-order ODEs van der Pol equations in relaxation oscillation: dy 1 = y 2 y 1 (0) = 0 dy 2 =1000(1" y 1 2 )y 2 " y 1 y 2 (0) =1 To simulate this system, create a function osc containing the equations. Method 2: vectorize the differential functions 1 function dy = osc(t,y) 2- dy = [y(2) 3 1000*(1 - y(1)^2)*y(2) - y(1)]; 4 %Still y(1) is y1 and y(2) is y2, and dy(1) 5 %is dy1/ and dy(2) is dy2/. 6- end Save as osc.m in the same directory as before.

III. Solving systems of first-order ODEs van der Pol equations in relaxation oscillation: dy 1 = y 2 y 1 (0) = 2 dy 2 =1000(1" y 1 2 )y 2 " y 1 y 2 (0) = 0 Now solve on a time interval from 0 to 3000 with the above initial conditions. Create a scatter plot of y 1 with time. 1 function [T,Y] = call_osc() 2- tspan = [0 3000]; 3- y1_0 = 2; 4- y2_0 = 0; 5- [T,Y] = ode15s(@osc,tspan,[y1_0 y2_0]); 6- plot(t,y(:,1),'o') 7-end Save as call_osc.m in the same directory as before.

Plot of numerical solution van der Pol equations in relaxation oscillation: dy 1 = y 2 dy 2 =1000(1" y 1 2 )y 2 " y 1 y 1 (0) = 2 y 2 (0) = 0

IV. Solving higher order ODEs Simple pendulum: 2 d ML = " MG sin 2 2 d G = " sin 2 L Second order non-linear ODE Convert the 2 nd order ODE to standard form: z 1 = ", z 2 = d" / MG dz 1 = z 2 dz 2 = # G L sin(z 1 )

Non-linear pendulum function file G = 9.8 m/s L = 2 m Time 0 to 2π Initial θ = π/3 Try ode23 Plot θ with time z 1 = ", z 2 = d" / dz 1 = z 2 dz 2 = # G L sin(z 1) 1 2-3- 4-5- 6 7-8- 9-10- 11-12- function [] = call_pend() tspan=[0 2*pi]; % set time interval z0=[pi/3,0]; % set initial conditions [t,z]=ode23(@pend,tspan,z0); plot(t,z(:,1)) function dz = pend(t,z) end end G=9.8; L=2; % set constants z1=z(1); % get z1 z2=z(2); % get z2 dz = [z2 ; -G/L*sin(z1);];