Professional Documents
Culture Documents
1. there was a ball that has a velocity of 100m/s then when it hits the ground the acceleration has
decreases to Xo=0 to Xf=2 with the time of y= 1s the differential equation of the velocity of the
𝑑𝑦 6
ball is given by = 𝑦𝑥 2 − 𝑦
𝑑𝑥 5
Find the velocity at time n=5s
Manual solution: 1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3
Step 1 y3=0.438218
K1=f(x0,y0)
= f(0,1)
= -1.2 Step 4
3 3
K2=𝑓(𝑥0 + 4 𝑛, 𝑦0 + 4 𝑘1𝑛) K1=f(x3,y3)
=f(2/5,13/25) =f(1.2,0.438218)
=-0.5409 =0.105172
1 2
ynew=y+(3 𝑘1 + 3 𝑘2)𝑛
3 3
y1= 0.65184 K2=𝑓(𝑥2 + 𝑛, 𝑦2 + 𝑘1𝑛)
4 4
=f(1.6,0.482086)
Step 2 =0.65319
K1=f(x1,y1)
=f(2/5,0.65184)
=-0.677914 1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3
y4=0.58989
3 3
K2=𝑓(𝑥1 + 4 𝑛, 𝑦1 + 4 𝑘1𝑛)
=f(0.8,0.380675) Step 5
=-0.213178 K1=f(x4,y4)
=f(1.6,0.58989)
1 2 =0.80225
ynew=y+(3 𝑘1 + 3 𝑘2)𝑛
y2=0.473622
3 3
K2=𝑓(𝑥2 + 4 𝑛, 𝑦2 + 4 𝑘1𝑛)
Step 3 =f(2,0.91079)
K1=f(x2,y2) =2.55021
=f(0.8,0.473622)
=-0.265228
1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3 3 3
K2=𝑓(𝑥2 + 4 𝑛, 𝑦2 + 4 𝑘1𝑛) y5=1.26038
=f(1.2,0.36753)
=0.0882073 True error = exact-approximate
=1.30561-1.26038
=0.0452262
Matlab result
***************************Introduction*******************************
The 2nd Order Runge-Kutta method approximates the solution to an
ordinary differential equation by using the equation expressed in
the form dy/dx = f(x,y) to approximate the slope. This slope is used
to project the solution to the ODE a fixed distance away.
***************************Input Data*******************************
Below are the input parameters to begin the simulation which can be
changed in the m-file input section
f = dy/dx
x0 = initial x
y0 = initial y
xf = final x
a2 = constant value between 0 and 1.
= 0.5, Heun Method
= 1.0, Midpoint Method
= 0.66667, Ralston's Method
n = number of steps to take
-----------------------------------------------------------------
-----------------------------------------------------------------
For this simulation, the following parameter is constant.
h = ( xf - x0 ) / n
=(2-0)/5
= 0.4
a1 = 1 - a2
= 1 - 0.5
= 0.5
p1 = 1 / ( 2 * a2 )
= 1 / ( 2 * 0.5 )
=1
q11 = p1
=1
***************************Simulation******************************
Step 1
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x0 , y0 )
= f( 0 , 1 ) )
= -1.2
k2 = f( x0 + p1 * h , y0 + q11 * k1 * h )
= f( 0 + 1 * 0.4 , 1 + 1 * -1.2 * 0.4)
= f( 0.4 , 0.52 )
= -0.5408
at x1 = 0.4
Step 2
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x1 , y1 )
= f( 0.4 , 0.65184 ) )
= -0.677914
k2 = f( x1 + p1 * h , y1 + q11 * k1 * h )
= f( 0.4 + 1 * 0.4 , 0.65184 + 1 * -0.677914 * 0.4)
= f( 0.8 , 0.380675 )
= -0.213178
at x2 = 0.8
Step 3
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x2 , y2 )
= f( 0.8 , 0.473622 ) )
= -0.265228
k2 = f( x2 + p1 * h , y2 + q11 * k1 * h )
= f( 0.8 + 1 * 0.4 , 0.473622 + 1 * -0.265228 * 0.4)
= f( 1.2 , 0.36753 )
= 0.0882073
at x3 = 1.2
Step 4
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x3 , y3 )
= f( 1.2 , 0.438218 ) )
= 0.105172
k2 = f( x3 + p1 * h , y3 + q11 * k1 * h )
= f( 1.2 + 1 * 0.4 , 0.438218 + 1 * 0.105172 * 0.4)
= f( 1.6 , 0.480286 )
= 0.65319
2) Apply the Runge-Kutta 2nd Order method to estimate y4
y4 = y3 + ( a1 * k1 + a2 * k2 ) * h
= 0.438218 + 0.379181 * 0.4
= 0.58989
at x4 = 1.6
Step 5
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x4 , y4 )
= f( 1.6 , 0.58989 ) )
= 0.80225
k2 = f( x4 + p1 * h , y4 + q11 * k1 * h )
= f( 1.6 + 1 * 0.4 , 0.58989 + 1 * 0.80225 * 0.4)
= f( 2 , 0.91079 )
= 2.55021
at x5 = 2
********************************Results**********************************
Approximate = 1.26038
Exact = 1.30561
clc
clf
clear all
fcnstr='y*x^2-1.2*y' ;
f=inline(fcnstr) ;
x0=0 ;
y0=1 ;
xf=2 ;
a2=0.5 ;
n=5 ;
%**********************************************************************
xa(1)=x0 ;
ya(1)=y0 ;
disp(sprintf('\n\n***************************Simulation**********************
********'))
for i=1:n
disp(sprintf('\nStep %g',i))
disp(sprintf('-------------------------------------------------------------
----'))
% Calculating k1 and k2
k1 = f(xa(i),ya(i)) ;
k2 = f(xa(i)+p1*h,ya(i)+q11*k1*h) ;
end
disp(sprintf('\n\n********************************Results********************
**************'))
2. a boy stand still on a air-condition room with 15 degree Celsius, then the boy tries to change the
speed of the air condition to Xo=0, Xf=3 find the temperature rate when the boy lower the level
of the air condition the differential equation of the room temperature is.
𝑑𝑦
𝑑𝑥
= 𝑥𝑦 2 − 2.1𝑥 Find the temperature at n=5s
3 3 Step 5
K2=𝑓(𝑥1 + 4 𝑛, 𝑦1 + 4 𝑘1𝑛)
=f(1.2,0.277553) K1=f(x4,y4)
=-2.42756 =f(2.4,1.43519)
=-0.0965512
1 2
ynew=y+(3 𝑘1 + 3 𝑘2)𝑛 3 3
K2=𝑓(𝑥2 + 4 𝑛, 𝑦2 + 4 𝑘1𝑛)
y2=-0.18849
=f(3,1.37726)
Step 3 =-0.609471
K1=f(x2,y2)
=f(1.2,-0.18849)
1 2
=-2.47737 ynew=y+(3 𝑘1 + 3 𝑘2)𝑛
y5=1.22338
3 3
K2=𝑓(𝑥2 + 4
𝑛, 𝑦2 + 4
𝑘1𝑛)
=f(1.8,-1.67491) True error = exact-approximate
=1.26958
= -1.44906-1.22338
=-2.67244
1 2
ynew=y+( 𝑘1 + 𝑘2)𝑛
3 3
Matlab result
***************************Introduction*******************************
The 2nd Order Runge-Kutta method approximates the solution to an
ordinary differential equation by using the equation expressed in
the form dy/dx = f(x,y) to approximate the slope. This slope is used
to project the solution to the ODE a fixed distance away.
***************************Input Data*******************************
Below are the input parameters to begin the simulation which can be
changed in the m-file input section
f = dy/dx
x0 = initial x
y0 = initial y
xf = final x
a2 = constant value between 0 and 1.
= 0.5, Heun Method
= 1.0, Midpoint Method
= 0.66667, Ralston's Method
n = number of steps to take
-----------------------------------------------------------------
-----------------------------------------------------------------
For this simulation, the following parameter is constant.
h = ( xf - x0 ) / n
=(3-0)/5
= 0.6
a1 = 1 - a2
= 1 - 0.5
= 0.5
p1 = 1 / ( 2 * a2 )
= 1 / ( 2 * 0.5 )
=1
q11 = p1
=1
***************************Simulation******************************
Step 1
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x0 , y0 )
= f( 0 , 1 ) )
=0
k2 = f( x0 + p1 * h , y0 + q11 * k1 * h )
= f( 0 + 1 * 0.6 , 1 + 1 * 0 * 0.6)
= f( 0.6 , 1 )
= -0.66
at x1 = 0.6
Step 2
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x1 , y1 )
= f( 0.6 , 0.802 ) )
= -0.874078
k2 = f( x1 + p1 * h , y1 + q11 * k1 * h )
= f( 0.6 + 1 * 0.6 , 0.802 + 1 * -0.874078 * 0.6)
= f( 1.2 , 0.277553 )
= -2.42756
at x2 = 1.2
Step 3
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x2 , y2 )
= f( 1.2 , -0.18849 ) )
= -2.47737
k2 = f( x2 + p1 * h , y2 + q11 * k1 * h )
= f( 1.2 + 1 * 0.6 , -0.18849 + 1 * -2.47737 * 0.6)
= f( 1.8 , -1.67491 )
= 1.26958
at x3 = 1.8
Step 4
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x3 , y3 )
= f( 1.8 , -0.550826 ) )
= -3.23386
k2 = f( x3 + p1 * h , y3 + q11 * k1 * h )
= f( 1.8 + 1 * 0.6 , -0.550826 + 1 * -3.23386 * 0.6)
= f( 2.4 , -2.49114 )
= 9.85392
at x4 = 2.4
Step 5
-----------------------------------------------------------------
1) Find k1 and k2 using the previous step information.
k1 = f( x4 , y4 )
= f( 2.4 , 1.43519 ) )
= -0.0965512
k2 = f( x4 + p1 * h , y4 + q11 * k1 * h )
= f( 2.4 + 1 * 0.6 , 1.43519 + 1 * -0.0965512 * 0.6)
= f( 3 , 1.37726 )
= -0.609471
at x5 = 3
********************************Results**********************************
Approximate = 1.22338
Exact = -1.44906
Matlab codes
clc
clf
clear all
fcnstr='x*y^2-2.1*x' ;
f=inline(fcnstr) ;
x0=0 ;
y0=1 ;
xf=3 ;
a2=0.5 ;
n=5 ;
%**********************************************************************
xa(1)=x0 ;
ya(1)=y0 ;
disp(sprintf('\n\n***************************Simulation**********************
********'))
for i=1:n
disp(sprintf('\nStep %g',i))
disp(sprintf('-------------------------------------------------------------
----'))
% Calculating k1 and k2
k1 = f(xa(i),ya(i)) ;
k2 = f(xa(i)+p1*h,ya(i)+q11*k1*h) ;
end
disp(sprintf('\n\n********************************Results********************
**************'))