Вы находитесь на странице: 1из 14

PROJECT 1:

Introduction:

Project 1 deals with RK4, EULER METHOD and Improved Euler Method. These methods
would be solved by MATLAB.

RK4:

In numerical analysis, the Runge–Kutta methods are a family of implicit and explicit iterative
methods, which include the well-known routine called the Euler Method, used in temporal
discretization for the approximate solutions of ordinary differential equations.

Euler's method is a numerical method to solve first order first degree differential equation with
a given initial value.

In the Euler method, we will be given a differential equation which is the slope of a function,
and define a step size for the integral ( the smaller steps sizes you have, the more accurate
approximation values you will be get )

Discussion:

This is telling us that when we reduce the value h, it reduces the


error. For simple functions like the one we just tested, using this
Euler method can appear to be accurate especially when you
reduce h, but when it comes to complex systems, this may not be
the best numerical method to use to approximate the plot of
ODEs. Improved methods exist just like the famous Runge-Kutta
method.

he Euler method numerically gets the first-derivative correct. RK4 gets the first four
derivatives correct.
From the Taylor series, this means that RK4's first error term is from the 5th derivative
term, which from the Taylor series has a (x-x_0)^5. = h^5 or if you prefer dx^5. So it's
error is like the fifth power of h and is called 4th order. The Euler method is incorrect at
the second derivative, which from the Taylor series has a (x-x_0)^2 = h^2. This is why it
scales as h^2 and is called 1st order.
To understand what this really means, think about scaling your solution. You have a
solution at h, and now you re-solve at h/2. How much did you change your error? For
the Euler method your error is (h/2)^2 = h^2/4, so you have a quarter of the error from
before. For the RK4 method you have (h/2)^5 = h^2/32 and so you have 1/32 the error
from before! Thus you can see that as h gets smaller the higher order method gets
better and better.
Euler’s method is basically derived from Taylor’s Expansion of a function y
around t0.

As per differential equation, y’ = f( t, y). Substituting this in Taylor’s


Expansion and neglecting the terms with higher order (or power), we get:

y’(t0) = [y( t0 + h) – y(t0)]/ h

which is the forward finite difference formula of Euler’s method.Modified


Euler’s Method is a popular method of numerical analysis for integration of
initial value problem with the best accuracy and reliability. It solves ordinary
differential equations (ODE) by approximating in an interval with slope as an
arithmetic average. This method is a simple improvement on Euler’s
method in function evaluation per step but leads to yield a second order
method. Runge-Kutta method is a popular iteration method of approximating
solution of ordinary differential equations. Developed around 1900 by
German mathematicians C.Runge and M. W. Kutta, this method is applicable
to both families of explicit and implicit functions. Also known as RK method,
the Runge-Kutta method is based on solution procedure of initial value
problem in which the initial conditions are known. Based on the order of
differential equation, there are different Runge-Kutta methods which are
commonly referred to as: RK2, RK3, and RK4 methods.

To summarize, if h is the step size, then local truncation error Euler's method is h^2 while for RK, 4th
order it is h^5. The answer is essentially embedded in the formulation of the numerical schemes.
There are even higher order RK methods which can provide even more accurate solutions.

Problem 1:

Part A:

MATLAB CODE:

clear all;
close all;
clc;
syms t
y0 = -0.5;
f = @(t) ( 1 + (y0 - 1)*(exp(cos(t) - 1)) );
fplot(@(t) f(t));
xlabel('t')
ylabel('y')
axis([0 10 -1 1])
hold on;

h = 1.0/16.0;
y(1) = -0.5;
x = 0:h:20;
for n = 1:size(x,2)-1
y(n+1) = y(n) + h*( sin(x(n))*(1 - y(n)) );
end
plot(x,y,'r')
xlabel('t');
ylabel('y');

legend('Original Function', 'Euler Output')

figure
plot(x,log(abs(y - f(x))))
xlabel('x');
ylabel('log(error)');

Discussion:

a. T and y is a function of f.
b. Step size 1/16
c. Time 0<t<20
d. Original function is plot against x (time)
e. Y(n) is plot against x.

OUTPUT:
Part B:

MATLAB CODE:

%%%% Matlab code %%%%

clc;
clear all;
close all;
format long
f=@(t,y) sin(t) *(1-y)
f = function_handle with value:
@(t,y)sin(t)*(1-y)

h=1/16;
y_(1)= -1/2;

t1=0:h:20
t1 = 1×321
0 0.062500000000000 0.125000000000000 0.187500000000000
0.250000000000000 0.312500000000000 0.375000000000000 0.437500000000000
0.500000000000000 0.562500000000000 0.625000000000000 0.687500000000000
0.750000000000000 0.812500000000000 0.875000000000000 0.937500000000000
1.000000000000000 1.062500000000000 1.125000000000000 1.187500000000000
1.250000000000000 1.312500000000000 1.375000000000000 1.437500000000000
1.500000000000000 1.562500000000000 1.625000000000000 1.687500000000000
1.750000000000000 1.812500000000000 1.875000000000000 1.937500000000000
2.000000000000000 2.062500000000000 2.125000000000000 2.187500000000000
2.250000000000000 2.312500000000000 2.375000000000000 2.437500000000000
2.500000000000000 2.562500000000000 2.625000000000000 2.687500000000000
2.750000000000000 2.812500000000000 2.875000000000000 2.937500000000000
3.000000000000000 3.062500000000000
for n=1:length(t1)-1

%%%% Improved Eular method


yp=y_(n)+h*feval(f,t1(n),y_(n));
y_(n+1)=y_(n)+h/2*(feval(f,t1(n),y_(n))+feval(f,t1(n+1),yp));

end
plot(t1,y_);
legend('Improved Eular Method');
xlabel('t');
ylabel('y');

OUTPUT:
PART C:

MATLAB CODE:

%%%% Matlab code %%%%


clc;
clear all;
close all;
format long
f=@(t,y) sin(t) *(1-y)
f = function_handle with value:
@(t,y)sin(t)*(1-y)
h=1/16;
y(1)=-1/2;
y_(1)=-1/2;
t1=0:h:20
t1 = 1×321
0 0.062500000000000 0.125000000000000 0.187500000000000
0.250000000000000 0.312500000000000 0.375000000000000 0.437500000000000
0.500000000000000 0.562500000000000 0.625000000000000 0.687500000000000
0.750000000000000 0.812500000000000 0.875000000000000 0.937500000000000
1.000000000000000 1.062500000000000 1.125000000000000 1.187500000000000
1.250000000000000 1.312500000000000 1.375000000000000 1.437500000000000
1.500000000000000 1.562500000000000 1.625000000000000 1.687500000000000
1.750000000000000 1.812500000000000 1.875000000000000 1.937500000000000
2.000000000000000 2.062500000000000 2.125000000000000 2.187500000000000
2.250000000000000 2.312500000000000 2.375000000000000 2.437500000000000
2.500000000000000 2.562500000000000 2.625000000000000 2.687500000000000
2.750000000000000 2.812500000000000 2.875000000000000 2.937500000000000
3.000000000000000 3.062500000000000
for n=1:length(t1)-1
%%% RK-4 method
k1=h*feval(f,t1(n),y(n));
k2=h*feval(f,t1(n)+h/2,y(n)+k1/2);
k3=h*feval(f,t1(n)+h/2,y(n)+k2/2);
k4=h*feval(f,t1(n)+h,y(n)+k2);
y(n+1)=y(n)+(k1+2*k2+2*k3+k4)/6;
%%%% Improved Eular method
yp=y_(n)+h*feval(f,t1(n),y_(n));
y_(n+1)=y_(n)+h/2*(feval(f,t1(n),y_(n))+feval(f,t1(n+1),yp));
end
plot(t1,y,'* r')
hold on
plot(t1,y_,'-b');
legend('RK-4 Method','Improved Eular Method');
xlabel('t');
ylabel('y');

OUTPUT:
Problem 2:

Part A:

clc;
clear all;
close all;
format compact

f=@(t,y) y-y^2+1.14*cos(exp(t/2));
y(1)=1;

h=[1/8,1/16,1/32,1/64];
for i=1:length(h)
t1=0:h(i):20;
for n=1:length(t1)-1

%%%% Improved Eular method


yp=y(n)+h(i)*feval(f,t1(n),y(n));
y(n+1)=y(n)+h(i)/2*(feval(f,t1(n),y(n))+feval(f,t1(n+1),yp));

end
plot(t1,y)
hold on
end
legend('h=1/8','h=1/16','h=1/32','h=1/64')
xlabel('t');
ylabel('y');
OUTPUT:
Part B:

MATLAB CODE:

%%% Matlab code %%%

clc;
clear all;
close all;
format compact

f=@(t,y) y-y^2+1.14*cos(exp(t/2));
y(1)=1;

h=[1/8,1/16,1/32,1/64];
for i=1:length(h)
t1=0:h(i):20;
for n=1:length(t1)-1
k1=h(i)*feval(f,t1(n),y(n));
k2=h(i)*feval(f,t1(n)+h(i)/2,y(n)+k1/2);
k3=h(i)*feval(f,t1(n)+h(i)/2,y(n)+k2/2);
k4=h(i)*feval(f,t1(n)+h(i),y(n)+k2);
y(n+1)=y(n)+(k1+2*k2+2*k3+k4)/6;

end
plot(t1,y)
hold on
end
legend('h=1/8','h=1/16','h=1/32','h=1/64')
xlabel('t');
ylabel('y');
OUTPUT:
The purpose of this question is approximate the solution to the IVP using the RK4 method with the
following conditions: Initial condition �0=1; time steps ℎ=1/8,1/16,1/32,1/64;

The Runge-Kutta methods are a family of iterative methods for the approximation of solutions
of ODE. There are many formulations of Runge-Kutta methods, as well as 2 nd, 3rd, and 4th order
methods.

The local truncation error of this Runge-Kutta Method goes as h4; the global truncation error
goes as h3. This Runge-Kutta Method is third order.

For on the ODE dydx=y with the initial condition y(0)=1, apply the 3rd-order Runge-Kutta
algorithm to approximate the solution from x=0 to x=1. Let h=0.2. Since the exact solution
is y=ex, determine the relative accuracy.

Here Yn+1 is the RK4 approximation of ytn+1, and the next value ( yn+1) is determined by the
present value (yn) 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 increment based on the slope at the beginning of the interval, using y (Euler's
method);

 k2 is the increment based on the slope at the midpoint of the interval, using y and k1;

 k3 is again the increment based on the slope at the midpoint, but now using y and k2;

 k4 is the increment based on the slope at the end of the interval, using y and k3.

The Improved Euler method adapts the Euler's method by using the Euler Method result as a
predictor, and then averaging with a corrector that estimates the derivative at the end point of the
step interval: Since yn+1 also appears on the right side, it can be replaced by Euler's formula, and
the Improved Euler method. The local truncation error of this method goes a h3; the global
truncation error goes as h2. The Improved Euler Method is of second order.

For on the ODE dydx=y with the initial condition y(0)=1, apply the Improved Euler (Heun)
method to approximate the solution from x=0 to x=1. Let h=0.2. Since the exact solution is y=ex,
determine the relative accuracy.

The Euler-Heun algorithm is: yn+1=yn+h2[F(xn,yn)+F(xn+1,yn+hF(xn,yn))].

For this problem: F(x,y)=y, with y(x0=0)=1=y0;


h=1−05=0.2, so that xn=x0+0.2n=0.2n for n=0,1,…5.

The Euler improved algorithm to use is: yn+1=yn+h2[yn+yn+hyn]=[1+h+h22]yn

yn+1=1.22 yn

SUMMARY:

 Usually error in Euler method is higher than higher order RK method (RK2,
RK3, etc.), because truncation error in higher order methods is less
compared to Euler method.
 In some of the beginner level literature in numerical methods, it is loosely
mentioned that higher order methods (say, RK4) give less error than lower
order method (say, Euler method). Most of the time this is true, but not all
the time. This property depends on the mesh and initial condition and
differential equations you have considered.
 If the exact solution to the differential equation is a polynomial of
order nn, it will be solved exactly by an nn-th Runge-Kutta method. For
example, forward Euler will be exact if the solution is a line. RK4 will be
exact if the solution is a polynomial of degree 4 or less.

Вам также может понравиться