Beginning Programming for Engineers

Slides:



Advertisements
Similar presentations
Formal Computational Skills
Advertisements

MATLAB EXAMPLES Initial-value problems
Ch 7.7: Fundamental Matrices
Dynamics of a four-bar linkage
Differential Equations Math Review with Matlab: Finding Solutions to Differential Equations S. Awad, Ph.D. M. Corless, M.S.E.E. D. Cinpinski E.C.E. Department.
Some foundations of Cellular Simulation Nathan Addy Scientific Programmer The Molecular Sciences Institute November 19, 2007.
Computational Methods in Physics PHYS 3437
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 2 : Dynamics & Numerical Methods Goal : To write a simple physics simulation Topics: Intro.
Dr. Jie Zou PHY Chapter 9 Ordinary Differential Equations: Initial-Value Problems Lecture (I) 1 1 Besides the main textbook, also see Ref.: “Applied.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Ordinary Differential Equations Equations which are.
Ordinary Differential Equations. Outline Announcements: –Homework II: Solutions on web –Homework III: due Wed. by 5, by Structs Differential Equations.
Dr. Jie Zou PHY Chapter 9 Ordinary Differential Equations: Initial-Value Problems Lecture (II) 1 1 Besides the main textbook, also see Ref.: “Applied.
Ordinary Differential Equations. Outline Announcements: –Homework II: Solutions on web –Homework III: due Wed. by 5, by Homework II Differential.
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.
Chapter 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6 th edition)
Differential Equations and Boundary Value Problems
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Daniel Baur ETH Zurich, Institut.
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers Daniel Baur ETH Zurich, Institut.
MATLAB Ordinary Differential Equations – Part I Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University September.
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Daniel Baur ETH Zurich, Institut.
Numerical Solutions to ODEs Nancy Griffeth January 14, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis.
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 8 Linear Algebraic Equations PowerPoint to accompany Copyright © 2010.
CMPSC 200 Spring 2013 Lecture 38 November 18 or 20, 2013 (depending on section)
Erin Catto Blizzard Entertainment Numerical Integration.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Engineering Computation and Simulation Conor Brennan Dublin City University EE317.
1 Tips for solving Project 1 Reactor SO 3 SO 2 +O 2.
Math 3120 Differential Equations with Boundary Value Problems Chapter 4: Higher-Order Differential Equations Section 4-9: Solving Systems of Linear Differential.
An Introduction to Programming and Algorithms. Course Objectives A basic understanding of engineering problem solving process. A basic understanding of.
C OMPUTATION AND S IMULATION EE A SSIGNMENT T WO By: Shimiao Cheng, Femi Adeleke, Hanieh Alirezaeeabyaneh.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Multiple Integration 14 Copyright © Cengage Learning. All rights reserved.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
An Introduction to MathCAD You can always find a solution !!
Numerical Methods for Solving ODEs Euler Method. Ordinary Differential Equations  A differential equation is an equation in which includes derivatives.
First, a little review: Consider: then: or It doesn’t matter whether the constant was 3 or -5, since when we take the derivative the constant disappears.
Mathematical Applications using MATLAB (Cont….)
Introduction to MATLAB Session 4 Simopekka Vänskä, THL 2010.
Ordinary Differential Equations (ODEs) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Michael Sokolov ETH Zurich, Institut.
Warm-Up 4-1: Antiderivatives & Indefinite Integrals ©2002 Roy L. Gover ( Objectives: Define the antiderivative (indefinite integral)
SIMULINK-Tutorial 1 Class ECES-304 Presented by : Shubham Bhat.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical Calculus and Differential Equations.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 7 - Chapter 25.
Absolute Values Review Defn: A non-directional distance from zero. Absolute values are always non-negative. Does not exist.
Chapter 10 ordinary differential equations (ODEs) Chapter 11 systems of ODEs (6 th edition)
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
Part 6 Chapter 21 Adaptive Methods and Stiff Systems PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The McGraw-Hill.
ME451 Kinematics and Dynamics of Machine Systems Numerical Integration. Stiffness: Implicit Methods. October 30, 2013 Radu Serban University of Wisconsin-Madison.
DIFFERENTIAL EQUATIONS Note: Differential equations are equations containing a derivative. They can be solved by integration to obtain a general solution.
Ordinary Differential Equations
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CHAPTER- 3.2 ERROR ANALYSIS. 3.3 SPECIFIC ERROR FORMULAS  The expressions of Equations (3.13) and (3.14) were derived for the general relationship of.
ACSL, POSTECH1 MATLAB 입문 CHAPTER 8 Numerical Calculus and Differential Equations.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 6 - Chapters 22 and 23.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MA2213 Lecture 10 ODE. Topics Importance p Introduction to the theory p Numerical methods Forward Euler p. 383 Richardson’s extrapolation.
1 Week 3 First-order ordinary differential equations (ODE) 1.Basic definitions 2.Separable ODEs 3.ODEs reducible to separable form 4.Linear first-order.
HA1-439: Functions Intro Remember, a relation is ANY set of ordered pairs like (3,2), (-2, 4), (4.5, 6) …It is any set of x’s and y’s. A FUNCTION is a.
Engineering Problem Solution
Physics Lesson 6 Projectile Motion
Mechanical Engineering at Virginia Tech
Projectile Motion Projectile motion is independent vertical and horizontal motion through the air only under the influence of gravity after a having an.
Section Euler’s Method
Ordinary differentiall equations
Adaptive Methods and Stiff Systems
Introduction to Functions
Section 9.4 – Solving Differential Equations Symbolically
Presentation transcript:

Beginning Programming for Engineers Numerical Solutions to Differential Equations

Initial Value Problems We wish to "solve" the ordinary differential equation (ODE):   where we have the initial conditions: By "solve", we want to compute            for Most problems can't be solved analytically!

Euler's Method will use the initial conditions and the derivative to estimate points on the function. Color code: Red: derivative Black: the function Green: large step Blue: smaller step This approximation is sensible because of the very definition of a derivative: the limit as h goes to zero. Large time steps or large distance to cover, and eventually errors are likely to grow too large to be useful. Euler's Method

Matlab's ODE Solvers Matlab provides a selection of solvers to integrate an ODE function over a range.  General form: [t,y] = solver(odefun, tspan, y0) The odefun is the function to be integrated over the interval tspan with initial conditions y0.  The result is the vector t for values of the independent variable in the range tspan, and estimated values y for each value t. The solver is typically ode45. The odefun you will provide actually computes the differential equation.  The solver will compute the solution! There are many solvers in Matlab.  This is only one. Next time: Symbolic Toolbox lets you solve some differential equations symbolically.

Simple usage of ode45 We would like to solve this system numerically: We need a function to calculate   function [dy] = simple_ode(t, y)     dy = -3.7*y; end Call the solver: [t,y] = ode45(@simple_ode, [0 1], 5);

Using ode45, continued Given the example:   [t,y] = ode45(@simple_ode, [0 1], 5); The interval [0 1] is the range over which the independent variable (often x or t) varies.  The solver chooses its own values to use.  Actual values chosen are sorted in t, a column vector. Corresponding values of the integrated function are stored in the column vector y. Instead of [0 1], additional points can be given, e.g, 0:0.1:1 -- which will be used as the values of the output t (and to compute y).

Function Handles The notation @simple_ode is a function handle, to allow the solver to call our function simple_ode. Small functions can be defined anonymously:   odefun = @(T,Y) -3.7*Y; [t,y] = ode45(odefun, [0 1], 5); or even: [t,y] = ode45(@(T,Y)-3.7*Y, [0 1], 5);

Multiple ODEs The Matlab ODE solvers can solve systems of related equations over the same interval of the independent variable. Example: The Lotka-Volterra predator-prey model can be used to model the population on rabbits (r) and foxes (f): Model this with 

Rabbits and Foxes (2) ... Need the ODE function: function [dy] = foxrabbitode(t,y)     % FOXRABBITODE calculates population changes     % y(1) = rabbits     % y(2) = foxes     alpha = 0.01;     r = y(1);     f = y(2);     dy = [ 2*r - alpha*r*f            -f + alpha*r*f ]; end Calculate on y, a column vector, and produce a column vector.  Each row corresponds to one of the equations.

Rabbits and Foxes (3) ... Create column vector of initial conditions (r0 and f0)   y0 = [ 300; 150 ]; [t,y] = ode45(@foxrabbitode, [0 20], y0); figure; hold on; plot(t, y(:,1), 'b');  % rabbits plot(t, y(:,2), 'r');  % foxes Each column of dy corresponds to the points computed for one of the equations.

Function handles and arguments We might want to pass alpha as an argument to the ODE function:   function [dy] = foxrabbitode2(t,y,alpha)     r = y(1);     f = y(2);     dy = [ 2*r - alpha*r*f            -f + alpha*r*f ]; end Need anonymous function to pass this to ODE function: odefun = @(T,Y) foxrabbitode2(T,Y,alpha); [t,y] = ode45(odefun, interval, y0); Some values: r=300, f=150, alpha=0.01 r=15, f=22 r=102, f=198

Higher-order ODEs Treat higher-order ODEs as systems of related ODEs.   The ODE function receives both y and y', and computes y' and y''.  (Simply copy the received y' value!) We need initial conditions for y and y'. Consider solving: Let: y(1) = 0,  y'(1) = 1 Solve for y(t), for t in [1 30]. This is a key piece to homework 5!

Function for simple second-order ODE function [dy] = second_ode(t,y)     % SECOND_ODE is a simple second order ODE.     %     % y(1) = f(t)    % 1st equation     % y(2) = f'(t)   % 2nd equation     % dy(1) = f'(t)  % 1st equation     % dy(2) = f''(t) % 2nd equation          % Note how we can just copy y(2) to dy(1).     dy = [ y(2)           (-1/t)*sin(y(1)) ];

Solving the second-order ODE % Compute the solutions. [t,y] = ode45(@second_ode, [1 30], [0;1]); % y(:,1) is y(t), and y(:,2) is y'(t). plot(t, y(:,1))

Trajectory as ODE function [dpv] = update_path(t, pv)   %    pv = position and velocity state vector   %        pv(1) = x position, xpos, at time t   %        pv(2) = y position, ypos, at time t   %        pv(3) = x velocity, vx, at time t   %        pv(4) = y velocity, vy, at time t   x = pv(1);   y = pv(2);   xv = pv(3);   yv = pv(4);   dpv = [xv        % dx = x velocity          yv        % dy = y velocity          0         % d2x = d(x velocity) = x acceleration          -9.81 ];  % d2y = d(y velocity) = y acceleration

Event functions Sometimes we need to stop the integration due to some event, especially if we don't know how long to run! You can create an "event function" to stop integration: function [value,isterminal,direction] = event(t,y) This accepts t,y like the ODE function.  It computes a value at t,y.  The output isterminal, if 1, means to stop the integration if value is 0.  The direction indicates when we care about value being 0; if it is 0, then we always care.

Event functions: Trajectory Sample for testing if above ground:   function [value, isterminal, direction] = ...          trajectory_event(t, pv)       if pv(2) > 0         value = 1;  % Above the ground     else         value = 0;  % Hit the ground     end     isterminal = 1; % Stop when value=0     direction = 0;  % Always care about zeros end

Using an event function Use odeset to configure options to the ODE solvers, including events functions:   options = odeset('Event', @trajectory_event); [t,y] = ode45(@update_path, 0:dt:ft, ...                   [x;y;v0x;v0y], options); You can use [0 Inf] as an interval when you don't know when to finish, but have n Event function to stop. You can use more complicated function handles to pass information to the Event function, e.g., the terrain.

Other solvers Some problems are stiff, needing slower specialized solvers.  Other solvers are faster than ode45, but not as accurate. Usually try ode45 first.  If it works but is too slow, consider ode23. If ode45 is so slow it is unusable, you might have a stiff problem.  Try using ode15s or ode23s.  See doc ode45 for much more information, including guidance about choosing a solver, options you can pass to solvers, etc.