FINAL EXAM
DIAMANTE OPENA
Problem Given:
By solving an appropriate initial value problem, make a table of
values of the function
x
t
2
f (t) given by the following integral
1 1
f ( x )= +
e dt for 0 x 3
2 2 0
Use the Runge-Kutta method of order four with h = 0:1 for
your computations. Use the solution to generate a table of
areas for a standard normal distribution.
Solutions: Codes needed for the program:
Simpsons Method for evaluating integral
function s=simpr1(f, a, b, M)
//input
//f given function using def
//a,b bounds of the def integral
// 2M no of sub intervals
//output
//s approximate value of the def. integral
h = (b-a)/(2*M); //distance between nodes
s1 = 0;
s2 = 0;
k=1;
while k<=M
x = a + h*(2*k-1);
s1 = s1 + f(x);
k=k+1;
end
j = 1;
while j<=M-1
y = a + h*(2*j);
s2 = s2 + f(y);
j=j+1;
end
s = (h/3)*(f(a) + f(b) + 4*s1 + 2*s2);
endfunction
Runge-Kutta Method of order 4 for evaluating initial value
problems
function Y=rk4(f, a, b, ya, M)
// Input
// a, b - left and right endpoints
// ya - initial condition y(a)
// M - number of mesh points
// Ouput
// T - mesh points
// Y - approximate solution
h = (b - a)/M;
T = a:h:b;
Y = zeros(1, M + 1);
Y(1) = ya;
for j = 1:M
k1 = h*f(T(j), Y(j));
k2 = h*f(T(j)+h/2, Y(j)+k1/2);
k3 = h*f(T(j)+h/2, Y(j)+k2/2);
k4 = h*f(T(j)+h, Y(j)+k3);
Y(j+1) = Y(j) + (k1 + 2*k2 + 2*k3 + k4)/6;
end
endfunction
Command:
clc;clear;
exec("rk4.sce");
def("y = f(x,t)" , "y = exp(((-x^2)/2))/sqrt(2*%pi)")
format (16);
M = 30; a = 0; b = 3; ya = 0.5;
Y = rk4(f,a,b,ya,M);
exec("simp1.sce");
def("y = g(x)" , "y = exp(((-x^2)/2))")
a = 0; N = 30;
T = [0:0.1:3];
for i = 1:31
b = T(i);
S(i) = simpr1(g,a,b,N);
U(i) = (1/2) + (1/sqrt(2*%pi))*S(i);
end
Y = Y';
E=abs(U - Y)';
A=[T' Y U E'];
fd = mopen("RESULTSFINAL.xls","w");
for i1=1:31
for j1=1:4
mfprintf(fd,"%.16f\t",A(i1,j1));
end
mfprintf(fd,"\n");
end
mclose(fd);
Results:
Z
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3
Predicted
0.5
0.539827841
0.579259717
0.617911434
0.655421756
0.691462478
0.725746901
0.758036367
0.78814462
0.815939893
0.841344763
0.864333954
0.884930342
0.903199526
0.919243348
0.933192804
0.945200711
0.955434538
0.96406968
0.971283438
0.977249864
0.982135575
0.986096547
0.989275885
0.991802459
0.99379033
0.995338807
0.996533022
0.997444866
0.998134183
0.998650099
Actual
0.5
0.539827837
0.579259709
0.617911422
0.655421742
0.691462461
0.725746882
0.758036348
0.788144602
0.815939875
0.841344746
0.864333939
0.88493033
0.903199516
0.919243341
0.933192799
0.945200709
0.955434537
0.964069681
0.97128344
0.977249867
0.982135578
0.986096551
0.989275888
0.991802462
0.993790332
0.995338809
0.996533023
0.997444867
0.998134184
0.998650099
Error
0
4.12726E-09
8.04976E-09
1.15764E-08
1.45421E-08
1.68186E-08
1.83219E-08
1.90166E-08
1.89165E-08
1.80814E-08
1.66111E-08
1.46357E-08
1.23054E-08
9.77823E-09
7.20891E-09
4.73824E-09
2.48475E-09
5.38816E-10
1.04046E-09
2.22548E-09
3.01793E-09
3.44445E-09
3.55093E-09
3.39605E-09
3.04486E-09
2.56282E-09
2.01092E-09
1.44199E-09
8.98407E-10
4.11161E-10
0