You are on page 1of 4

Lecture Computational Physics. Teacher: Dr Khusniddin.

Numerical Solution of practical


problems writing logical programs in MATLAB
[Q. 1] Write a program to solve differential equation y=2xy with initial condition y(x=0)=1 and the step length h=0.01
on the interval [0 ; 1] using Improved-Euler method and determine the value of your solution y(x=1)=? (The value of
solution y for the last point x=1 of the interval [0 ; 1]).
>> % Solution by Improved Euler Method;
format long
a=0; b=1.; h=0.01; n=(b-a)/h+1;
for i=1:n
x(i)=a+(i-1)*h;
end
x(1)=0.;
y(1)=1.;
f=@(x,y)2*x*y;
for i=1:n-1
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h,y(i)+k1);
y(i+1)=y(i)+(k1+k2)/2.;
end
plot(x,y),xlabel('x'),ylabel('y(from improved Euler Method)')
y(101)
ans =
2.71819095944903
[Q. 2] Write a program to solve differential equation y=2xy+1 with initial condition y(x=0)=0 and the step length
h=0.01 on the interval [0 ; 1] using Improved-Euler method and determine the value of your solution y(x=0.5)=? (The
value of solution y for the point x=0.5).
>> % Solution by Improved Euler Method;
format long
a=0; b=1.; h=0.01; n=(b-a)/h+1;
for i=1:n
x(i)=a+(i-1)*h;
end
x(1)=0.;
y(1)=0.;
f=@(x,y)2*x*y+1;
for i=1:n-1
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h,y(i)+k1);
y(i+1)=y(i)+(k1+k2)/2.;
end
plot(x,y),xlabel('x'),ylabel('y(from improved Euler Method)')
y(51)
ans =
0.59231207062377
[Q. 3] According to the law of radioactive decay the amount of radioactive substance present at a time t is determined
by the relation

y (t ) = y0 * exp( k * t ) ,

where t the time, y0 the amount of radioactive substance present at a time t=0, and k decay constant.
Given k = 1.8*10-11 sec-1 (to be written in Matlab as 1.8e-011) and y0=35, write the program and determine the value
of A:
A = (y(t=1))2+(y(t=2))2+(y(t=3))2+(y(t=4))2+.....+(y(t=501))2

>> clear all


format long
y0=35; k=-1.8e-011; n=501;
f=@(t)y0*exp(k*t);
aaa=0;
for i=1:n
aaa=aaa+f(i).*f(i);
end
aaa
aaa =
6.137249944543809e+005
[Q. 4] Write a simple program in MATLAB (using for end loop, and, of course, format long) to calculate the
following sum J

300

=
i =1

1 1 1
1
1
= 3 + 3 + 3 + +
.
3
i
1 2 3
300 3

You need to present in your answer sheets: J=?


>> clear all
format long
n=300; j=0;
for i=1:n
j=j+(1/(i*i*i));
end
j
j=
1.20205136609169
[Q. 5] Write a program to solve differential equation y=2xy with initial condition y(x=0)=1 and the step length h=0.01
on the interval [0 ; 1] using Runge-Kutta method. Make the necessary additions to your program to determine the value
of A using your solution y(x):
A = (y(x=0.00))1.8+(y(x=0.03))1.8+(y(x=0.06))1.8+(y(x=0.09))1.8+.....+(y(x=0.99))1.8
>> clear all
format long
a=0; b=1.; h=0.01; n=(b-a)/h+1;
for i=1:n
x(i)=a+(i-1)*h;
end
x(1)=0.;
y(1)=1.;
f=@(x,y) 2*x.*y;
for i=1:n-1
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2.,y(i)+k1/2.);
k3=h*f(x(i)+h/2.,y(i)+k2/2.);
k4=h*f(x(i)+h,y(i)+k3);
y(i+1)=y(i)+(k1+2.*k2+2.*k3+k4)/6.;
end
A=0.;
for i=1:34
j=3*(i-1)+1;
A=A+(y(j)).^1.8;
end

A
plot(x,y),xlabel('x'),ylabel('y')
A=
72.50326843711238
[Q. 6] Write a program to solve differential equation y=2xy+1 with initial condition y(x=0)=0 and the step length
h=0.01 on the interval [0 ; 1] using Runge-Kutta method. Make the necessary additions to your program to determine
the value of A using your solution y(x):
A = (y(x=0.00))2.2+(y(x=0.10))2.2+(y(x=0.20))2.2+(y(x=0.30))2.2+.....+(y(x=1.00))2.2
>> clear all
format long
a=0; b=1.; h=0.01; n=(b-a)/h+1;
for i=1:n
x(i)=a+(i-1)*h;
end
x(1)=0.;
y(1)=0.;
f=@(x,y) 2*x.*y+1;
for i=1:n-1
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2.,y(i)+k1/2.);
k3=h*f(x(i)+h/2.,y(i)+k2/2.);
k4=h*f(x(i)+h,y(i)+k3);
y(i+1)=y(i)+(k1+2.*k2+2.*k3+k4)/6.;
end
A=0.;
for i=1:11
j=10*(i-1)+1;
A=A+(y(j)).^2.2;
end
A
plot(x,y),xlabel('x'),ylabel('y')
A=
11.25690268339038
[Q. 7] Write a program to solve differential equation y=y with initial condition y(x=0)=1 and the step length h=0.01
on the interval [0 ; 1] using Runge-Kutta method. Make the necessary additions to your program to determine the value
of A using your solution y(x):
A=(y(x=0.00))2.5+(y(x=0.06))2.5+(y(x=0.12))2.5+(y(x=0.18))2.5+.....+(y(x=0.96))2.5
>> clear all
format long
a=0; b=1.; h=0.01; n=(b-a)/h+1;
for i=1:n
x(i)=a+(i-1)*h;
end
x(1)=0.;
y(1)=1.;
f=@(x,y)y;
for i=1:n-1
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2.,y(i)+k1/2.);
k3=h*f(x(i)+h/2.,y(i)+k2/2.);

k4=h*f(x(i)+h,y(i)+k3);
y(i+1)=y(i)+(k1+2.*k2+2.*k3+k4)/6.;
end
A=0.;
for i=1:17
j=6*(i-1)+1;
A=A+(y(j)).^2.5;
end
A
plot(x,y),xlabel('x'),ylabel('y')
A=
72.95800679735507
[Q. 8] Write a program to solve differential equation y=x+y with initial condition y(x=0)=0 and the step length h=0.01
on the interval [0 ; 1] using Runge-Kutta method. Make the necessary additions to your program to determine the value
of A using your solution y(x):
A=(y(x=0.00))3.7+(y(x=0.07))3.7+(y(x=0.14))3.7+(y(x=0.21))3.7+.....+(y(x=0.98))3.7
>> clear all
format long
a=0; b=1.; h=0.01; n=(b-a)/h+1;
for i=1:n
x(i)=a+(i-1)*h;
end
x(1)=0.;
y(1)=0.;
f=@(x,y)x+y;
for i=1:n-1
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2.,y(i)+k1/2.);
k3=h*f(x(i)+h/2.,y(i)+k2/2.);
k4=h*f(x(i)+h,y(i)+k3);
y(i+1)=y(i)+(k1+2.*k2+2.*k3+k4)/6.;
end
A=0.;
for i=1:15
j=7*(i-1)+1;
A=A+(y(j)).^3.7;
end
A
plot(x,y),xlabel('x'),ylabel('y')
A=
0.49196637307480

You might also like