Difference between revisions of "Scilab/C4/ODE-Euler-methods/English"
(Created page with ''''Title of script''': '''Solving ODEs using Euler Methods''' '''Author: Shamika''' '''Keywords: ODEs, Euler method, modified Euler method, video tutorial''' {| style="border…') |
Nancyvarkey (Talk | contribs) |
||
Line 22: | Line 22: | ||
* Solve '''ODEs '''using''' Euler '''and''' Modified Euler methods '''in '''Scilab''' | * Solve '''ODEs '''using''' Euler '''and''' Modified Euler methods '''in '''Scilab''' | ||
* Develop '''Scilab''' code to solve '''ODEs''' | * Develop '''Scilab''' code to solve '''ODEs''' | ||
− | |||
− | |||
|- | |- | ||
Line 29: | Line 27: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using | ||
− | '''Ubuntu 12.04''' as the operating system | + | *'''Ubuntu 12.04''' as the operating system |
− | + | *and '''Scilab 5.3.3''' version | |
− | and '''Scilab 5.3.3''' version | + | |
|- | |- | ||
Line 44: | Line 41: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5- Euler Method | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5- Euler Method | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In '''Euler | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In '''Euler method''', we get an accurately approximate solution of the '''ODE'''. |
Line 57: | Line 54: | ||
− | We are given an initial value problem | + | We are given an initial value problem - |
'''y dash is equal to minus two t minus y'''. | '''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'''. | |
− | We have to find the value of '''y '''at time '''t | + | |
|- | |- | ||
Line 85: | Line 82: | ||
where | where | ||
− | * ''' | + | * '''f '''denotes the function to be solved, |
* '''t init''' is the initial value of time '''t''', | * '''t init''' is the initial value of time '''t''', | ||
* '''y init '''is the initial value of '''y''', | * '''y init '''is the initial value of '''y''', | ||
Line 95: | Line 92: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | t | + | t = zeros(N+1, 1) |
− | y | + | y = zeros(N+1,1) |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we initialize the values of '''t '''and '''y to vectors of zeros. ''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we initialize the values of '''t '''and '''y to vectors of zeros. ''' | ||
Line 104: | Line 101: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | t(1) | + | t(1) = tinit |
− | y(1) | + | y(1) = yinit |
Line 116: | Line 113: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | for j | + | for j = 1:N |
− | t(j+1) | + | t(j+1) = t(j) + h |
− | y(j + 1) | + | y(j + 1) = y(j) + h<nowiki>*</nowiki>f(t(j), y(j)) |
end | end | ||
Line 149: | Line 146: | ||
<nowiki>deff('[ydot]=f(t,y)','ydot=(-2*t)-y')</nowiki> | <nowiki>deff('[ydot]=f(t,y)','ydot=(-2*t)-y')</nowiki> | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We define the function by typing | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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''' | '''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''' | + | Press '''Enter'''. |
|- | |- | ||
Line 162: | Line 159: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type '''t init is equal to zero'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type '''t init is equal to zero'''. | ||
− | Press '''Enter''' | + | Press '''Enter'''. |
|- | |- | ||
Line 178: | Line 175: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''step length h is equal to zero point one'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''step length h is equal to zero point one'''. | ||
− | Press '''Enter''' | + | Press '''Enter'''. |
|- | |- | ||
Line 192: | Line 189: | ||
− | So type '''capital | + | So type '''capital N is equal to five'''. |
Line 204: | Line 201: | ||
− | '''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 | + | '''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''' | + | Press '''Enter'''. |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Show console | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Show console | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The value of''' y | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The value of''' y at t equal to zero point five''' is shown. |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7- Modified Euler Method | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7- Modified Euler Method | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us look at ''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us look at '''Modified Euler method'''. |
Line 225: | Line 222: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8- Example | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8- Example | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us solve this example using ''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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'''. | 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'''. | + | *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. ''' | + | We have to find |
+ | *the value of '''y ''' | ||
+ | *at '''time t equal to zero point one ''' | ||
+ | *using '''Modified Euler's method. ''' | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open ModiEuler_ode.sci on Scilab Editor | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Open ModiEuler_ode.sci on Scilab Editor | ||
− | |||
− | |||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us look at the code for '''Modified Euler method '''on '''Scilab Editor'''. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us look at the code for '''Modified Euler method '''on '''Scilab Editor'''. | ||
Line 253: | Line 252: | ||
where | where | ||
− | * ''' | + | * '''f''' is the '''function''' to be solved, |
* '''t init''' is the intial '''time''' value, | * '''t init''' is the intial '''time''' value, | ||
* '''y init''' is the inital value of '''y''', | * '''y init''' is the inital value of '''y''', | ||
* '''h''' is the '''step length''' and | * '''h''' is the '''step length''' and | ||
* '''n''' is the number of '''iterations'''. | * '''n''' is the number of '''iterations'''. | ||
− | |||
− | |||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | t | + | t = zeros(N+1,1) |
− | y | + | y = zeros(N+1,1) |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we initialize the '''arrays''' for '''y''' and '''t.''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we initialize the '''arrays''' for '''y''' and '''t.''' | ||
Line 272: | Line 269: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | t(1) | + | t(1) = tinit |
− | y(1) | + | y(1) = yinit |
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We place the initial values of''' t''' and''' y '''in''' t of one''' and''' y of one''' respectively. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We place the initial values of''' t''' and''' y '''in''' t of one''' and''' y of one''' respectively. | ||
Line 280: | Line 277: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
− | for j | + | for j = 1:N |
− | t(j+1) | + | t(j+1) = t(j) + h |
− | y(j+1) | + | y(j+1) = y(j) + h<nowiki>*</nowiki>f(t(j), y(j)) |
− | y(j+1) | + | y(j+1) = y(j) + h<nowiki>*</nowiki>(f(t(j),y(j)) + f(t(j + 1),y(j)+h<nowiki>*</nowiki>f(t(j),y(j))))/2<nowiki>;</nowiki> |
end | end | ||
Line 363: | Line 360: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then call the '''function modi euler underscore o d e''' by typing | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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 | + | '''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.''' | Press '''Enter.''' | ||
Line 369: | Line 366: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Show console | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Show console | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The value of '''y | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The value of '''y at t equal to zero point one''' is shown. |
|- | |- |
Revision as of 12:54, 26 December 2013
Title of script: Solving ODEs using Euler Methods
Author: Shamika
Keywords: ODEs, Euler method, modified Euler method, video tutorial
Visual Cue | |
---|---|
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:
|
Slide 3-System Requirement slide | To record this tutorial, I am using
|
Slide 4- Prerequisites slide | To practise this tutorial, a learner
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.
|
Slide 6- Example | Let us solve an example using Euler method.
y dash is equal to minus two t minus y.
|
Open Euler_ode.sci on Scilab Editor | Let us look at the code for Euler method.
|
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
|
Highlight
t = zeros(N+1, 1)
|
Then we initialize the values of t and y to vectors of zeros. |
Highlight
t(1) = tinit
|
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.
|
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 the 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
|
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.
At each iteration, the value of t will be increased by zero point one.
|
Type on console
[t, y] = Euler_ode(f, tinit, yinit, h, N) |
To call the function, type
|
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.
|
Slide 8- Example | Let us solve this example using Modified Euler 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
|
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.
|
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
|
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.
|
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.
|
Show Slide 10
Title: About the Spoken Tutorial Project
|
* About the Spoken Tutorial Project
|
Show Slide 11
Title: Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team
|
Show Slide 12
Title: Acknowledgement
|
* Spoken Tutorial Project is a part of the Talk to a Teacher project
|
This is Ashwini Patil signing off. Thanks for joining. |