Lecture 8: Calculus and Differential Equations

Similar documents
Lecture 8: Calculus and Differential Equations

MTH 452/552 Homework 3

Bindel, Fall 2011 Intro to Scientific Computing (CS 3220) Week 12: Monday, Apr 18. HW 7 is posted, and will be due in class on 4/25.

Numerical Methods for the Solution of Differential Equations

sing matlab Farida Mosally Mathematics Department King Abdulaziz University

Applied Numerical Analysis

Applied Math for Engineers

Scientific Computing with Case Studies SIAM Press, Lecture Notes for Unit V Solution of

Applied Linear Algebra in Geoscience Using MATLAB

Initial value problems for ordinary differential equations

Chapter 9b: Numerical Methods for Calculus and Differential Equations. Initial-Value Problems Euler Method Time-Step Independence MATLAB ODE Solvers

Research Computing with Python, Lecture 7, Numerical Integration and Solving Ordinary Differential Equations

Physics 584 Computational Methods

Fourth Order RK-Method

Rectilinear Motion. Velocity

NUMERICAL SOLUTION OF ODE IVPs. Overview

CS 450 Numerical Analysis. Chapter 9: Initial Value Problems for Ordinary Differential Equations

Virtual University of Pakistan

Syntax. Arguments. Solve m oderately stifo DEsand DAEs;trapezoidalrule. 1 of :34

4. Numerical analysis II

Math 128A Spring 2003 Week 11 Solutions Burden & Faires 5.6: 1b, 3b, 7, 9, 12 Burden & Faires 5.7: 1b, 3b, 5 Burden & Faires 5.

Mathematics for chemical engineers. Numerical solution of ordinary differential equations

4.4 Computing π, ln 2 and e

Numerical Integration of Functions

MATHEMATICAL METHODS INTERPOLATION

COSC 3361 Numerical Analysis I Ordinary Differential Equations (II) - Multistep methods

Solving ODEs and PDEs in MATLAB. Sören Boettcher

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF MATHEMATICS ACADEMIC YEAR / EVEN SEMESTER QUESTION BANK

ECE257 Numerical Methods and Scientific Computing. Ordinary Differential Equations

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

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

Chapter 5 Exercises. (a) Determine the best possible Lipschitz constant for this function over 2 u <. u (t) = log(u(t)), u(0) = 2.

Math Numerical Analysis Homework #4 Due End of term. y = 2y t 3y2 t 3, 1 t 2, y(1) = 1. n=(b-a)/h+1; % the number of steps we need to take

Differential Equations

Lesson 14: Van der Pol Circuit and ode23s

Numerical Methods for Engineers. and Scientists. Applications using MATLAB. An Introduction with. Vish- Subramaniam. Third Edition. Amos Gilat.

Scientific Computing: An Introductory Survey

An Overly Simplified and Brief Review of Differential Equation Solution Methods. 1. Some Common Exact Solution Methods for Differential Equations

Multistep Methods for IVPs. t 0 < t < T

Introduction to MATLAB

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

A First Course on Kinetics and Reaction Engineering Supplemental Unit S5. Solving Initial Value Differential Equations

Numerical Differential Equations: IVP

PARTIAL DIFFERENTIAL EQUATIONS

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

This practice exam is intended to help you prepare for the final exam for MTH 142 Calculus II.

Linear Multistep Methods I: Adams and BDF Methods

Initial Value Problems

Appendix 3B MATLAB Functions for Modeling and Time-domain analysis

Linearized Equations of Motion!

Name of the Student: Unit I (Solution of Equations and Eigenvalue Problems)

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Ordinary Differential Equations (ODE)

AMS 27L LAB #8 Winter 2009

Lecture 10: Linear Multistep Methods (LMMs)

Lecture 42 Determining Internal Node Values

Mathematical Computing

Quantitative Understanding in Biology Module IV: ODEs Lecture I: Introduction to ODEs

AM205: Assignment 3 (due 5 PM, October 20)

6.0 INTRODUCTION TO DIFFERENTIAL EQUATIONS

24, B = 59 24, A = 55

multistep methods Last modified: November 28, 2017 Recall that we are interested in the numerical solution of the initial value problem (IVP):

Ordinary Differential Equations (ode)

Solving PDEs with PGI CUDA Fortran Part 4: Initial value problems for ordinary differential equations

MATH 350: Introduction to Computational Mathematics

Bessel s Equation. MATH 365 Ordinary Differential Equations. J. Robert Buchanan. Fall Department of Mathematics

Ordinary Differential Equations

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

First In-Class Exam Solutions Math 246, Professor David Levermore Tuesday, 21 February log(2)m 40, 000, M(0) = 250, 000.

Numerical Methods for Differential Equations

Quantitative Understanding in Biology Module IV: ODEs Lecture I: Introduction to ODEs

Lecture 4: Numerical solution of ordinary differential equations

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

Integration of Ordinary Differential Equations

Numerical Methods - Initial Value Problems for ODEs

FORTRAN 77 Lesson 7. Reese Haywood Ayan Paul

Ordinary Differential Equations

Math 23: Differential Equations (Winter 2017) Midterm Exam Solutions

Lecture 1. Scott Pauls 1 3/28/07. Dartmouth College. Math 23, Spring Scott Pauls. Administrivia. Today s material.

M.SC. PHYSICS - II YEAR

Lecture 17: Ordinary Differential Equation II. First Order (continued)

Math 128A Spring 2003 Week 12 Solutions

Graded Project #1. Part 1. Explicit Runge Kutta methods. Goals Differential Equations FMN130 Gustaf Söderlind and Carmen Arévalo

SOLUTION OF EQUATION AND EIGENVALUE PROBLEMS PART A( 2 MARKS)

Lecture 10. (2) Functions of two variables. Partial derivatives. Dan Nichols February 27, 2018

Fall 2003 Math 308/ Numerical Methods 3.6, 3.7, 5.3 Runge-Kutta Methods Mon, 27/Oct c 2003, Art Belmonte

Solution for Assignment 4

FIRST-ORDER ORDINARY DIFFERENTIAL EQUATIONS III: Numerical and More Analytic Methods David Levermore Department of Mathematics University of Maryland

Chapter 6 - Ordinary Differential Equations

Numerical integration and differentiation. Unit IV. Numerical Integration and Differentiation. Plan of attack. Numerical integration.

Part IB Numerical Analysis

First In-Class Exam Solutions Math 246, Professor David Levermore Thursday, 20 September 2018

Products & Services Solutions Academia Support User Commu

Lecture 4. Programming

2.29 Numerical Fluid Mechanics Fall 2011 Lecture 20

Numerical Integration and Numerical Differentiation. Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

A Variable-Step Double-Integration Multi-Step Integrator

Solution: In standard form (i.e. y + P (t)y = Q(t)) we have y t y = cos(t)

Math 308 Week 8 Solutions

8.1 Introduction. Consider the initial value problem (IVP):

Transcription:

Lecture 8: Calculus and Differential Equations Dr. Mohammed Hawa Electrical Engineering Department University of Jordan EE21: Computer Applications. See Textbook Chapter 9. Numerical Methods MATLAB provides many functions that support numerical solutions to common math problems: Integration and Differentiation (Calculus) Finding zeros of a function Solving ordinary differential equations Many others Numerical analysis provides answers as numbers, not closed-form solutions as in analytical solutions (see next lecture for symbolic math in MATLAB). 2 1

The integral of f(x) is the area A under the curve of f (x) from x = a to x = b. = 3 Illustration of Numerical Integration: (a) rectangular method and (b) more accurate trapezoidal method. 4 2

Example sin cos 1 1 2 trapz(x,y) Uses trapezoidal integration to compute the integral of y with respect to x, where the array y contains the function values at the points contained in the array x. >> x = linspace(,pi,1); >> y = sin(x); >> A = trapz(x,y) A = 1.9797 >> x = linspace(,pi,1); >> y = sin(x); >> A = trapz(x,y) A = 1.9998 5 Simpson s Rule Another approach to numerical integration is Simpson s Rule, which divides the integration range [a, b] into an even number of sections and uses a different quadratic function to represent the integrand for each panel. 6 3

Important numerical integration functions: quad(fun, a, b) quad(fun, a, b, tol) Uses an adaptive Simpson s rule to compute the integral of the function whose handle is fun, with a the lower limit and b the upper limit. The function fun must accept a vector argument. The parameter tol is optional, and indicates the specified error tolerance. quadl(fun,a,b) Uses Lobatto quadrature to compute the integral of the function fun. The rest of the syntax is identical to quad. dblquad(fun, a, b, c, d) triplequad(fun,a,b,c,d,e,f) computes the integral of f(x,y,z) from x = a to b, y = c to d, and z = e to f. The function must accept a vector x, and scalar y and z. computes the integral of f(x,y) from x = a to b, and y = c to d. The function fun must accept a vector argument x and scalar y, and it must return a vector result. 7 Although the quad and quadl functions are more accurate than trapz, they are restricted to computing the integrals of functions and cannot be used when the integrand is specified by a set of points. For such cases, use the trapz function. 8 4

MATLAB function quad implements an adaptive version of Simpson s rule, while the quadl function is based on an adaptive Lobatto integration algorithm. To compute the integral of sin(x) from to π, type >> A = quad(@sin,,pi) The answer given by MATLAB is 2., which is correct. We use quadl the same way; namely, >>A = quadl(@sin,,pi). 9 To integrate cos(x 2 ) from to 2, create the function in an m-file: function yy = cossq(x) yy = cos(x.^2); Note that we must use array exponentiation. Then quad function is called as follows: 1.5 >> quad(@cossq,, sqrt(2*pi)) ans =.6119 1.5 -.5-1 -1.5.5 1 1.5 2 2.5 Or you can use an anonymous function: >> f = @(x)(1./(x.^3-2*x - 5)); >> quad(f,, 2) ans = -.465-1.5.2.4.6.8 1 1.2 1.4 1.6 1.8 2 -.5-1 1 5

Double and Triple Integrals A = dblquad(fun, a, b, c, d) computes the integral of f(x,y) from x = a to b, and y = c to d. Example: f(x,y) = xy 2. >> fun = @(x,y) x.*y^2; >> A = dblquad(fun, 1, 3,, 1) A = 1.3333 A = triplequad(fun, a, b, c, d, e, f) computes the triple integral of f(x,y, z) from x = a to b, y = c to d, and z = e to f. Example: f(x,y,z) = (xy -y 2 )/z. >> fun = @(x,y,z)(x*y - y^2)/z; >> A = triplequad(fun, 1,3,,2, 1,2) A = 1.8484, Note: The function must accept a vector x, but scalar y and z.,, 11 Be careful: function singularity >> f = @(x) ( 1./(x-1)); >> quad(f,, 2) Warning: Infinite or Nota-Number function value encountered. > In quad at 113 ans = NaN 1 8 6 4 2-2 1 1-4 -6-8 -1.2.4.6.8 1 1.2 1.4 1.6 1.8 2 12 6

Numerical differentiation: Illustration of estimating the derivative dy/dx. lim 13 MATLAB provides the diff function to use for computing derivative estimates. d = diff(y), where y is a vector of n elements, the result is a vector d containing n 1 elements that are the differences between adjacent elements in y. That is: d=[y(2)-y(1), y(3)-y(2),..., y(n)-y(n-1)] For example: >> y = [5, 7, 12, -2]; >> diff(y) ans = 2 5-32 14 7

Example 6 4 f(x) df/dx step =.1; x = : step : pi; y = sin(x.^2); d = diff(y)/step; % an approximation % to derivative % 2.*x.*cos(x.^2) plot(x,y,'k',x(2:end),d,'--'); legend('f(x)', 'df/dx'); 2-2 -4-6 -8.5 1 1.5 2 2.5 3 3.5 15 Ordinary Differential Equations An ordinary differential equation (ODE) is an equation containing ordinary derivatives of the dependent variable. An equation containing partial derivatives with respect to two or more independent variables is a partial differential equation (PDE). We limit ourselves to ODE that must be solved for a given set of initial conditions. Solution methods for PDEs are an advanced topic, and we do not look at them. 16 8

Several Methods Several numerical methods to solve ODEs. Examples include: Euler and Backward Euler methods Predictor-Corrector method First-order exponential integrator method Runge-Kutta methods Adams-Moulton methods Gauss-Radau methods Adams-Bashforth methods Hermite Obreschkoff methods Fehlberg methods Parker Sochacki methods Nyström methods Quantized State Systems methods 17 Multiple Solvers MATLAB offers multiple ODE solvers, each uses different methods. Ode23: Solves non-stiff differential equations, low order method. ode45: Solves non-stiff differential equations, medium order method: uses a combination of fourth- and fifth-order Runge-Kutta methods. ode23s: Solves stiff differential equations, low order method. ode15i: Solves fully implicit differential equations, variable order method. And so on. We will limit ourselves to the ode45 solver. 18 9

Example: Find the response of the first-order RC circuit. + = = (.. ) () = / ( ) = = + = = (.. ) = + ( ) / ( ) 19 Solving First-Order Differential Equations First write the equation as dy/dt = f(t,y) then solve it using this syntax: [t,y] = ode45(@f,tspan,y) where @f is the handle of the function file whose inputs must be t and y, and whose output must be a column vector representing dy/dt; that is, f(t,y). The number of rows in the output column vector must equal the order of the equation. The array tspan contains the starting and ending values of the independent variable t, and optionally any intermediate values. The array y contains the initial values of y. If the equation is first order, then y is a scalar. 2 1

The circuit model for zero input voltage and =.1 is:.1 + = And the i.c. is () = 2 V. First re-write the equation in the required format: = 1 Next define the following function file. Note that the order of the input arguments must be t and y. f = @(t,y) -1*y; 21 The solver is called as follows, and the solution plotted along with the analytical solution y_true. The initial condition is () = 2. f = @(t,y) -1*y; [t, y] = ode45(f, [.5], 2); y_analytical = 2*exp(-1*t); plot(t,y,'o', t, y_analytical); legend('ode solver', 'Actual'); xlabel('time(s)'); ylabel('capacitor Voltage'); Note that we need not generate the array t to evaluate y_analytical, because t is generated by the ode45 function. The plot is shown on the next slide. 22 11

Free (natural) response of an RC circuit (decaying exponential). 2 1.8 ODE solver Actual 1.6 1.4 Capacitor Voltage 1.2 1.8.6.4.2.5.1.15.2.25.3.35.4.45.5 Time(s) 23 The circuit model for input voltage = 1 and =.1:.1 + = 1 And the i.c. is () = 2 V. First re-write the equation in the required format: = 1 + 1 Next define the following function file. Note that the order of the input arguments must be t and y. f = @(t,y) -1*y+1; 24 12

The solver is called as follows, and the solution plotted along with the analytical solution y_true. The initial condition is () = 2. f = @(t,y) -1*y+1; [t, y] = ode45(f, [.5], 2); y_analytical = 1+(2-1)*exp(-1*t); plot(t,y,'o', t, y_analytical); legend('ode solver', 'Actual'); xlabel('time(s)'); ylabel('capacitor Voltage'); Note that we need not generate the array t to evaluate y_analytical, because t is generated by the ode45 function. The plot is shown on the next slide. 25 Natural plus forced (total) response of an RC circuit (increasing exponential). 11 1 9 8 Capacitor Voltage 7 6 5 4 3 2 1 ODE solver Actual.5.1.15.2.25.3.35.4.45.5 Time(s) 26 13

The circuit model for input voltage = 1 /. sin. and =.1:.1 + = 1/. sin 2.3 And assume the i.c. is () = V. First re-write the equation in the required format: = 1 + 1/. sin 2.3 Next define the following function file. Note that the order of the input arguments must be t and y. f = @(t,y) -1*y+1*... exp(-1*t/.3).*sin(2*pi*t/.3); 27 Result 1 Applied Voltage 5-5 -1.2.4.6.8 1 1.2 Time (s) 1 Capacitor Voltage.5 -.5.2.4.6.8 1 1.2 Time (s) 28 14

Extension to Higher-Order Equations To use the ODE solvers to solve an equation of 2 nd order or higher, you must first write the equation as a set of first-order equations. Example: 5 + 7 + 4 = By re-arranging to get the highest derivative: = 1 5 4 5 7 5 29 Example (Continue) 1 5 4 5 7 5 We then change variables: / Hence: / / Also:. Hence we have two equations: 1 5 4 5 7 5 3 15

Example (Continue) 1 5 4 5 7 5 This form is sometimes called the Cauchy form or the state-variable form. We now define a function that accepts two values of x and then computes the values of / and / and stores them in a column vector. 31 Example (Code) 1 5 sin 4 5 7 5 d = @(t,x) [x(2); sin(t)/5-4*x(1)/5-7*x(2)/5]; [t, x] = ode45(d, [ 6], [3 9]); Here 3 and 9, and we solve for 6. Also sin. Note x is a matrix with two columns. The first column contains the values of x 1 at the various times generated by the solver; the second column contains the values of x 2. If you type plot(t, x), you will obtain a plot of both x 1 and x 2 versus t. Thus, type plot(t, x(:,1)) to see the result for y. 32 16

Result 7 6 5 4 3 2 1-1 1 2 3 4 5 6 33 HW: Alternative Solution Define the function in an m-file: function xdot = d(t, x) xdot(1) = x(2); xdot(2) = (1/5)*(sin(t)-4*x(1)-7*x(2)); xdot = [xdot(1); xdot(2)]; Use the function to solve the ODE: [t, x] = ode45(@d, [ 6], [3 9]); % notice the need to use handles plot(t, x(:,1)); 34 17

Homework Solve as many problems from Chapter 9 as you can Suggested problems: Solve: 9.1, 9.4, 9.14, 9.16, 9.23, 9.27, 9.31, 9.34. 35 18