0% found this document useful (0 votes)
72 views13 pages

Matass: 1. Question 1 (Comparison of All Methods With Three Step Sizes)

The document presents the results of using various numerical integration methods to solve initial value problems (IVPs) including Euler's method, Runge-Kutta methods of order 2 and 4, and midpoint and Ralston's methods. It solves 4 test problems and records the maximum error and percentage error for each method across 3 different step sizes (h=0.1, 0.01, 0.001). For each problem and step size, it runs the different methods and plots the numerical and exact solutions, outputting the error analysis.

Uploaded by

Harsh Darji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views13 pages

Matass: 1. Question 1 (Comparison of All Methods With Three Step Sizes)

The document presents the results of using various numerical integration methods to solve initial value problems (IVPs) including Euler's method, Runge-Kutta methods of order 2 and 4, and midpoint and Ralston's methods. It solves 4 test problems and records the maximum error and percentage error for each method across 3 different step sizes (h=0.1, 0.01, 0.001). For each problem and step size, it runs the different methods and plots the numerical and exact solutions, outputting the error analysis.

Uploaded by

Harsh Darji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

MatAss

1. Question 1 (Comparison of all methods with three step sizes)

clear all
clc

x0 = 0;
y0 = 2;
xEnd = 5;
for h = [0.1,0.01,0.001]
%% Initialization of Solution
N = (xEnd - x0)./h;
X = (x0:h:xEnd)';
Y = zeros(N+1,1);
Y(1) = y0;
%% Solving by Huens Method's

%% Solution of IVP using R-K Second Order Method


for i = 1:N
k1 = 4*exp(0.8*X(i)) - 0.5*Y(i);
xNew = X(i) + h;
yNew = Y(i) + k1.*h;
k2 = 4*exp(0.8*xNew) - 0.5*yNew;
Y(i+1) = Y(i) + (((k1 + k2).*h)/2);
end

%% Plot and Error Analysis


Ytrue = ((3.07692.*exp(1.3.*X) - 1.07692).*(exp(-0.5.*X)));
Err= abs(Ytrue-Y);
plot(X,Y); hold on
Max_Error_Huen = max(Err)
PErr = ((max(Err)/Ytrue)*100);
Percentage_Error_Huen = max(PErr)

%% Solving by Midpoint Method

%% Solution of IVP using R-K Second Order Method


for i = 1:N
k3 = 4*exp(0.8*X(i)) - 0.5*Y(i);
xNew1 = X(i) + h./2;
yNew1 = Y(i) + k3.*(h/2);
k4 = 4*exp(0.8*xNew1) - 0.5*yNew1;
Y(i+1) = Y(i) + (k4).*h;
end
%% Plot and Error Analysis
Err1= abs(Ytrue-Y);
plot(X,Y);hold on
Max_Error_Midpoint = max(Err1)
PErr1 = (max(Err1)/Ytrue*100);
Percentage_Error_Midpoint = max(PErr1)

%% Solving by Ralston's Mehod

%% Solution IVP using R-K Second Order Method


for i = 1:N
k5 = 4*exp(0.8*X(i))-0.5*Y(i);
xnew2 = X(i) + ((3 .*h)/4);
ynew2 = Y(i) + ((3*k5.*h)/4);
k6 = 4*exp(0.8*xnew2)-0.5*ynew2;
Y(i+1) = Y(i) + (k5+2*k6).*(h/3);
end

%% Plotting and Error analysis


yTrue = (exp(-0.5.*X)).*(3.07692.*exp(1.3.*X)-1.07692);
Err2 = abs(yTrue-Y);
plot(X,Y);hold on
Max_Error_Ralston = max(Err2)
PErr2 = (max(Err2)/yTrue*100);
Percentage_Error_Ralston = max(PErr2)

%% Solving by Explicit Method

%% Solution of Part of IVP


for i = 1:N
fi = 4*exp(0.8*X(i))-0.5*Y(i);
Y(i+1) = Y(i) + h.*fi;
end

%% Plotting for solution of IVP


Ytrue =(exp(-0.5.*X)).*(3.07692.*exp(1.3.*X)-1.07692);
Err3 = abs(Ytrue - Y);
plot(X,Y);hold on
Max_Error_Explicit = max(Err3)
PErr3 = max(Err3)/Ytrue*100;
Percentage_Error_Explicit = max(PErr3)
%% Solving by Kutta's method

%% Solving IVP by R-K fourth order


for i = 1:N
f = 4*exp(0.8*X(i))-0.5*Y(i);
k7 = f;
k8 = f+(k7*h/3);
k9 = f+(k8*2*h/3);
k10 = f+(k9*h);
Y(i+1) = Y(i) + (k7+(3*k8)+(3*k9)+k10)*h/8;
end

%% Plotting and Error analysis


Ytrue = exp(-0.5.*X).*(3.07692.*(exp(1.3.*X))-1.07692);
Err4 = abs(Ytrue-Y);
plot(X,Y)
Max_Error_Kutta = max(Err4)
PErr4 = (max(Err4)/Ytrue*100);
Percentage_Error_Kutta = max(PErr4)

%% Solving by Runge's Method

%% Solving IVP by R-K fourth order


for i = 1:N
f2 = 4*exp(0.8*X(i))-0.5*Y(i);
k11 = f2;
k12 = f2+(k11*h/2);
k13 = f2+(k12*h/2);
k14 = f2+(k3*h);
Y(i+1) = Y(i) + (k11+(2*k12)+(2*k13)+k14)*h/6;
end

%% Plotting and Error analysis for runge’s Method


Ytrue = exp(-0.5.*X).*(3.07692.*(exp(1.3.*X))-1.07692);
Err5 = abs(Ytrue-Y);
plot(X,Y)
Max_Error_Runge = max(Err5)
PErr5 = (max(Err5)/Ytrue)*100;
Percentage_Error_Runge = max(PErr5)

end
plot(X,Ytrue,"-")
OUTPUT
Max_Error_Huen = 0.1609
Percentage_Error_Huen = 0.0958
Max_Error_Midpoint = 0.0234
Percentage_Error_Midpoint = 0.0139
Max_Error_Ralston = 0.0917
Percentage_Error_Ralston = 0.0546
Max_Error_Explicit = 4.1315
Percentage_Error_Explicit = 2.4606
Max_Error_Kutta = 1.0641
Percentage_Error_Kutta = 0.6337
Max_Error_Runge = 3.2305
Percentage_Error_Runge = 1.9240
Max_Error_Huen = 0.0018
Percentage_Error_Huen = 0.0010
Max_Error_Midpoint = 4.0690e-04
Percentage_Error_Midpoint = 2.4234e-04
Max_Error_Ralston = 0.0011
Percentage_Error_Ralston = 6.4268e-04
Max_Error_Explicit = 0.4123
Percentage_Error_Explicit = 0.2455
Max_Error_Kutta = 0.1052
Percentage_Error_Kutta = 0.0627
Max_Error_Runge = 0.3551
Percentage_Error_Runge = 0.2115
Max_Error_Huen = 1.8356e-04
Percentage_Error_Huen = 1.0933e-04
Max_Error_Midpoint = 1.7014e-04
Percentage_Error_Midpoint = 1.0133e-04
Max_Error_Ralston = 1.7685e-04
Percentage_Error_Ralston = 1.0533e-04
Max_Error_Explicit = 0.0411
Percentage_Error_Explicit = 0.0245
Max_Error_Kutta = 0.0107
Percentage_Error_Kutta = 0.0063
Max_Error_Runge = 0.0359
Percentage_Error_Runge = 0.0214
2. Question 2 (R-k second order with three step sizes)

clear all

clc

x0 = 0;
y0 = 1;
xEnd = 5;
for h = [0.1,0.01,0.001]

%% Initialization of Solution
N = (xEnd - x0)./h;
X = (x0:h:xEnd)';
Y = zeros(N+1,1);
Y(1) = y0;

%% Solving by Huens Method's

%% Solution of IVP using R-K Second Order Method


for i = 1:N
k1 = (-2*(X(i).^3))+(12.*(X(i).^2))-(20.*X(i))+8.5;
xNew = X(i) + h;
yNew = Y(i) + k1.*h;
k2 = (-2*(xNew.^3))+(12*(xNew.^2))-(20.*xNew)+8.5;
Y(i+1) = Y(i) + (((k1 + k2).*h)/2);
end

%% Plot and Error Analysis


Ytrue = (-(X.^4)./2)+(4.*(X.^3))-(10.*(X.^2))+(8.5.*X)+1;
Err= abs(Ytrue-Y);
plot(X,Y); hold on
Max_Error_Huen = max(Err)
PErr = abs((max(Err)/Ytrue)*100);
Percentage_Error_Huen = max(PErr)

%% Solving by Midpoint Method

%% Solution of IVP using R-K Second Order Method


for i = 1:N
k3 = (-2*(X(i).^3))+(12.*(X(i).^2))-(20.*X(i))+8.5;
xNew1 = X(i) + h./2;
yNew1 = Y(i) + k3.*(h/2);
k4 = (-2*(xNew1.^3))+(12*(xNew1.^2))-(20*xNew1)+8.5;
Y(i+1) = Y(i) + (k4).*h;
end
%% Plot and Error Analysis
Ytrue = (-(X.^4)./2)+(4.*(X.^3))-(10.*(X.^2))+(8.5.*X)+1;
Err1= abs(Ytrue-Y);
plot(X,Y);hold on
Max_Error_Midpoint = max(Err1)
PErr1 = abs(max(Err1)/Ytrue*100);
Percentage_Error_Midpoint = max(PErr1)

%% Solving by Ralston's Mehod

%% Solution IVP using R-K Second Order Method


for i = 1:N
k5 = (-2*(X(i).^3))+(12*(X(i).^2))-(20*X(i))+8.5;
xNew2 = X(i) + ((3 .*h)/4);
yNew2 = Y(i) + ((3*k5.*h)/4);
k6 = (-2*(xNew2.^3))+(12*(xNew2.^2))-(20*xNew2)+8.5;
Y(i+1) = Y(i) + (k5+2*k6).*(h/3);
end

%% Plotting and Error analysis


Ytrue = (-(X.^4)./2)+(4.*(X.^3))-(10.*(X.^2))+(8.5.*X)+1;
Err2 = abs(Ytrue-Y);
plot(X,Y);hold on
Max_Error_Ralston = max(Err2)
PErr2 = abs(max(Err2)/Ytrue*100);
Percentage_Error_Ralston = max(PErr2)

end
plot(X,Ytrue,"-")

OUTPUT
Max_Error_Huen = 0.0250
Percentage_Error_Huen = 0.1316
Max_Error_Midpoint = 0.0125
Percentage_Error_Midpoint = 0.0658
Max_Error_Ralston = 0.0059
Percentage_Error_Ralston = 0.0312
Max_Error_Huen = 2.5000e-04
Percentage_Error_Huen = 0.0013
Max_Error_Midpoint = 1.2500e-04
Percentage_Error_Midpoint = 6.5789e-04
Max_Error_Ralston = 6.2188e-05
Percentage_Error_Ralston = 3.2730e-04
Max_Error_Huen = 2.5000e-06
Percentage_Error_Huen = 1.3158e-05
Max_Error_Midpoint = 1.2500e-06
Percentage_Error_Midpoint = 6.5789e-06
Max_Error_Ralston = 6.2469e-07
Percentage_Error_Ralston = 3.2878e-06

3. Question 3(R-k second order with three step sizes )

 
clear all
clc

x0 = 0;
y0 = 1;
xEnd = 5;
for h = [0.1,0.01,0.001]

%% Initialization of Solution
N = (xEnd - x0)./h;
X = (x0:h:xEnd)';
Y = zeros(N+1,1);
Y(1) = y0;
%% Solving by Huens Method's

%% Solution of IVP using R-K Second Order Method


for i = 1:N
k1 = Y(i)*(sin(X(i)))^3;
xNew = X(i) + h;
yNew = Y(i) + k1.*h;
k2 = yNew*(sin(xNew))^3;
Y(i+1) = Y(i) + (((k1 + k2).*h)/2);
end
%% Plot and Error Analysis
Ytrue = exp((2/3)+(cos(3.*X)./12)-3.*(cos(X)./4));
Err= abs(Ytrue-Y);
plot(X,Y); hold on
Max_Error_Huen = max(Err)
PErr = ((max(Err)/Ytrue)*100);
Percentage_Error_Huen = max(PErr)

%% Solving by Midpoint Method

%% Solution of IVP using R-K Second Order Method


for i = 1:N
k3 = Y(i)*(sin(X(i)))^3;
xNew1 = X(i) + h./2;
yNew1 = Y(i) + k3.*(h/2);
k4 = yNew1*(sin(xNew1))^3;
Y(i+1) = Y(i) + (k4).*h;
end

%% Plot and Error Analysis


Ytrue = exp((2/3)+(cos(3.*X)./12)-3.*(cos(X)./4));
Err1= abs(Ytrue-Y);
plot(X,Y);hold on
Max_Error_Midpoint = max(Err1)
PErr1 = (max(Err1)/Ytrue*100);
Percentage_Error_Midpoint = max(PErr1)

%% Solving by Ralston's Mehod

%% Solution IVP using R-K Second Order Method


for i = 1:N
k5 = Y(i)*(sin(X(i)))^3;
xnew2 = X(i) + ((3 .*h)/4);
ynew2 = Y(i) + ((3*k5.*h)/4);
k6 = ynew2*(sin(xnew2))^3;
Y(i+1) = Y(i) + (k5+2*k6).*(h/3);
end

%% Plotting and Error analysis


Ytrue = exp((2/3)+(cos(3.*X)./12)-3.*(cos(X)./4));
Err2 = abs(Ytrue-Y);
plot(X,Y);hold on
Max_Error_Ralston = max(Err2)
PErr2 = (max(Err2)/Ytrue*100);
Percentage_Error_Ralston = max(PErr2)

end
plot(X,Ytrue,"-")
OUTPUT
Max_Error_Huen = 0.0083
Percentage_Error_Huen = 0.2192
Max_Error_Midpoint = 0.0052
Percentage_Error_Midpoint = 0.1367
Max_Error_Ralston = 0.0060
Percentage_Error_Ralston = 0.1571
Max_Error_Huen = 7.8921e-05
Percentage_Error_Huen = 0.0021
Max_Error_Midpoint = 5.1465e-05
Percentage_Error_Midpoint = 0.0014
Max_Error_Ralston = 5.8040e-05
Percentage_Error_Ralston = 0.0015
Max_Error_Huen = 7.8409e-07
Percentage_Error_Huen = 2.0668e-05
Max_Error_Midpoint = 5.1393e-07
Percentage_Error_Midpoint = 1.3547e-05
Max_Error_Ralston = 5.7850e-07
Percentage_Error_Ralston = 1.5249e-05

4. Question 4(R-k fourth order with three step sizes )

clear all
clc

x0 = 0;
y0 = 1;
xEnd = 5;
for h = [0.1,0.01,0.001]
%% Initialization of Solution
N = (xEnd - x0)./h;
X = (x0:h:xEnd)';
Y = zeros(N+1,1);
Y(1) = y0;

%% Solving by Kutta's method

%% Solving IVP by R-K fourth order


for i = 1:N
f = (-2*Y(i))+(X(i)^2);
k7 = f;
k8 = f+(k7*h/3);
k9 = f+(k8*2*h/3);
k10 = f+(k9*h);
Y(i+1) = Y(i) + (k7+(3*k8)+(3*k9)+k10)*h/8;
end

%% Plotting and Error analysis


Ytrue = (((2*(X.^2))-(2*X)+1)/4)+(3*(exp(-2*X))/4);
Err4 = abs(Ytrue-Y);
plot(X,Y); hold on
Max_Error_Kutta = max(Err4)
PErr4 = (max(Err4)/Ytrue*100);
Percentage_Error_Kutta = max(PErr4)

%% Solving by Runge's Method

%% Solving IVP by R-K fourth order


for i = 1:N
f2 = (-2*Y(i))+(X(i)^2);
k11 = f2;
k12 = f2+(k11*h/2);
k13 = f2+(k12*h/2);
k14 = f2+(k13*h);
Y(i+1) = Y(i) + (k11+(2*k12)+(2*k13)+k14)*h/6;
end

%% Plotting and Error analysis for runge’s Method


Err5 = abs(Ytrue-Y);
plot(X,Y)
Max_Error_Runge = max(Err5)
PErr5 = (max(Err5)/Ytrue)*100;
Percentage_Error_Runge = max(PErr5)

end
plot(X,Ytrue,"-")
hold off
OUTPUT

Max_Error_Kutta = 0.0751
Percentage_Error_Kutta = 0.7328
Max_Error_Runge = 0.0751
Percentage_Error_Runge = 0.7330
Max_Error_Kutta = 0.0075
Percentage_Error_Kutta = 0.0731
Max_Error_Runge = 0.0075
Percentage_Error_Runge = 0.0731
Max_Error_Kutta = 7.4954e-04
Percentage_Error_Kutta = 0.0073
Max_Error_Runge = 7.4954e-04
Percentage_Error_Runge = 0.0073

5. Question 5(R-k fourth order with three step sizes )

clear all
clc

x0 = 0;
y0 = 2;
xEnd = 5;
for h = [0.1,0.01,0.001]

%% Initialization of Solution
N = (xEnd - x0)./h;
X = (x0:h:xEnd)';
Y = zeros(N+1,1);
Y(1) = y0;
%% Solving by Kutta's method

%% Solving IVP by R-K fourth order


for i = 1:N
f = (-2*Y(i) + 5*exp(-X(i)));
k7 = f;
k8 = f+(k7*h/3);
k9 = f+(k8*2*h/3);
k10 = f+(k9*h);
Y(i+1) = Y(i) + (k7+(3*k8)+(3*k9)+k10)*h/8;
end

%% Plotting and Error analysis


Ytrue = (5.*exp(-X)) - (3.*exp(-2.*X));
Err4 = abs(Ytrue-Y);
plot(X,Y); hold on
Max_Error_Kutta = max(Err4)
PErr4 = (max(Err4)/Ytrue*100);
Percentage_Error_Kutta = max(PErr4)

%% Solving by Runge's Method

%% Solving IVP by R-K fourth order


for i = 1:N
f2 = (-2*Y(i) + 5*exp(-X(i)));
k11 = f2;
k12 = f2+(k11*h/2);
k13 = f2+(k12*h/2);
k14 = f2+(k13*h);
Y1(i+1) = Y(i) + (k11+(2*k12)+(2*k13)+k14)*h/6;
end

%% Plotting and Error analysis for runge’s Method


Err5 = abs(Ytrue-Y);
plot(X,Y)
Max_Error_Runge = max(Err5)
PErr5 = (max(Err5)/Ytrue)*100;
Percentage_Error_Runge = max(PErr5)

end
plot(X,Ytrue)
hold off

OUTPUT

Max_Error_Kutta = 0.0613
Percentage_Error_Kutta = 2.9436
Max_Error_Runge = 0.0613
Percentage_Error_Runge = 2.9436
Max_Error_Kutta = 0.0053
Percentage_Error_Kutta = 0.2555
Max_Error_Runge = 0.0053
Percentage_Error_Runge = 0.2555
Max_Error_Kutta = 5.2545e-04
Percentage_Error_Kutta = 0.0252
Max_Error_Runge = 5.2545e-04
Percentage_Error_Runge = 0.0252

You might also like