Introduction to ODEs in Scilab

Similar documents
Speed and Velocity: Recall from Calc 1: If f (t) gives the position of an object at time t, then. velocity at time t = f (t) speed at time t = f (t)

Linear Algebra and Optimization Using Scilab

Computer Problems for Methods of Solving Ordinary Differential Equations

Systems of Linear ODEs

Hands-on Session: Linear Algebra and Ordinary Differential Equations with SCILAB

Numerical Solution of Differential Equations

Linear Algebra, Optimization and Solving Ordinary Differential Equations Using Scilab

Mathematics Engineering Calculus III Fall 13 Test #1

Linear algebra and differential equations (Math 54): Lecture 20

3.2 Systems of Two First Order Linear DE s

A quadratic expression is a mathematical expression that can be written in the form 2

First Order Linear Ordinary Differential Equations

ENGI 3424 First Order ODEs Page 1-01

5.2 LENGTHS OF CURVES & AREAS OF SURFACES OF REVOLUTION

Lecture 9. Systems of Two First Order Linear ODEs

Solving Differential Equations Using MATLAB

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

37. f(t) sin 2t cos 2t 38. f(t) cos 2 t. 39. f(t) sin(4t 5) 40.

Exam 1 Review SOLUTIONS

Substitutions and by Parts, Area Between Curves. Goals: The Method of Substitution Areas Integration by Parts

MA 102 Mathematics II Lecture Feb, 2015

MATH 2413 TEST ON CHAPTER 4 ANSWER ALL QUESTIONS. TIME 1.5 HRS.

Table of contents. d 2 y dx 2, As the equation is linear, these quantities can only be involved in the following manner:

SOME EXAMPLES OF THE USE OF COMPUTER ALGEBRA SYSTEMS IN INTRODUCTORY COURSES

Section 1.6 Inverse Functions

MATH 307: Problem Set #3 Solutions

Parametric Equations, Function Composition and the Chain Rule: A Worksheet

Section 1.4. Meaning of Slope for Equations, Graphs, and Tables

EEL2216 Control Theory CT1: PID Controller Design

MAT187H1F Lec0101 Burbulla

Chemical Engineering Applications in Scilab

Riemann Sums. Outline. James K. Peterson. September 15, Riemann Sums. Riemann Sums In MatLab

Ordinary Differential Equations

Modeling Rarefaction and Shock waves

(Refer Slide Time: 00:32)

Section 2.1 (First Order) Linear DEs; Method of Integrating Factors. General first order linear DEs Standard Form; y'(t) + p(t) y = g(t)

DIFFERENTIATION RULES

Ordinary Differential Equations

A. Incorrect! Apply the rational root test to determine if any rational roots exist.

1.3 Subtraction of Rational Numbers

O.K. But what if the chicken didn t have access to a teleporter.

Selected Solutions: 3.5 (Undetermined Coefficients)

Limits: How to approach them?

Section 2.1 Differential Equation and Solutions

Time Response of Systems

Review for Ma 221 Final Exam

Advanced Calculus Questions

Ordinary Differential Equations. Monday, October 10, 11

a k 0, then k + 1 = 2 lim 1 + 1

APPLICATIONS OF INTEGRATION

Regression I - the least squares line

Section 7.2 Addition and Subtraction Identities. In this section, we begin expanding our repertoire of trigonometric identities.

Physics 202 Laboratory 3. Root-Finding 1. Laboratory 3. Physics 202 Laboratory

x(t) = t[u(t 1) u(t 2)] + 1[u(t 2) u(t 3)]

Use of Differential Equations In Modeling and Simulation of CSTR

A Glimpse at Scipy FOSSEE. June Abstract This document shows a glimpse of the features of Scipy that will be explored during this course.

Often, in this class, we will analyze a closed-loop feedback control system, and end up with an equation of the form

Week 7: Binary Outcomes (Scott Long Chapter 3 Part 2)

Infinite series, improper integrals, and Taylor series

9.4 CALCULUS AND PARAMETRIC EQUATIONS

Week 2 Techniques of Integration

(Refer Slide Time: 02:11 to 04:19)

1 Differential. Equations. A differential equation is any equation that involves a derivative. For example, Newton s second law F ma

3.5: Euler s Method (cont d) and 3.7: Population Modeling

Computational Fluid Dynamics Prof. Dr. SumanChakraborty Department of Mechanical Engineering Indian Institute of Technology, Kharagpur

MTH4100 Calculus I. Week 6 (Thomas Calculus Sections 3.5 to 4.2) Rainer Klages. School of Mathematical Sciences Queen Mary, University of London

Numerical techniques to solve equations

Lecture IX. Definition 1 A non-singular Sturm 1 -Liouville 2 problem consists of a second order linear differential equation of the form.

Advanced Operations Research Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology, Madras

DIFFERENTIATION RULES

Standard Form of Conics

TAYLOR POLYNOMIALS DARYL DEFORD

Modelling and Mathematical Methods in Process and Chemical Engineering

MATH 307 Introduction to Differential Equations Autumn 2017 Midterm Exam Monday November

CURVILINEAR MOTION: GENERAL & RECTANGULAR COMPONENTS

Math 308 Week 8 Solutions

Parameter Estimation of Mathematical Models Described by Differential Equations

Computational Techniques Prof. Dr. Niket Kaisare Department of Chemical Engineering Indian Institute of Technology, Madras

Solutions to Homework 3

Unit VIII Matrices, Eigenvalues and Eigenvectors

CURVILINEAR MOTION: RECTANGULAR COMPONENTS (Sections )

Ordinary Differential Equations (ODEs)

Predator - Prey Model Trajectories and the nonlinear conservation law

Math 111: Calculus. David Perkinson

ES 111 Mathematical Methods in the Earth Sciences Lecture Outline 15 - Tues 20th Nov 2018 First and Higher Order Differential Equations

Math 3B: Lecture 14. Noah White. February 13, 2016

Normal Modes, Wave Motion and the Wave Equation Hilary Term 2011 Lecturer: F Hautmann

This file is /conf/snippets/setheader.pg you can use it as a model for creating files which introduce each problem set.

Dynamics of Ocean Structures Prof. Dr. Srinivasan Chandrasekaran Department of Ocean Engineering Indian Institute of Technology, Madras

then the substitution z = ax + by + c reduces this equation to the separable one.

Second-Order Homogeneous Linear Equations with Constant Coefficients

Properties of Real Numbers

Exercise 4) Newton's law of cooling is a model for how objects are heated or cooled by the temperature of an ambient medium surrounding them.

1.4 Techniques of Integration

Puzzle 1 Puzzle 2 Puzzle 3 Puzzle 4 Puzzle 5 /10 /10 /10 /10 /10

Spring 2015, MA 252, Calculus II, Final Exam Preview Solutions

CLEP College Algebra - Problem Drill 21: Solving and Graphing Linear Inequalities

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

(Refer Slide Time: 01:17)

Chapter 2 Notes, Kohler & Johnson 2e

Transcription:

Introduction to Aditya Sengupta Indian Institute of Technology Bombay apsengupta@iitb.ac.in April 15th, 2010, Smt. Indira Gandhi College of Engineering

Scilab can be used to model and simulate a variety of systems, such as: 1 Ordinary Differential Equations 2 Boundary Value Problems 3 Difference Equations 4 Differential Algebraic Equations We will deal with Ordinary Differential Equations in this talk.

Scilab can be used to model and simulate a variety of systems, such as: 1 Ordinary Differential Equations 2 Boundary Value Problems 3 Difference Equations 4 Differential Algebraic Equations We will deal with Ordinary Differential Equations in this talk.

We will do two things: 1 Model the ODE in a way Scilab can understand. 2 Solve the system for a given set of initial values.

Modeling the system We will model the system as a first-order equation: ẏ = f (t, y) Note: Scilab tools assume the differential equation to have been written as first order system. Some models are initially written in terms of higher-order derivatives, but they can always be rewritten as first-order systems by the introduction of additional variables

Modeling the system We will model the system as a first-order equation: ẏ = f (t, y) Note: Scilab tools assume the differential equation to have been written as first order system. Some models are initially written in terms of higher-order derivatives, but they can always be rewritten as first-order systems by the introduction of additional variables

Modeling the system We will model the system as a first-order equation: ẏ = f (t, y) Note: Scilab tools assume the differential equation to have been written as first order system. Some models are initially written in terms of higher-order derivatives, but they can always be rewritten as first-order systems by the introduction of additional variables

Example Let us consider the simple system: dx dt = sin(2t) We can model this system using this code: 1 f u n c t i o n dx = f ( t, x ) 2 dx = s i n (2 t ) ; 3 e n d f u n c t i o n

Example Let us consider the simple system: dx dt = sin(2t) We can model this system using this code: 1 f u n c t i o n dx = f ( t, x ) 2 dx = s i n (2 t ) ; 3 e n d f u n c t i o n

Solution We know that the solution is supposed to be x = 1 2 cos(2t) + c where c is a constant that depends on the initial value of the problem.

Depending on the initial value, the plot will look like this:

Solving an ODE in Scilab The simulation tool we will use for solving is the ode function The simplest calling sequence for ode is: y=ode(y0, t0, t, f) where y0 is the initial value at t0 and t contains the points in time at which the solution is to be determined. f is the function corresponding to ẏ = f (t, y)

For our example, we will take the initial value to be y0 = -0.5 at t0 = 0. Let us evaluate the ODE from t = 0:0.1:5. The code is: 1 t0 = 0 2 x0 = 0.5 3 t = 0 : 0. 1 : 5 ; 4 x = ode ( x0, t0, t, f ) ; 5 p l o t 2 d ( t, x )

For our example, we will take the initial value to be y0 = -0.5 at t0 = 0. Let us evaluate the ODE from t = 0:0.1:5. The code is: 1 t0 = 0 2 x0 = 0.5 3 t = 0 : 0. 1 : 5 ; 4 x = ode ( x0, t0, t, f ) ; 5 p l o t 2 d ( t, x )

You should get a graph that looks like this:

Higher Order Derivatives When we have ODEs formulated in terms of higher order derivatives, we need to rewrite them as first-order systems. We do this by using variables to fill in the intermediate order derivaties. For example, let us consider the system: d 2 y dt 2 = sin(2t) whose one solution we can easily guess to be y = (1/4)sin(2t)

Higher Order Derivatives When we have ODEs formulated in terms of higher order derivatives, we need to rewrite them as first-order systems. We do this by using variables to fill in the intermediate order derivaties. For example, let us consider the system: d 2 y dt 2 = sin(2t) whose one solution we can easily guess to be y = (1/4)sin(2t)

We convert the second order equation into two first order equations: dy/dt = z dz/dt = sin(2t) Therefore, we have the ode in the form: where dx and x are vectors: dx/dt = f (t, x) x = [z; sin(2t)] dx = [dy/dt; dz/dt] We then proceed to replace z, dy/dt, and dz/dt with vector components x(2), dx(1), and dx(2)

We convert the second order equation into two first order equations: dy/dt = z dz/dt = sin(2t) Therefore, we have the ode in the form: where dx and x are vectors: dx/dt = f (t, x) x = [z; sin(2t)] dx = [dy/dt; dz/dt] We then proceed to replace z, dy/dt, and dz/dt with vector components x(2), dx(1), and dx(2)

We convert the second order equation into two first order equations: dy/dt = z dz/dt = sin(2t) Therefore, we have the ode in the form: where dx and x are vectors: dx/dt = f (t, x) x = [z; sin(2t)] dx = [dy/dt; dz/dt] We then proceed to replace z, dy/dt, and dz/dt with vector components x(2), dx(1), and dx(2)

We model the system thus: 1 f u n c t i o n dx = f ( t, x ) 2 dx ( 1 ) = x ( 2 ) 3 dx ( 2 ) = s i n (2 t ) 4 e n d f u n c t i o n and simulate the ODE thus: 1 t = 0 : 0. 0 1 : 4 %pi ; 2 3 y=ode ( [ 0 ; 1/2], 0, t, f ) ; 4 // Note t h e i m p o r t a n c e o f g i v i n g c o r r e c t s t a r t i n g v a l u e s. Try to put a l t e r n a t e s t a r t i n g v a l u e s and s e e t h e d i f f e r e n c e. 5 6 p l o t 2 d ( t, [ y ( 1, : ) y ( 2, : ) ] ) 7 // The c u r v e i n b l a c k i s t h e f i n a l s o l u t i o n. The o t h e r c u r v e i s f o r i l l u s t r a t i o n to show t h e i n t e r m e d i a t e s t e p.

We model the system thus: 1 f u n c t i o n dx = f ( t, x ) 2 dx ( 1 ) = x ( 2 ) 3 dx ( 2 ) = s i n (2 t ) 4 e n d f u n c t i o n and simulate the ODE thus: 1 t = 0 : 0. 0 1 : 4 %pi ; 2 3 y=ode ( [ 0 ; 1/2], 0, t, f ) ; 4 // Note t h e i m p o r t a n c e o f g i v i n g c o r r e c t s t a r t i n g v a l u e s. Try to put a l t e r n a t e s t a r t i n g v a l u e s and s e e t h e d i f f e r e n c e. 5 6 p l o t 2 d ( t, [ y ( 1, : ) y ( 2, : ) ] ) 7 // The c u r v e i n b l a c k i s t h e f i n a l s o l u t i o n. The o t h e r c u r v e i s f o r i l l u s t r a t i o n to show t h e i n t e r m e d i a t e s t e p.

Root Finding Sometimes- we just want to simulate a differential equation up to the time that a specific event occurs. For example, an engine being revved until it reaches a particular speed- after which the gear is to be changed. For such circumstances, we need to define a quantity that signals the occurance of the event.

Root Finding Sometimes- we just want to simulate a differential equation up to the time that a specific event occurs. For example, an engine being revved until it reaches a particular speed- after which the gear is to be changed. For such circumstances, we need to define a quantity that signals the occurance of the event.

Root Finding Sometimes- we just want to simulate a differential equation up to the time that a specific event occurs. For example, an engine being revved until it reaches a particular speed- after which the gear is to be changed. For such circumstances, we need to define a quantity that signals the occurance of the event.

In Scilab we use the ode root function, which is called thus: [y, rd] = ode("root", y0, t0, t, f, ng, g) where g is a function that becomes zero valued when the constraining event occurs and ng is the size of g. rd is a vector that contains the stopping time as its first element.

In Scilab we use the ode root function, which is called thus: [y, rd] = ode("root", y0, t0, t, f, ng, g) where g is a function that becomes zero valued when the constraining event occurs and ng is the size of g. rd is a vector that contains the stopping time as its first element.

Example Let us consider the example of the engine that is revved. We wish to constrain the revving of the engine till it reaches a certain point. We build a first order approximation of an engine using the following code (call it engine.sci): 1 f u n c t i o n d_revs = engine ( t, revs ) 2 d_revs = %eˆ( revs ) 3 e n d f u n c t i o n We can simulate the behaviour of the engine when it is unconstrained using the following code: 1 e x e c engine. sci 2 revs = ode ( 0, 0, 0 : 0. 1 : 1 0, engine ) ; 3 p l o t 2 d ( 0 : 0. 1 : 1 0, revs )

Example Let us consider the example of the engine that is revved. We wish to constrain the revving of the engine till it reaches a certain point. We build a first order approximation of an engine using the following code (call it engine.sci): 1 f u n c t i o n d_revs = engine ( t, revs ) 2 d_revs = %eˆ( revs ) 3 e n d f u n c t i o n We can simulate the behaviour of the engine when it is unconstrained using the following code: 1 e x e c engine. sci 2 revs = ode ( 0, 0, 0 : 0. 1 : 1 0, engine ) ; 3 p l o t 2 d ( 0 : 0. 1 : 1 0, revs )

Example Let us consider the example of the engine that is revved. We wish to constrain the revving of the engine till it reaches a certain point. We build a first order approximation of an engine using the following code (call it engine.sci): 1 f u n c t i o n d_revs = engine ( t, revs ) 2 d_revs = %eˆ( revs ) 3 e n d f u n c t i o n We can simulate the behaviour of the engine when it is unconstrained using the following code: 1 e x e c engine. sci 2 revs = ode ( 0, 0, 0 : 0. 1 : 1 0, engine ) ; 3 p l o t 2 d ( 0 : 0. 1 : 1 0, revs )

We then write the constraining function (call it gearbox.sci): 1 f u n c t i o n stop = gearbox ( t, revs ) 2 stop = 1. 5 revs //We choose to s t o p t h e e n g i n e when t h e r e v s r e a c h t h e v a l u e 1. 5 ( You can choose any o t h e r v a l u e ) 3 e n d f u n c t i o n We then simulate the behaviour of the engine when it is constrained as above. 1 e x e c engine. sci 2 e x e c gearbox. sci 3 [ revs, stop_time ]= ode ( r o o t, 0, 0, 0 : 0. 1 : 1 0, engine, 1, gearbox ) ; 4 p l o t 2 d ( [ 0 : 0. 1 : stop_time ( 1 ), stop_time ( 1 ) ], revs )

We then write the constraining function (call it gearbox.sci): 1 f u n c t i o n stop = gearbox ( t, revs ) 2 stop = 1. 5 revs //We choose to s t o p t h e e n g i n e when t h e r e v s r e a c h t h e v a l u e 1. 5 ( You can choose any o t h e r v a l u e ) 3 e n d f u n c t i o n We then simulate the behaviour of the engine when it is constrained as above. 1 e x e c engine. sci 2 e x e c gearbox. sci 3 [ revs, stop_time ]= ode ( r o o t, 0, 0, 0 : 0. 1 : 1 0, engine, 1, gearbox ) ; 4 p l o t 2 d ( [ 0 : 0. 1 : stop_time ( 1 ), stop_time ( 1 ) ], revs )

Compare the two graphs- can you see where the simulation was halted in the second case?

Linear Systems Since they appear so often, there are special functions for modeling and simulating linear systems. For instance, you can create a linear system thus: 1 s = p o l y ( 0, s ) 2 sys = s y s l i n ( c, 1/( s+1) ) and simulate it thus: 1 t = 0 : 0. 1 : 1 0 ; 2 y = csim ( s t e p, t, sys ) ; 3 p l o t 2 d ( t, y ) ; 4 5 z = csim ( s i n (5 t ), t, sys ) ; 6 p l o t 2 d ( t, z ) ; 7 8 bode ( sys, 0. 0 1, 100) ;

Linear Systems Since they appear so often, there are special functions for modeling and simulating linear systems. For instance, you can create a linear system thus: 1 s = p o l y ( 0, s ) 2 sys = s y s l i n ( c, 1/( s+1) ) and simulate it thus: 1 t = 0 : 0. 1 : 1 0 ; 2 y = csim ( s t e p, t, sys ) ; 3 p l o t 2 d ( t, y ) ; 4 5 z = csim ( s i n (5 t ), t, sys ) ; 6 p l o t 2 d ( t, z ) ; 7 8 bode ( sys, 0. 0 1, 100) ;

Now try to: Model and simulate your own systems Use the help command to find more options

Thanks