Scilab/C4/ODE-Euler-methods/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Solving ODEs using Euler Methods

Author: Shamika

Keywords: ODEs, Euler method, modified Euler method, video tutorial


Visual Cue
Narration
Slide 1 Dear Friends,

Welcome to the Spoken Tutorial on “Solving ODEs using Euler Methods

Slide 2 -Learning Objective Slide At the end of this tutorial, you will learn how to:
  • Solve ODEs using Euler and Modified Euler methods in Scilab
  • Develop Scilab code to solve ODEs
Slide 3-System Requirement slide To record this tutorial, I am using
  • Ubuntu 12.04 as the operating system
  • and Scilab 5.3.3 version
Slide 4- 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 5- Euler Method In Euler method, we get an accurately approximate solution of the ODE.


It is used to solve initial value problems where initial values of the differential equation are given.


It can be used to solve continuous functions.

Slide 6- Example Let us solve an example using Euler method.


We are given an initial value problem -

y dash is equal to minus two t minus y.

  • The initial value of y is given as minus one
  • and the step length is given as zero point one.


We have to find the value of y at time t equal to zero point five.

Open Euler_ode.sci on Scilab Editor Let us look at the code for Euler method.


Open Euler underscore o d e dot sci on Scilab editor.

Highlight

Euler_ode(f, tinit, yinit, h, N)


We define the function Euler underscore o d e with arguments f, t init, y init, h and n

where

  • f denotes the function to be solved,
  • t init is the initial value of time t,
  • y init is the initial value of y,
  • h is the step length, and n is the number of iterations.


Highlight

t = zeros(N+1, 1)


y = zeros(N+1,1)

Then we initialize the values of t and y to vectors of zeros.
Highlight

t(1) = tinit


y(1) = yinit


We place the initial values of t and y in t of one and y of one respectively.
Highlight

for j = 1:N

t(j+1) = t(j) + h

y(j + 1) = y(j) + h*f(t(j), y(j))

end

Then we iterate from one to N to find the value of y.


Here we apply Euler method to find the value of y.

Highlight

endfunction


Finally we end the function.
Click on Execute and select Save and Execute Save and execute the file Euler underscore o d e dot sci
Switch to Scilab console Switch to Scilab console to solve the example problem.
Type on console

deff('[ydot]=f(t,y)','ydot=(-2*t)-y')

We define the function by typing

d e f f open paranthesis open single quote open square bracket y dot close square bracket equal to f of t comma y close single quote comma open single quote y dot equal to open paranthesis minus two asterisk t close paranthesis minus y close single quote close paranthesis


Press Enter.

Type on console

tinit=0

Then type t init is equal to zero.

Press Enter.

Type on console

yinit=-1

Type y init is equal to minus one.

Press Enter

Type on console

h=0.1

Type step length h is equal to zero point one.

Press Enter.

Type on console

N=5

The step length is zero point one, and we have to find the value of y at zero point five.


So, the number of iterations should be five.

At each iteration, the value of t will be increased by zero point one.


So type capital N is equal to five.


And press Enter.

Type on console

[t, y] = Euler_ode(f, tinit, yinit, h, N)

To call the function, type


open square bracket t comma y close square bracket equal to Euler underscore o d e open paranthesis f comma t init comma y init comma h comma capital N close paranthesis


Press Enter.

Show console The value of y at t equal to zero point five is shown.
Slide 7- Modified Euler Method Now let us look at Modified Euler method.


It is a second order method and is a stable two step method.


We find the average of the function at the beginning and end of time step.

Slide 8- Example Let us solve this example using Modified Euler method.


We are given a function y dash is equal to t plus y plus t y.

  • The initial value of y is one
  • and the step length is zero point zero one.


We have to find

  • the value of y
  • at time t equal to zero point one
  • using Modified Euler's method.
Open ModiEuler_ode.sci on Scilab Editor Let us look at the code for Modified Euler method on Scilab Editor.
Highlight

ModiEuler_ode(f, tinit, yinit, h, N)


We define the function with arguments f, t init, y init, h and n

where

  • f is the function to be solved,
  • t init is the intial time value,
  • y init is the inital value of y,
  • h is the step length and
  • n is the number of iterations.
Highlight

t = zeros(N+1,1)

y = zeros(N+1,1)

Then we initialize the arrays for y and t.
Highlight

t(1) = tinit

y(1) = yinit

We place the initial values of t and y in t of one and y of one respectively.
Highlight

for j = 1:N

t(j+1) = t(j) + h

y(j+1) = y(j) + h*f(t(j), y(j))

y(j+1) = y(j) + h*(f(t(j),y(j)) + f(t(j + 1),y(j)+h*f(t(j),y(j))))/2;

end

We implement Modified Euler Method here.


Here we find the average value of y at the beginning and end of time step.

Click on Execute and select Save and Execute Save and execute the file Modi Euler underscore o d e dot sci.
Switch to Scilab console Switch to Scilab console.
Type clc

Press enter

Clear the screen by typing c l c.

Press Enter.

Type on console

deff('[ydot]=f(t,y)','ydot=t+y+t*y')

Define the function by typing

d e f f open paranthesis open single quote open square bracket y dot close square bracket equal to f of t comma y close single quote comma open single quote y dot equal to t plus y plus t asterisk y close single quote close paranthesis


Press Enter.

Type on console

tinit=0

Then type t init equal to zero

and press Enter

Type on console

yinit=1

Type y init equal to one

and press Enter.

Type on console

h=0.01

Then type h equal to zero point zero one

and press Enter.

Type on console

N=10

Type capital N equal to ten.


Since the number of iterations should be ten to time t equal to zero point one with step length of zero point zero one.


Press Enter.

Type on console

[t, y] = ModiEuler_ode(f, tinit, yinit, h, N)

Then call the function modi euler underscore o d e by typing

open square bracket t comma y close square bracket equal to modi euler underscore o d e open paranthesis f comma t init comma y init comma h comma capital N close paranthesis

Press Enter.

Show console The value of y at t equal to zero point one is shown.
Slide 9- Example Let us summarize this tutorial.


In this tutorial we have learnt to develop Scilab code for Euler and modified Euler methods.


We have also learnt to solve ODEs using these methods in Scilab..

Show Slide 10

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
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


Show Slide 11

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 12

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 signing off. Thanks for joining.

Contributors and Content Editors

Lavitha Pereira, Nancyvarkey