You are on page 1of 15

Assessment No : 09 Name:

DEGAPUDI
PRABHADITYA
Date : 09/05/2021 Registration Number: 18BCM0078

AIM:
To develop a MATLAB code to solve the given ordinary differential equation (ODE) using
Runge-Kutta (R-K) 4th order method and compare it with Euler, Modified Euler methods and
analytical solution represents the results using the graph.
The velocity of a falling parachutist is given by the following equation:

dv cv
=g−
dt m
where, v is velocity (m/s), t is time (s), g is the acceleration due to gravity (9.81 m/s2), c is the
drag coefficient (kg/s), and m is mass (kg). Take c = 12.5 kg/s and m = 68 kg.
Initial Condition: At t = 0 s; v = 0 m/s. ie., v(0) = 0.

Analytical Solution is given by the following equation:

v(t) =
gm
(1− exp− (c/ m)t )
c
Plot graph v vs. t for all the above 2 methods with varying the step size h=2, h=4, h=6, h= 8,
h=10. Time starts from 0 s to 60 s.

Required formula:

𝑦𝑖+1 = 𝑦𝑖 + 𝑓(𝑥𝑖, 𝑦𝑖) × ℎ → Euler Method


1
𝑦𝑖+1 = 𝑦𝑖 + [𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4] × ℎ → R-K Method
6 1 1
𝑘1 = 𝑓(𝑥𝑖, 𝑦𝑖)1 𝑘2 = 𝑓(𝑥𝑖 + ℎ, 𝑦𝑖 + 𝑘1ℎ)
𝑘 = 𝑓(𝑥 + ℎ 1 ℎ) 2 𝑘 = 𝑓(𝑥2 + ℎ, 𝑦 + 𝑘 ℎ)
3 𝑖
2
, 𝑦 + 𝑘
𝑖 2 2 4 𝑖 𝑖 3

h
y =y+ ( f (t , y ) + f (t , y + hf (t , y ) ) → Modified Euler's Method
i+1 i i i i+1 i i i
2
THEORY:

Euler’s Method :

Euler method (also called forward Euler method) is a first-order numerical procedure
for solving ordinary differential equations (ODEs) with a given initial value. The Euler
method is a first-order method, which means that the local error (error per step) is
proportional to the square of the step size, and the global error (error at a given time) is
proportional to the step size. Consider the problem of calculating the shape of an unknown
curve which starts at a given point and satisfies a given differential equation. Here, a
differential equation can be thought of as a formula by which the slope of the tangent line to
the curve can be computed at any point on the curve, once the position of that point has been
calculated. The idea is that while the curve is initially unknown, its starting point, which we
denote by A0. Then, from the differential equation, the slope to the curve at A0 can be
computed, and so, the tangent line. Take a small step along that tangent line up to a point A1.
Along this small step, the slope does not change too much, so A1 will be close to the curve. If
we pretend that A1 is still on the curve, the same reasoning as for the point A0 above can be
used. After several steps, a polygonal curve A0 , A1 , A2 , A3 … is computed. In general, this
curve does not diverge too far from the original unknown curve, and the error between the
two curves can be made small if the step size is small enough and the interval of computation
is finite.

Formula :

𝑦𝑖+1 = 𝑦𝑖 + 𝑓(𝑡𝑖, 𝑦𝑖) × ℎ

Modified Euler’s Method :

The Euler forward scheme may be very easy to implement but it can't give accurate
solutions. A very small step size is required for any meaningful result. In this scheme,
since, the starting point of each sub-interval is used to find the slope of the solution
curve, the solution would be correct only if the function is linear. So an improvement over
this is to take the arithmetic average of the slopes at xi and xi+1(that is, at the end points of
each sub-interval). The scheme so obtained is called modified Euler's method. It works first
by approximating a value to yi+1 and then improving it by making use of average slope.

Formula:


𝑦𝑖+1 = 𝑦𝑖 + (𝑓(𝑡𝑖, 𝑦𝑖) + 𝑓(𝑡𝑖+1, 𝑦𝑖 + ℎ𝑓(𝑡𝑖, 𝑦𝑖) )
2
Runge – Kutta 4th Order Method :

Let an initial value problem be specified as follows:

Here y is an unknown function (scalar or vector) of time t, which we would like to


approximate; we are told that dy/dt , the rate at which y changes, is a function of t and of y
itself. At the initial time t0 the corresponding y value is y0. The function f and the initial
conditions t0,y0 are given. Now pick a step-size h > 0 and define the next iteration value
.Here yi+1 is the RK4 approximation of y(ti+1) and the next value yi+1 is determined by the
present value yi plus the weighted average of four increments, where each increment is the
product of the size of the interval, h, and an estimated slope specified by function f on the
right-hand side of the differential equation. k1 is the slope at the beginning of the interval,
using y (Euler's method) . k2 is the slope at the midpoint of the interval, using y and k1;
k3 is again the slope at the midpoint, but now using y and k2. k4 is the slope at the end of the
interval, using y and k3. In averaging the four slopes, greater weight is given to the slopes at
the midpoint. If f is independent of y, so that the differential equation is equivalent to a
simple integral, then RK4 is Simpson's rule. The RK4 method is a fourth-order method,
meaning that the local truncation error is on the order of O(h5) while the total accumulated
error is on the order of O(h4) .

Formula :
1
𝑦𝑖+1 = 𝑦𝑖 + [𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4] × ℎ
6
𝑘1 = 𝑓(𝑥𝑖, 𝑦𝑖)
1 1
𝑘2 = 𝑓(𝑥𝑖 + ℎ, 𝑦𝑖 + 𝑘1ℎ)
2 2
1 1
𝑘3 = 𝑓 (𝑥𝑖 + ℎ, 𝑦𝑖 + 𝑘2ℎ)
2 2

𝑘4 = 𝑓(𝑥𝑖 + ℎ, 𝑦𝑖 + 𝑘3ℎ)
RESULTS AND DISCUSSIONS:

MATLAB Code :

clear all
clc
f=@(x,y) 10- ((12.5*y)/68);
x0=input('Enter the initial value of x: ');
y0=input('Enter the initial value of y: ');
xn=input('Enter the final value of x: ');
h=input('Enter the step value: ');
y1(1)=y0;
y2(1)=y0;
y3(1)=y0;
y4(1)=y0;
ti(1)=x0;
for i=1:100
ti(i+1)=ti(i)+h;
k_1 = f(ti(i),y3(i));
k_2 = f(ti(i)+0.5*h,y3(i)+0.5*h*k_1);
k_3 = f((ti(i)+0.5*h),(y3(i)+0.5*h*k_2));
k_4 = f((ti(i)+h),(y3(i)+k_3*h));
y1(i+1)= y1(i)+ (f(ti(i),y1(i))*h);
y2(i+1)=
y2(i)+(h/2)*(f(ti(i),y2(i))+f(ti(i),y2(i)+h*f(ti(i),y2(i)
)));
y3(i+1)= y3(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h;
y4(i+1)=((10*68)/(12.5))*(1-exp(-(12.5/68)*ti(i+1)));
if ti(i)>=xn
n=i;
break;
end
end
fprintf('\n Iteration time(s) velocity(m/s)
');
for i=1:n
fprintf('\n %d %4.3f %4.3f
',i,ti(i),y1(i));
fprintf('\n %d %4.3f %4.3f
',i,ti(i),y2(i));
fprintf('\n %d %4.3f %4.3f
',i,ti(i),y3(i));
fprintf('\n %d %4.3f %4.3f
',i,ti(i),y4(i));
end
fprintf('\n');
plot(ti,y1);
hold on;
plot(ti,y2);
hold on;
plot(ti,y3);
hold on;
plot(ti,y4);
xlabel('Time');
ylabel('Velocity');
title('Velocity of a parachutist');
legend('Euler Method','Modified Euler Method','RK 4th
Order Method','Analytical Method');

Output :

1. For Step-Size h = 2 :

Enter the initial value of x: 0


Enter the initial value of y: 0
Enter the final value of x: 60
Enter the step value: 2

Iteration time(s) velocity(m/s)


1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
2 2.000 20.000
2 2.000 16.324
2 2.000 16.733
2 2.000 16.736
3 4.000 32.647
3 4.000 27.749
3 4.000 28.319
3 4.000 28.323
4 6.000 40.644
4 6.000 35.746
4 6.000 36.341
4 6.000 36.345
5 8.000 45.702
5 8.000 41.343
5 8.000 41.896
5 8.000 41.899
6 10.000 48.900
6 10.000 45.261
6 10.000 45.742
6 10.000 45.745
7 12.000 50.922
7 12.000 48.003
7 12.000 48.405
7 12.000 48.408
8 14.000 52.201
8 14.000 49.923
8 14.000 50.249
8 14.000 50.251
9 16.000 53.009
9 16.000 51.266
9 16.000 51.526
9 16.000 51.527
10 18.000 53.521
10 18.000 52.207
10 18.000 52.410
10 18.000 52.411
11 20.000 53.844
11 20.000 52.865
11 20.000 53.022
11 20.000 53.023
12 22.000 54.048
12 22.000 53.325
12 22.000 53.446
12 22.000 53.447
13 24.000 54.178
13 24.000 53.648
13 24.000 53.739
13 24.000 53.740
14 26.000 54.259
14 26.000 53.874
14 26.000 53.943
14 26.000 53.943
15 28.000 54.311
15 28.000 54.032
15 28.000 54.083
15 28.000 54.084
16 30.000 54.344
16 30.000 54.142
16 30.000 54.181
16 30.000 54.181
17 32.000 54.364
17 32.000 54.219
17 32.000 54.248
17 32.000 54.248
18 34.000 54.378
18 34.000 54.274
18 34.000 54.295
18 34.000 54.295
19 36.000 54.386
19 36.000 54.312
19 36.000 54.327
19 36.000 54.327
20 38.000 54.391
20 38.000 54.338
20 38.000 54.350
20 38.000 54.350
21 40.000 54.394
21 40.000 54.357
21 40.000 54.365
21 40.000 54.365
22 42.000 54.396
22 42.000 54.370
22 42.000 54.376
22 42.000 54.376
23 44.000 54.398
23 44.000 54.379
23 44.000 54.383
23 44.000 54.383
24 46.000 54.399
24 46.000 54.385
24 46.000 54.388
24 46.000 54.388
25 48.000 54.399
25 48.000 54.390
25 48.000 54.392
25 48.000 54.392
26 50.000 54.399
26 50.000 54.393
26 50.000 54.394
26 50.000 54.394
27 52.000 54.400
27 52.000 54.395
27 52.000 54.396
27 52.000 54.396
28 54.000 54.400
28 54.000 54.396
28 54.000 54.397
28 54.000 54.397
29 56.000 54.400
29 56.000 54.398
29 56.000 54.398
29 56.000 54.398
30 58.000 54.400
30 58.000 54.398
30 58.000 54.399
30 58.000 54.399
31 60.000 54.400
31 60.000 54.399
31 60.000 54.399
31 60.000 54.399

2. For Step-Size h = 4 :

Enter the initial value of x: 0


Enter the initial value of y: 0
Enter the final value of x: 60
Enter the step value: 4

Iteration time(s) velocity(m/s)


1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
2 4.000 40.000
2 4.000 25.294
2 4.000 28.236
2 4.000 28.323
3 8.000 50.588
3 8.000 38.827
3 8.000 41.816
3 8.000 41.899
4 12.000 53.391
4 12.000 46.068
4 12.000 48.348
4 12.000 48.408
5 16.000 54.133
5 16.000 49.942
5 16.000 51.489
5 16.000 51.527
6 20.000 54.329
6 20.000 52.015
6 20.000 53.000
6 20.000 53.023
7 24.000 54.381
7 24.000 53.124
7 24.000 53.727
7 24.000 53.740
8 28.000 54.395
8 28.000 53.717
8 28.000 54.076
8 28.000 54.084
9 32.000 54.399
9 32.000 54.035
9 32.000 54.244
9 32.000 54.248
10 36.000 54.400
10 36.000 54.205
10 36.000 54.325
10 36.000 54.327
11 40.000 54.400
11 40.000 54.295
11 40.000 54.364
11 40.000 54.365
12 44.000 54.400
12 44.000 54.344
12 44.000 54.383
12 44.000 54.383
13 48.000 54.400
13 48.000 54.370
13 48.000 54.392
13 48.000 54.392
14 52.000 54.400
14 52.000 54.384
14 52.000 54.396
14 52.000 54.396
15 56.000 54.400
15 56.000 54.391
15 56.000 54.398
15 56.000 54.398
16 60.000 54.400
16 60.000 54.395
16 60.000 54.399
16 60.000 54.399

3. For Step-Size h = 6 :

Enter the initial value of x: 0


Enter the initial value of y: 0
Enter the final value of x: 60
Enter the step value: 6

Iteration time(s) velocity(m/s)


1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
2 6.000 60.000
2 6.000 26.912
2 6.000 35.722
2 6.000 36.345
3 12.000 53.824
3 12.000 40.510
3 12.000 47.987
3 12.000 48.408
4 18.000 54.459
4 18.000 47.382
4 18.000 52.198
4 18.000 52.411
5 24.000 54.394
5 24.000 50.854
5 24.000 53.644
5 24.000 53.740
6 30.000 54.401
6 30.000 52.608
6 30.000 54.140
6 30.000 54.181
7 36.000 54.400
7 36.000 53.495
7 36.000 54.311
7 36.000 54.327
8 42.000 54.400
8 42.000 53.942
8 42.000 54.369
8 42.000 54.376
9 48.000 54.400
9 48.000 54.169
9 48.000 54.389
9 48.000 54.392
10 54.000 54.400
10 54.000 54.283
10 54.000 54.396
10 54.000 54.397
11 60.000 54.400
11 60.000 54.341
11 60.000 54.399
11 60.000 54.399
4. For Step-Size h = 8 :

Enter the initial value of x: 0


Enter the initial value of y: 0
Enter the final value of x: 60
Enter the step value: 8

Iteration time(s) velocity(m/s)


1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
2 8.000 80.000
2 8.000 21.176
2 8.000 39.410
2 8.000 41.899
3 16.000 42.353
3 16.000 34.110
3 16.000 50.270
3 16.000 51.527
4 24.000 60.069
4 24.000 42.008
4 24.000 53.262
4 24.000 53.740
5 32.000 51.732
5 32.000 46.832
5 32.000 54.086
5 32.000 54.248
6 40.000 55.655
6 40.000 49.778
6 40.000 54.314
6 40.000 54.365
7 48.000 53.809
7 48.000 51.577
7 48.000 54.376
7 48.000 54.392
8 56.000 54.678
8 56.000 52.676
8 56.000 54.393
8 56.000 54.398
9 64.000 54.269
9 64.000 53.347
9 64.000 54.398
9 64.000 54.400
5. For Step-Size h = 10 :

Enter the initial value of x: 0


Enter the initial value of y: 0
Enter the final value of x: 60
Enter the step value: 10

Iteration time(s) velocity(m/s)


1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
1 0.000 0.000
2 10.000 100.000
2 10.000 8.088
2 10.000 38.525
2 10.000 45.745
3 20.000 16.176
3 20.000 14.974
3 20.000 49.767
3 20.000 53.023
4 30.000 86.440
4 30.000 20.836
4 30.000 53.048
4 30.000 54.181
5 40.000 27.543
5 40.000 25.826
5 40.000 54.005
5 40.000 54.365
6 50.000 76.913
6 50.000 30.075
6 50.000 54.285
6 50.000 54.394
7 60.000 35.529
7 60.000 33.691
7 60.000 54.366
7 60.000 54.399

You might also like