# Scilab/C4/ODE-Applications/English

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Solving ODEs using Scilab ode Function

Author: Shamika

Keywords: ODEs

Visual Cue
Narration
Slide 1 Dear Friends,

Welcome to the Spoken Tutorial on “Solving ODEs using Scilab ode Function

Slide 2 -Learning Objective Slide At the end of this tutorial, you will learn how to:
• Use Scilab ode function
• Solve typical examples of ODEs and
• Plot the solution

Slide 3 -Learning Objective Slide The typical examples we will be solving are:
• Motion of simple pendulum
• Van der Pol equation
• Lorenz system

Slide 4-System Requirement slide To record this tutorial, I am using
• Ubuntu 12.04 as the operating system
• and Scilab 5.3.3 version

Slide 5- Prerequisites slide To practise this tutorial, a learner
• should have basic knowledge of Scilab
• and should know how to solve ODEs.

To learn Scilab, please refer to the relevant tutorials available on the Spoken Tutorial website.

Slide 6- ode Function The ode function is an ordinary differential equation solver.

The syntax is y equal to ode within paranthesis y zero, t zero, t and f

Here

• y zero is the initial conditon of the ODEs
• t zero is the initial time
• t is the time range
• and f is the function

Slide 7- Motion of Simple pendulum Consider the motion of simple pendulum.

Let theta t be the angle made by the penndulum with the vertical at time t.

We are given the initial conditions

• theta zero is equal to pi by four and
• theta dash of zero is equal to zero.

Slide 8- Motion of Simple pendulum Then the position of the pendulum is described by

theta double dash t minus g by l into sin of theta t equal to zero.

Here

• g equal to 9.8 m per second square is the acceleration due to gravity and
• l equal to zero point five meter is the length of the pendulum.

Slide 9- Motion of Simple pendulum For the given initial conditions, we have to solve the ODE within the time range zero less than equal to t less than equal to five.

We also have to plot the solution.

Open Pendulum.sci on Scilab Editor Let us look at the code for solving this problem.

Open pendulum dot sci on Scilab editor.

Highlight

y0=[%pi/4 0]';

t0=0;

t=0:1:5

The first line of the code defines the initial conditions of the ODE.

Then we define the intial time value. And we provide the time range.

Highlight

function dy=Pendulum(t, y)

dy(1) = y(2);

dy(2) = (9.8/0.5)*sin(y(1));

endfunction

Next, we convert the given equation to a system of first order ODEs.

We substitute the values of g and l.

Here we take y to be the given variable theta and y dash to be theta dash.

Highlight

y=ode(y0,t0,t,Pendulum)

Then we call the ode function with arguments y zero, t zero, t and the function Pendulum.
Highlight

plot(t,y(1,:),'-*',t,y(2,:),'-')

The solution to the equation will be a matrix with two rows.

The first row will contain the values of y in the given time range.

The second row will contain the values of y dash within the time range.

Hence we plot both the rows with respect to time.

Click on Execute and select Save and Execute Save and execute the file Pendulum dot sci
Show plot The plot shows how the values of y and y dash vary with time.
Switch to Scilab console Switch to Scilab console
Type y

Press Enter

If you want to see the values of y, type y on the console and press Enter.

The values of y and y dash are displayed.

Slide 10- Van der Pol Equation Let us solve Van der Pol equation using the ode function.

We are given the equation

v double dash of t plus epsilon into v of t square minus one into v dash of t plus v of t equal to zero.

The initial conditions are v of two equal to one and v dash of two equal to zero.

Assume epsilon is equal to zero point eight nine seven.

We have to find the solution within the time range two less than t less than ten and then plot the solution.

Open Vanderpol.sci on scilab editor Let us look at the code for Van der Pol equation.

Switch to Scilab editor and open van der pol dot sci.

Highlight

y0=[1 0]';

t0=0;

t=2:1:10;

We define the initial conditions of the ODEs and time and then define the time range.

Since the inital time value is given as two, we start the time range at two.

Highlight

function dy=Vanderpol(t, y)

dy(1) = y(2);

dy(2) = -0.897*(y(1).^2 - 1)*y(2) - y(1);

endfunction

Then we define the function van der pol and construct a system of first order ODEs.

We substitute the value of epsilon with zero point eight nine seven.

Here y refers to the voltage v.

Highlight

y=ode(y0,t0,t,Vanderpol)

Then we call ode function and solve the system of equations.
Highlight

plot(t,y(1,:),'-',t,y(2,:),'--')

Finally we plot y and y dash versus t.
Click on Execute and select Save and Execute Save and execute the file van der pol dot sci.
Show plot The plot showing voltage versus time is shown.

Let's move onto Lorenz system of equations.

Slide 11, 12- Lorenz system

The Lorenz system of equations is given by
• x one dash equal to sigma into x two minus x one,
• x two dash equal to one plus r minus x three into x one minus x two and
• x three dash equal to x one into x two minus b into x three.

The initial conditions are x one zero equal to minus ten, x two zero equal to ten and x three zero equal to twenty five.

Let sigma be equal to ten, r be equal to twenty eight and b equal to eight by three.

Open Lorenz.sci on Scilab editor

Switch to Scilab editor and open Lorenz dot sci
Highlight

x0=[-10 10 25]';

t0=0;

t=0:1:25;

We start by defining the initial conditions of the ODEs.

Since there are three different ODEs, there are three initial conitions.

Then we define the inital time condition and next the time range.

Highlight

function dx=Lorenz(t, x)

sigma = 10;

r = 28;

b = 8/3;

dx(1) = sigma*(x(2) - x(1) );

dx(2) = ((1 + r) - x(3))*x(1) - x(2);

dx(3) = x(1)*x(2) - b*x(3);

endfunction

We define the function Lorenz and then define the given constants sigma, r and b. Then we define the first order ODEs.
Highlight

x=ode(x0,t0,t,Lorenz)

Then we call the ode function to solve the Lorenz system of equations.

We equate the solution to x.

Highlight

plot(t,x(1,:),'**',t,x(2,:),'--', t,x(3,:),'..')

Then we plot x one, x two and x three versus time.
Click on Execute and select Save and Execute Save and execute the file Lorenz dot sci.
Show plot The plot of x one, x two and x three versus time is shown.
Slide 13- Summary Let us summarize this tutorial.

In this tutorial we have learnt to develop Scilab code to solve an ODE using Scilab ode function.

Then we have learnt to plot the solution.

Show Slide 14

Title: About the Spoken Tutorial Project

• It summarises the Spoken Tutorial project
• If you do not have good bandwidth, you can download and watch it

* About the Spoken Tutorial Project
• Watch the video available given at the link shown below
• It summarises the Spoken Tutorial project
• If you do not have good bandwidth, you can download and watch it

Show Slide 15

Title: Spoken Tutorial Workshops

The Spoken Tutorial Project Team

• Conducts workshops using spoken tutorials
• Gives certificates for those who pass an online test
• For more details, please write to contact@spoken-tutorial.org

The Spoken Tutorial Project Team
• Conducts workshops using spoken tutorials
• Gives certificates for those who pass an online test
• For more details, please write to contact at spoken hyphen tutorial dot org

Show Slide 16

Title: Acknowledgement

• Spoken Tutorial Project is a part of the Talk to a Teacher project
• It is supported by the National Mission on Education through ICT, MHRD, Government of India
• More information on this Mission is available at

* Spoken Tutorial Project is a part of the Talk to a Teacher project
• It is supported by the National Mission on Education through ICT, MHRD, Government of India
• More information on this Mission is available at
• spoken hyphen tutorial dot org slash NMEICT hyphen Intro

This is Ashwini Patil from IIT Bombay signing off. Thanks for joining.