23.10.2014 Views

Numerical Analysis Programs Using Fortran 90 - University of ...

Numerical Analysis Programs Using Fortran 90 - University of ...

Numerical Analysis Programs Using Fortran 90 - University of ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Kurdistan Region-Iraq<br />

Sulaimani <strong>University</strong><br />

College <strong>of</strong> Science<br />

Physics Department<br />

<strong>Numerical</strong> <strong>Analysis</strong> <strong>Programs</strong><br />

<strong>Using</strong> <strong>Fortran</strong> <strong>90</strong><br />

(2009)<br />

Prepared by<br />

Dr. Omed Gh. Abdullah<br />

1


Problem: Write a program to find the roots <strong>of</strong> the equation<br />

x<br />

y = e − 3x<br />

using Bisection method in the interval [ 1,2]<br />

, within<br />

3<br />

the tolerance 10 − .<br />

f(x)=exp(x)-3*x<br />

tol=.001<br />

a=1<br />

b=2<br />

10 c=(a+b)/2<br />

print*,a,b,c<br />

if (f(c)*f(a))20,30,30<br />

20 b=c<br />

goto 40<br />

30 a=c<br />

40 if (abs (a-b).lt.tol) goto 50<br />

goto 10<br />

50 print*,'The Root is',c<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

1.000000 2.000000 1.500000<br />

1.500000 2.000000 1.750000<br />

1.500000 1.750000 1.625000<br />

1.500000 1.625000 1.562500<br />

1.500000 1.562500 1.531250<br />

1.500000 1.531250 1.515625<br />

1.500000 1.515625 1.507813<br />

1.507813 1.515625 1.511719<br />

1.511719 1.515625 1.513672<br />

1.511719 1.513672 1.512695<br />

The Root is 1.512695<br />

2


Problem: Write a program to find the roots <strong>of</strong> the equation<br />

f ( x)<br />

= x sin( x)<br />

− 1 using False position method in the interval<br />

[ 1,2] , within the tolerance<br />

4<br />

10 − .<br />

f(x)=x*sin(x)-1<br />

tol=.0001<br />

a=1<br />

b=2<br />

10 c=(a*f(b)-b*f(a))/(f(b)-f(a))<br />

print*,a,b,c<br />

if (f(a)*f(c))20,30,30<br />

20 b=c<br />

goto 40<br />

30 a=c<br />

40 if (abs (a-b).lt.tol) goto 50<br />

goto 10<br />

50 print*,'The Root is',c<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

1.000000 2.000000 1.162241<br />

1.000000 1.162241 1.114254<br />

1.000000 1.114254 1.114157<br />

The Root is 1.114157<br />

3


Problem: Write a program to find the roots <strong>of</strong> the equation<br />

x<br />

f ( x)<br />

= 3x<br />

+ sin x − e using Secant method in the interval [ 0,1]<br />

,<br />

4<br />

within the tolerance 10 − .<br />

f(x)=3*x+sin(x)-exp(x)<br />

tol=.00001<br />

x1=0<br />

x2=1<br />

10 x3=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1))<br />

print*,x1,x2,x3<br />

x1=x2<br />

x2=x3<br />

if (abs (x1-x2).lt.tol) goto 20<br />

goto 10<br />

20 print*,'The Root is',x3<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

0.000000 1.000000 4.709896E-01<br />

1.000000 4.709896E-01 3.075085E-01<br />

4.709896E-01 3.075085E-01 3.626132E-01<br />

3.075085E-01 3.626132E-01 3.604615E-01<br />

3.626132E-01 3.604615E-01 3.604217E-01<br />

3.604615E-01 3.604217E-01 3.604217E-01<br />

The Root is 3.604217E-01<br />

4


Problem: Write a program to find the roots <strong>of</strong> the equation<br />

2<br />

f ( x)<br />

= x − 2x<br />

− 3 by iterative method, within the tolerance<br />

4<br />

10 − .<br />

read*,xo<br />

tol=0.0001<br />

5 x=sqrt(2*xo+3)<br />

print*,xo,x<br />

if(abs(x-xo)


Problem: Write a program using the Newton-Raphson procedure to<br />

3 2<br />

determine the roots <strong>of</strong> the equation f ( x)<br />

= x − x − 2x<br />

+ 1 ,<br />

6<br />

within the tolerance 10 − .<br />

f(x)=x**3-x**2-2*x+1<br />

df(x)=3*x**2-2*x-2<br />

tol=1e-6<br />

xo=2<br />

k=0<br />

10 k=k+1<br />

x=xo-f(xo)/df(xo)<br />

if(abs(x-xo).lt.tol) goto 20<br />

print *,k,x<br />

xo=x<br />

goto 10<br />

20 print *,'The Root is',x<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

1 1.833333<br />

2 1.802935<br />

3 1.801939<br />

4 1.801938<br />

The Root is 1.801938<br />

If xo=0<br />

The Root is 4.450419E-01<br />

If xo=-1<br />

The Root is -1.246980<br />

6


Problem: Write a program to determine the roots <strong>of</strong> the equation<br />

4<br />

3<br />

2<br />

x −1.99x<br />

−1.76x<br />

+ 5.22x<br />

− 2.23 = 0 that are close to x = 1. 5<br />

by Newton-Raphson method.<br />

f(x)=x**4-1.99*x**3-1.76*x**2+5.22*x-2.23<br />

df(x)=4*x**3-5.97*x**2-3.52*x+5.22<br />

ddf(x)=12*x**2-11.94*x-3.52<br />

tol=0.00001<br />

x=1.5<br />

5 rat=df(x)/ddf(x)<br />

x=x-rat<br />

if(abs(rat)-tol)10,10,5<br />

10 eps=sqrt(-2*f(x)/ddf(x))<br />

sign=1<br />

do i=1,2<br />

k=0<br />

y=x+sign*eps<br />

15 k=k+1<br />

print *,y<br />

rat=f(y)/df(y)<br />

y=y-rat<br />

if (abs(rat)-tol)20,15,15<br />

20 sign=-1<br />

print*,'++++++++++++++++++++++++++++++++'<br />

enddo<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

1.569134<br />

1.565973<br />

1.565888<br />

++++++++++++++++++++++++++<br />

1.428165<br />

1.424016<br />

1.424112<br />

++++++++++++++++++++++++++<br />

7


Problem: Write a program to determine the roots <strong>of</strong> the equation<br />

4<br />

3<br />

2<br />

x −1.99x<br />

−1.76x<br />

+ 5.22x<br />

− 2.23 = 0 that are close to x = 1. 5<br />

by Newton-Raphson method.<br />

f(x)=x**4-1.99*x**3-1.76*x**2+5.22*x-2.23<br />

df(x)=4*x**3-5.97*x**2-3.52*x+5.22<br />

ddf(x)=12*x**2-11.94*x-3.52<br />

tol=0.00001<br />

xo=1.5<br />

10 x=xo-df(xo)/ddf(xo)<br />

if(abs(x-xo).lt.tol) goto 20<br />

xo=x<br />

goto 10<br />

20 eps=sqrt(-2*f(x)/ddf(x))<br />

y1=x+eps<br />

30 y=y1-f(y1)/df(y1)<br />

if(abs(y-y1).lt.tol) goto 40<br />

print *,y<br />

y1=y<br />

goto 30<br />

40 print *,'The First Root is',y<br />

print*,'************************'<br />

y1=x-eps<br />

50 y=y1-f(y1)/df(y1)<br />

if(abs(y-y1).lt.tol) goto 60<br />

print *,y<br />

y1=y<br />

goto 50<br />

60 print *,'The Second Root is',y<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

1.565973<br />

1.565888<br />

The First Root is 1.565888<br />

************************************<br />

1.424016<br />

1.424112<br />

The Second Root is 1.424112<br />

++++++++++++++++++++++++++<br />

8


Problem: Write a program to determine the roots <strong>of</strong> the equation<br />

3 2<br />

8x − 20x<br />

− 26x<br />

+ 33 = 0 that are close to x = 0. 8 using Birge-<br />

Vieta method.<br />

dimension a(0:10),b(0:10),c(0:10)<br />

a(0)=8<br />

a(1)=-20<br />

a(2)=-26<br />

a(3)=33<br />

m=3<br />

xo=0.8<br />

tol=1e-6<br />

5 k=k+1<br />

b(0)=a(0); c(0)=b(0)<br />

do j=1,m<br />

b(j)=a(j)+xo*b(j-1)<br />

c(j)=b(j)+xo*c(j-1)<br />

enddo<br />

x=xo-b(m)/c(m-1)<br />

print *,k,x<br />

if(abs(x-xo)


Problem: Write a program to determine the roots <strong>of</strong> the equation<br />

3 2<br />

8x − 20x<br />

− 26x<br />

+ 33 = 0 that are close to x = 0. 8 using Birge-<br />

Vieta method.<br />

dimension a(0:10),b(0:10),c(0:10)<br />

data (a(i),i=0,3)/8,-20,-26,33/<br />

m=3<br />

k=0<br />

xo=0.8<br />

tol=1e-6<br />

5 k=k+1<br />

b(0)=a(0); c(0)=b(0)<br />

do j=1,m<br />

b(j)=a(j)+xo*b(j-1)<br />

c(j)=b(j)+xo*c(j-1)<br />

enddo<br />

x=xo-b(m)/c(m-1)<br />

print *,k,x<br />

if(abs(x-xo)


Problem: Write a program to determine the roots <strong>of</strong> the system <strong>of</strong><br />

2<br />

2<br />

equations x + xy −10<br />

= 0 ; y + 3xy<br />

− 57 = 0<br />

using the fixed point iteration. Initiate the computation with<br />

guesses <strong>of</strong> x = 1. 5 and y = 3. 5 .<br />

xo=1.5<br />

yo=3.5<br />

tol=0.0001<br />

5 x=sqrt(10-xo*yo)<br />

y=sqrt((57-yo)/(3*x))<br />

print*,x,y<br />

if((abs(x-xo)


Problem: Write a program to solve the following system <strong>of</strong> equations,<br />

by using Newton-Raphson Method<br />

2<br />

2<br />

x − y − 0.2 = 0 ; y − x − 0.3 = 0<br />

Initiate the computation with guesses <strong>of</strong> x = 1. 2 and y = 1. 2 .<br />

f(x,y)=x**2-y-.2<br />

g(x,y)=y**2-x-.3<br />

fx(x,y)=2*x<br />

fy(x,y)=-1<br />

gx(x,y)=-1<br />

gy(x,y)=2*y<br />

xo=1.2<br />

yo=1.2<br />

tol=0.0001<br />

5 xj=fx(xo,yo)*gy(xo,yo)-fy(xo,yo)*gx(xo,yo)<br />

hh=(g(xo,yo)*fy(xo,yo)-f(xo,yo)*gy(xo,yo))/xj<br />

hk=(f(xo,yo)*gx(xo,yo)-g(xo,yo)*fx(xo,yo))/xj<br />

x=xo+hh<br />

y=yo+hk<br />

print*,x,y<br />

if((abs(x-xo)


Problem: Write a program to find the interpolated value for x =1. 15,<br />

using Newton forward method, for these tabulated data.<br />

X 1 1.2 1.4 1.6 1.8 2.0<br />

f 2.317 2.425 2.522 2.609 2.689 2.762<br />

program Newton_Forward_Iterpolation<br />

dimension x(20),y(20),d(20,20)<br />

n=6<br />

xx=1.15<br />

data (x(i), i=1,6) /1,1.2,1.4,1.6,1.8,2/<br />

data (y(i), i=1,6)/2.317,2.425,2.522,2.609,2.689,2.762/<br />

print*,(x(i), i=1,6)<br />

print*,(y(i), i=1,6)<br />

s=y(1)<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=y(j+1)-y(j)<br />

d(i,j)=y(j)<br />

enddo<br />

enddo<br />

r=(xx-x(1))/(x(2)-x(1))<br />

do i=1,n-1<br />

p=1<br />

do j=0,i-1<br />

p=p*(r-j)<br />

enddo<br />

f=1<br />

do j=1,i; f=f*j; enddo<br />

s=s+d(i,1)*p/f<br />

enddo<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong> (1.15)= 2.398955<br />

13


Problem: Write a program to find the interpolated value for x =1. 15,<br />

using Newton forward method, for these tabulated data.<br />

X 1 1.2 1.4 1.6 1.8 2.0<br />

f 2.317 2.425 2.522 2.609 2.689 2.762<br />

dimension f(0:20),diff(0:20)<br />

xo=1<br />

x=1.15<br />

h=0.2<br />

n=5<br />

data (f(i), i=0,5)/2.317,2.425,2.522,2.609,2.689,2.762/<br />

r=(x-xo)/h<br />

do i=0,n-1<br />

diff(i)=f(i+1)-f(i)<br />

enddo<br />

coeff=r<br />

yx=f(0)+coeff*diff(0)<br />

do i=2,n<br />

coeff=coeff*(r-i+1)/(i)<br />

do j=0,n-i<br />

diff(j)=diff(j+1)-diff(j)<br />

enddo<br />

yx=yx+coeff*diff(0)<br />

enddo<br />

print*,yx<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong> (1.15)= 2.398955<br />

14


Problem: Write a program to find the interpolated value for x = 0. 55,<br />

using Newton forward method, for these tabulated data.<br />

X 0.5 0.7 0.9 1.1 1.3 1.5<br />

Y 0.47943 0.64422 0.78333 0.89121 0.96356 0.99749<br />

dimension y(20),diff(20)<br />

x0=0.5<br />

x=0.55<br />

h=0.2<br />

n=6<br />

m=4<br />

data (y(i), i=1,6) /0.47943,0.64422,0.78333,0.89121,0.96356,0.99749/<br />

j=int((x-x0)/h-0.5)<br />

r=(x-x0)/h-j<br />

if ((j.ge.0).and.(j.lt.n-m)) goto 10<br />

print*,'Not enough function values'<br />

goto 20<br />

10 do i=1,m-1<br />

diff(i)=y(j+i+1)-y(j+i)<br />

enddo<br />

coeff=r<br />

yx=y(1)+coeff*diff(1)<br />

do i=2,m-1<br />

coeff=coeff*(r-i+1)/i<br />

do j=1,m-i<br />

diff(j)=diff(j+1)-diff(j)<br />

enddo<br />

yx=yx+coeff*diff(1)<br />

enddo<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',yx<br />

20 end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong> (0.55)= 5.227315 E-01<br />

15


Problem: Write a program to find the interpolated value for x =1. 93,<br />

using Newton backward method, for these tabulated data.<br />

X 1 1.2 1.4 1.6 1.8 2.0<br />

f 2.317 2.425 2.522 2.609 2.689 2.762<br />

program Newton_Backward_Iterpolation<br />

dimension x(20),y(20),d(20,20)<br />

n=6<br />

xx=1.93<br />

data (x(i), i=1,6) /1,1.2,1.4,1.6,1.8,2/<br />

data (y(i), i=1,6)/2.317,2.425,2.522,2.609,2.689,2.762/<br />

print*,(x(i), i=1,6)<br />

print*,(y(i), i=1,6)<br />

s=y(n)<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=y(j+1)-y(j)<br />

d(i,j)=y(j)<br />

enddo<br />

enddo<br />

r=(xx-x(n))/(x(2)-x(1))<br />

do i=1,n-1<br />

p=1<br />

do j=0,i-1<br />

p=p*(r+j)<br />

enddo<br />

f=1<br />

do j=1,i; f=f*j; enddo<br />

s=s+d(i,n-i)*p/f<br />

enddo<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong>(1.93)=2.737522<br />

16


Problem: Write a program to find the interpolated value for x =1. 47 ,<br />

using Newton backward method, for these tabulated data.<br />

x 0.5 0.7 0.9 1.1 1.3 1.5<br />

y 0.47943 0.64422 0.78333 0.89121 0.96356 0.99749<br />

program Newton_Backward_Iterpolation<br />

dimension x(20),y(20),d(20,20)<br />

n=6<br />

xx=1.47<br />

data (x(i), i=1,6)/.5,.7,.9,1.1,1.3,1.5/<br />

data (y(i), i=1,6) /0.47943,0.64422,0.78333,0.89121,0.96356,0.99749/<br />

print*,(x(i), i=1,6)<br />

print*,(y(i), i=1,6)<br />

s=y(n)<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=y(j+1)-y(j)<br />

d(i,j)=y(j)<br />

enddo<br />

enddo<br />

r=(xx-x(n))/(x(2)-x(1))<br />

do i=1,n-1<br />

p=1<br />

do j=0,i-1<br />

p=p*(r+j)<br />

enddo<br />

f=1<br />

do j=1,i; f=f*j; enddo<br />

s=s+d(i,n-i)*p/f<br />

enddo<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong>(1.47)= 9.9492 E-01<br />

17


Problem: Write a program to find the interpolated value for x =1. 05 ,<br />

using Stirling Interpolation method, for these tabulated data.<br />

X 0.5 0.7 0.9 1.1 1.3 1.5<br />

y 0.48 0.64 0.78 0.89 0.96 0.99<br />

program Stirling_Formula_Iterpolation<br />

dimension x(20),y(20),d(20,20)<br />

n=6<br />

xx=1.05<br />

data (x(i), i=1,6) /.5,.7,.9,1.1,1.3,1.5/<br />

data (y(i), i=1,6)/.48,.64,.78,.89,.96,.99/<br />

print*,(x(i), i=1,6)<br />

print*,(y(i), i=1,6)<br />

s=y(4)<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=y(j+1)-y(j)<br />

d(i,j)=y(j)<br />

print*,d(i,j)<br />

enddo<br />

enddo<br />

r=(xx-x(4))/(x(2)-x(1))<br />

p1=r*(d(1,3)+d(1,4))/2<br />

p2=r**2/2*d(2,3)<br />

p3=r*(r**2-1)/6*(d(3,2)+d(3,3))/2<br />

p4=r**2*(r**2-1)/24*d(4,2)<br />

s=s+p1+p2+p3+p4<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong>(1.05)=8.660302 E-01<br />

18


Problem: Write a program to find the interpolated value for x =1. 05 ,<br />

using Bessel’s Interpolation method, for these tabulated data.<br />

X 0.5 0.7 0.9 1.1 1.3 1.5<br />

y 0.48 0.64 0.78 0.89 0.96 0.99<br />

program Bessels_Formula_Iterpolation<br />

dimension x(20),y(20),d(20,20)<br />

n=6<br />

xx=1.05<br />

data (x(i), i=1,6) /.5,.7,.9,1.1,1.3,1.5/<br />

data (y(i), i=1,6)/.48,.64,.78,.89,.96,.99/<br />

print*,(x(i), i=1,6)<br />

print*,(y(i), i=1,6)<br />

s=(y(3)+y(4))/2<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=y(j+1)-y(j)<br />

d(i,j)=y(j)<br />

print*,d(i,j)<br />

enddo<br />

enddo<br />

r=(xx-x(3))/(x(2)-x(1))<br />

p1=(r-.5)*d(1,3)<br />

p2=r*(r-1)/2*(d(2,2)+d(2,3))/2<br />

p3=r*(r-1)*(r-.5)/6*d(3,2)<br />

p4=r*(r-1)*(r+1)*(r-2)/24*(d(4,1)+d(4,2))/2<br />

s=s+p1+p2+p3+p4<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong>(1.05)=8.659448 E-01<br />

19


Problem: Write a program to find the interpolated value for x = 0. 7 ,<br />

using divided difference formula, for these tabulated data.<br />

X 0 0.5 1 1.4 2 3.2 5.5<br />

F(x) -1.25 -3.5 6 2.32 1.5 1.27 5.6<br />

program Divided_Differences_Iterpolation<br />

dimension x(20),y(20),d(20,20)<br />

n=7<br />

xx=.7<br />

data (x(i), i=1,7) /0,.5,1,1.4,2,3.2,5.5/<br />

data (y(i), i=1,7)/-1.25,-3.5,6,2.32,1.5,1.27,5.6/<br />

s=y(1)<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=(y(j+1)-y(j))/(x(j+i)-x(j))<br />

d(i,j)=y(j)<br />

print*,d(i,j)<br />

enddo<br />

enddo<br />

do i=1,n-1<br />

p=1<br />

do j=1,i<br />

p=p*(xx-x(j))<br />

enddo<br />

s=s+d(i,1)*p<br />

enddo<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong> (0.7) =2.2<strong>90</strong>929<br />

20


Problem: Write a program to find the interpolated value for x =1. 5,<br />

using divided difference formula, for these tabulated data.<br />

X 1 2 3 4 5<br />

F(x) 0 1 4 6 10<br />

program Divided_Differences_Iterpolation<br />

dimension x(20),y(20),d(20,20)<br />

n=5<br />

xx=1.5<br />

data (x(i), i=1,5)/1,2,3,4,5/<br />

data (y(i), i=1,5)/0,1,4,6,10/<br />

s=y(1)<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=(y(j+1)-y(j))/(x(j+i)-x(j))<br />

d(i,j)=y(j)<br />

enddo<br />

enddo<br />

do i=1,n-1<br />

p=1<br />

do j=1,i<br />

p=p*(xx-x(j))<br />

enddo<br />

s=s+d(i,1)*p<br />

enddo<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong> (1.5) = -0.171875<br />

21


Problem: Write a program to find the interpolated value for x = 3, using<br />

Lagrangian Polynomial, from the following data.<br />

X 3.2 2.7 1 4.8<br />

F(x) 22 17.8 14.2 38.3<br />

program Lagrange_Iterpolation<br />

dimension x(20),y(20)<br />

n=4<br />

xx=3<br />

data (x(i), i=1,4)/3.2,2.7,1,4.8/<br />

data (y(i), i=1,4)/22,17.8,14.2,38.3/<br />

s=0<br />

do i=1,n<br />

p=1<br />

do j=1,n<br />

if (i.eq.j) goto 5<br />

p=p*(xx-x(j))/(x(i)-x(j))<br />

5 enddo<br />

s=s+p*y(i)<br />

enddo<br />

print*,'Interpolation <strong>of</strong>(',xx,')=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Interpolation <strong>of</strong> (3) = 20.21196<br />

22


Problem:<br />

Write a program to determine the parameters a<br />

1 & a<br />

2 so that<br />

( x)<br />

= a + a x , fits the following data in least squares sense.<br />

f<br />

1 2<br />

X 0 0.3 0.6 0.9 1.2 1.5 1.8 2.1<br />

y 1 2.7 4.3 6 7.5 9 10.6 12<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=8<br />

data (x(i), i=1,8)/0,.3,.6,.9,1.2,1.5,1.8,2.1/<br />

data (y(i), i=1,8)/1,2.7,4.3,6,7.5,9,10.6,12/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+x(i)<br />

sy=sy+y(i)<br />

sxx=sxx+x(i)**2<br />

sxy=sxy+x(i)*y(i)<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

print*,'a1=',a1<br />

print*,'a2=',a2<br />

s=0<br />

do i=1,n<br />

f(i)=a1+a2*x(i)<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

sx=8.4 sxx=12.6 sy=53.1 sxy=75.57<br />

a1=1.133333<br />

a2=5.242064<br />

Standard deviation = 6.726178 E -02<br />

23


Problem:<br />

Write a program to determine the parameters A & B so that<br />

f ( x)<br />

= A ln( x)<br />

+ B , fits the following data in least squares sense.<br />

X 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9<br />

y 0.27 0.72 1.48 2.66 4.48 7.26 11.43 17.64 26.78<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=9<br />

data (x(i), i=1,9)/.1,.2,.3,.4,.5,.6,.7,.8,.9/<br />

data (y(i), i=1,9)/.27,.72,1.48,2.66,4.48,7.26,11.43,17.64,26.78/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+log(x(i))<br />

sy=sy+y(i)<br />

sxx=sxx+(log(x(i)))**2<br />

sxy=sxy+log(x(i))*y(i)<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

a=a2; b=a1<br />

print*,'A=',a<br />

print*,'B=',b<br />

s=0<br />

do i=1,n<br />

f(i)=a*log(x(i))+b<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

A=9.74113 B=16.66255<br />

S=260.5141<br />

24


Problem:<br />

Write a program to determine the parameters A & C so that<br />

f =<br />

A x<br />

( x)<br />

C e , fits the following data in least squares sense.<br />

X 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9<br />

y 0.27 0.72 1.48 2.66 4.48 7.26 11.43 17.64 26.78<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=9<br />

data (x(i), i=1,9)/.1,.2,.3,.4,.5,.6,.7,.8,.9/<br />

data (y(i), i=1,9)/.27,.72,1.48,2.66,4.48,7.26,11.43,17.64,26.78/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+x(i)<br />

sy=sy+log(y(i))<br />

sxx=sxx+(x(i))**2<br />

sxy=sxy+x(i)*log(y(i))<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

a=a2; c=exp(a1)<br />

print*,'A=',a<br />

print*,'C=',c<br />

s=0<br />

do i=1,n<br />

f(i)=c*exp(a*x(i))<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

A= 5.512738<br />

C= .2359106<br />

S=52.53117<br />

25


Problem:<br />

Write a program to determine the parameters A & C so that<br />

A<br />

f ( x)<br />

= C x , fits the following data in least squares sense.<br />

X 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9<br />

y 0.27 0.72 1.48 2.66 4.48 7.26 11.43 17.64 26.78<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=9<br />

data (x(i), i=1,9)/.1,.2,.3,.4,.5,.6,.7,.8,.9/<br />

data (y(i), i=1,9)/.27,.72,1.48,2.66,4.48,7.26,11.43,17.64,26.78/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+log(x(i))<br />

sy=sy+log(y(i))<br />

sxx=sxx+(log(x(i)))**2<br />

sxy=sxy+log(x(i))*log(y(i))<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

a=a2; c=exp(a1)<br />

print*,'A=',a<br />

print*,'C=',c<br />

s=0<br />

do i=1,n<br />

f(i)=c*x(i)**a<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

A= 2.092605<br />

C= 23.42711<br />

S= 75.07242<br />

26


Problem:<br />

Write a program to determine the parameters C & D so that<br />

D x<br />

f ( x)<br />

= C x e , fits the following data in least squares sense.<br />

X 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9<br />

y 0.27 0.72 1.48 2.66 4.48 7.26 11.4 17.6 26.7<br />

3 4 8<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=9<br />

data (x(i), i=1,9)/.1,.2,.3,.4,.5,.6,.7,.8,.9/<br />

data (y(i), i=1,9)/.27,.72,1.48,2.66,4.48,7.26,11.43,17.64,26.78/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+x(i)<br />

sy=sy+log(y(i)/x(i))<br />

sxx=sxx+x(i)**2<br />

sxy=sxy+x(i)*log(y(i)/x(i))<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

d=a2; c=exp(a1)<br />

print*,'D=',d<br />

print*,'C=',c<br />

s=0<br />

do i=1,n<br />

f(i)=c*x(i)*exp(d*x(i))<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

C= 1.993407<br />

D= 3.004763<br />

S= 1.117279 E-01<br />

27


Problem:<br />

Write a program to determine the parameters A & B so that<br />

A<br />

f ( x)<br />

= + B , fits the following data in least squares sense.<br />

x<br />

X 0.5 2 3.5 4.1 5 6.2 7.5 9.2<br />

y 3.3 2 1.4 1.2 1 0.8 0.64 0.5<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=8<br />

data (x(i), i=1,8)/.5,2,3.5,4.1,5,6.2,7.5,9.2/<br />

data (y(i), i=1,8)/3.3,2,1.4,1.2,1,.8,.64,.5/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+1/x(i)<br />

sy=sy+y(i)<br />

sxx=sxx+(1/x(i))**2<br />

sxy=sxy+y(i)*(1/x(i))<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

a=a2; b=a1<br />

print*,'A=',a<br />

print*,'B=',b<br />

s=0<br />

do i=1,n<br />

f(i)=a/x(i)+b<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

A= 1.353131<br />

B= .7405201<br />

S=.7070401<br />

28


Problem:<br />

Write a program to determine the parameters D & C so that<br />

D<br />

f ( x)<br />

= , fits the following data in least squares sense.<br />

x + C<br />

X 0.5 2 3.5 4.1 5 6.2 7.5 9.2<br />

y 3.3 2 1.4 1.2 1 0.8 0.64 0.5<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=8<br />

data (x(i), i=1,8)/.5,2,3.5,4.1,5,6.2,7.5,9.2/<br />

data (y(i), i=1,8)/3.3,2,1.4,1.2,1,.8,.64,.5/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+x(i)*y(i)<br />

sy=sy+y(i)<br />

sxx=sxx+(x(i)*y(i))**2<br />

sxy=sxy+(x(i)*y(i))*y(i)<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

c=-1/a2; d=a1*c<br />

print*,'C=',c<br />

print*,'D=',d<br />

s=0<br />

do i=1,n<br />

f(i)=d/(x(i)+c)<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

C= 1.369226<br />

D= 6.20<strong>90</strong>51<br />

S=5.723841 E-02<br />

29


Problem:<br />

Write a program to determine the parameters A & B so that<br />

1<br />

f ( x)<br />

= , fits the following data in least squares sense.<br />

A x + B<br />

X 0.5 2 3.5 4.1 5 6.2 7.5 9.2<br />

y 3.3 2 1.4 1.2 1 0.8 0.64 0.5<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=8<br />

data (x(i), i=1,8)/.5,2,3.5,4.1,5,6.2,7.5,9.2/<br />

data (y(i), i=1,8)/3.3,2,1.4,1.2,1,.8,.64,.5/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+x(i)<br />

sy=sy+1/y(i)<br />

sxx=sxx+x(i)**2<br />

sxy=sxy+x(i)*1/y(i)<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

a=a2; b=a1<br />

print*,'A=',a<br />

print*,'B=',b<br />

s=0<br />

do i=1,n<br />

f(i)=1/(a*x(i)+b)<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

A= 1.953443 E-01<br />

B= 9.250808 E-02<br />

S= 3.864387<br />

30


Problem:<br />

Write a program to determine the parameters A & B so that<br />

x<br />

f ( x)<br />

= , fits the following data in least squares sense.<br />

A x + B<br />

X 0.5 2 3.5 4.1 5 6.2 7.5 9.2<br />

y 3.3 2 1.4 1.2 1 0.8 0.64 0.5<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=8<br />

data (x(i), i=1,8)/.5,2,3.5,4.1,5,6.2,7.5,9.2/<br />

data (y(i), i=1,8)/3.3,2,1.4,1.2,1,.8,.64,.5/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+1/x(i)<br />

sy=sy+1/y(i)<br />

sxx=sxx+(1/x(i))**2<br />

sxy=sxy+1/(x(i)*y(i))<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

a=a1; b=a2<br />

print*,'A=',a<br />

print*,'B=',b<br />

s=0<br />

do i=1,n<br />

f(i)=x(i)/(a*x(i)+b)<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

A= 1.279117<br />

B= -5.697291 E-01<br />

S= 16.4106<strong>90</strong><br />

31


Problem:<br />

Write a program to determine the parameters A & B so that<br />

1<br />

f ( x)<br />

= , fits the following data in least squares sense.<br />

A x + B<br />

( ) 2<br />

X 0.5 2 3.5 4.1 5 6.2 7.5 9.2<br />

y 3.3 2 1.4 1.2 1 0.8 0.64 0.5<br />

program List_Square_Fitting<br />

dimension x(20),y(20),f(20)<br />

n=8<br />

data (x(i), i=1,8)/.5,2,3.5,4.1,5,6.2,7.5,9.2/<br />

data (y(i), i=1,8)/3.3,2,1.4,1.2,1,.8,.64,.5/<br />

sx=0; sxx=0;sy=0;sxy=0<br />

do i=1,n<br />

sx=sx+x(i)<br />

sy=sy+(y(i))**(-.5)<br />

sxx=sxx+x(i)**2<br />

sxy=sxy+x(i)*(y(i))**(-.5)<br />

enddo<br />

print *,'sx=',sx<br />

print *,'sxx=',sxx<br />

print *,'sy=',sy<br />

print *,'sxy=',sxy<br />

d=n*sxx-sx**2<br />

a1=(sxx*sy-sx*sxy)/d<br />

a2=(n*sxy-sx*sy)/d<br />

a=a2; b=a1<br />

print*,'A=',a<br />

print*,'B=',b<br />

s=0<br />

do i=1,n<br />

f(i)=1/(a*x(i)+b)**2<br />

s=s+(y(i)-f(i))**2<br />

enddo<br />

print*,'Standerd Deviation=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

A= 9.919496 E-02<br />

B= 5.035566 E-01<br />

S= 2.275830 E-03<br />

32


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

1 dt<br />

∫<br />

using Trapezoidal rule with n = 6<br />

0 2<br />

2<br />

( t + 1) (3t<br />

+ 4)<br />

program Trapezoidal_Rule<br />

f(t)=1/((t**2+1)*(3*t**2+4))**.5<br />

a=0<br />

b=1<br />

n=6<br />

h=(b-a)/n<br />

s=0<br />

do i=1,n-1<br />

x=a+h*i<br />

s=s+f(x)<br />

enddo<br />

t=h*(f(a)/2+s+f(b)/2)<br />

print*,'Integration by Trapezoidal Rule=',t<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Integration by Trapezoidal rule = 4.016085 E-01<br />

33


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

π<br />

2<br />

dx<br />

∫ 2<br />

sin x + cos<br />

0<br />

dimension y(20)<br />

f(x)=1/((sin(x))**2+1./4*(cos(x))**2)<br />

pi=22./7<br />

a=0; b=pi/2; n=10<br />

h=(b-a)/n<br />

do i=1,n+1<br />

x=a+h*(i-1)<br />

y(i)=f(x)<br />

print*,y(i)<br />

enddo<br />

t=0<br />

do i=1,n<br />

t=t+h*(y(i)+y(i+1))/2<br />

enddo<br />

print*,'Integration by Trapezoidal rule =',t<br />

end<br />

1<br />

4<br />

2<br />

x<br />

using Trapezoidal rule with n = 10<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Integration by Trapezoidal rule = 3.142227<br />

34


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

1 dt<br />

∫<br />

using Simpson’s 1/3 rule with n = 6<br />

0 2<br />

2<br />

( t + 1) (3t<br />

+ 4)<br />

! program Simpson's 1/3 Rule<br />

f(t)=1/((t**2+1)*(3*t**2+4))**.5<br />

a=0<br />

b=1<br />

n=6<br />

h=(b-a)/n<br />

s1=0;s2=0<br />

do i=1,n-1,2<br />

x=a+h*i<br />

s1=s1+f(x)<br />

enddo<br />

do i=2,n-1,2<br />

x=a+h*i<br />

s2=s2+f(x)<br />

enddo<br />

s=(h/3)*(f(a)+4*s1+2*s2+f(b))<br />

print*,'Integration by Simpsons 1/3 Rule=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Integration by Simpson's 1/3 rule= 4.021834 E-01<br />

35


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

1 dt<br />

∫<br />

using Simpson’s 1/3 rule with n = 6<br />

0 2<br />

2<br />

( t + 1) (3t<br />

+ 4)<br />

! program Simpson's 1/3 Rule<br />

dimension y(20)<br />

f(t)=1/((t**2+1)*(3*t**2+4))**.5<br />

a=0<br />

b=1<br />

n=6<br />

h=(b-a)/n<br />

s=0<br />

do i=1,n+1<br />

x=a+h*(i-1)<br />

y(i)=f(x)<br />

enddo<br />

do i=1,n,2<br />

s=s+(h/3)*(y(i)+4*y(i+1)+y(i+2))<br />

enddo<br />

print*,'Integration by Simpsons 1/3 Rule=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Integration by Simpson's 1/3 rule= 4.021834 E-01<br />

36


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

1 dt<br />

∫<br />

using Simpson’s 3/8 rule with n = 6<br />

0 2<br />

2<br />

( t + 1) (3t<br />

+ 4)<br />

! program Simpson's 3/8 Rule<br />

dimension y(0:10)<br />

f(t)=1/((t**2+1)*(3*t**2+4))**.5<br />

a=0<br />

b=1<br />

n=6<br />

h=(b-a)/n<br />

s=0<br />

do i=0,n<br />

x=a+h*i<br />

y(i)=f(x)<br />

enddo<br />

do i=1,(n/2-1)<br />

s=s+y(3*i-3)+3*(y(3*i-2)+y(3*i-1))+y(3*i)<br />

enddo<br />

s=(3*h/8)*s<br />

print*,'Integration by Simpsons 3/8 Rule=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Integration by Simpson’s 3/8 rule= 4.021832 E-01<br />

37


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

1 dt<br />

∫<br />

using Simpson’s 3/8 rule with n = 6<br />

0 2<br />

2<br />

( t + 1) (3t<br />

+ 4)<br />

! program Simpson's 3/8 Rule<br />

dimension y(0:10)<br />

f(t)=1/((t**2+1)*(3*t**2+4))**.5<br />

a=0<br />

b=1<br />

n=6<br />

h=(b-a)/n<br />

s=0<br />

do i=1,n+1<br />

x=a+h*(i-1)<br />

y(i)=f(x)<br />

enddo<br />

do i=1,n,3<br />

s=s+(3*h/8)*(y(i)+3*y(i+1)+3*y(i+2)+y(i+3))<br />

enddo<br />

print*,'Integration by Simpsons 3/8 Rule=',s<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

Integration by Simpson’s 3/8 rule= 4.021832 E-01<br />

38


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

∫ 0.<br />

8<br />

0<br />

e<br />

t 2 dt<br />

with error <strong>of</strong> order<br />

using Trapezium rule and Romberg integration<br />

8<br />

h .<br />

dimension y(20),trap(20),romb(20,20)<br />

f(t)=exp(t**2)<br />

a=0; b=0.8; m=4<br />

do k=1,m<br />

n=2**k<br />

h=(b-a)/n<br />

do i=1,n+1<br />

x=a+h*(i-1)<br />

y(i)=f(x)<br />

enddo<br />

t=0<br />

do i=1,n<br />

t=t+h*(y(i)+y(i+1))/2<br />

enddo<br />

trap(k)=t<br />

print*,t<br />

enddo<br />

print*,'---------------------------'<br />

fact=2**2<br />

do k=1,m-1<br />

fact=fact**k<br />

do j=1,m-k<br />

trap(j)=(fact*trap(j+1)-trap(j))/(fact-1)<br />

romb(k,j)=trap(j)<br />

print*,romb(k,j)<br />

enddo<br />

print*,'---------------------------'<br />

enddo<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

39


1.048701<br />

1.019178<br />

1.011646<br />

1.009753<br />

--------------------------------------<br />

1.009338<br />

1.009135<br />

1.009122<br />

--------------------------------------<br />

1.009122<br />

1.009121<br />

--------------------------------------<br />

1.009121<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

40


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

∫ 0.<br />

8<br />

0<br />

e<br />

t 2 dt<br />

with error <strong>of</strong> order<br />

dimension trap(20,20)<br />

f(t)=exp(t**2)<br />

a=0; b=0.8; n=2; tol=1e-8<br />

h=(b-a)/n<br />

sum=(f(a)+f(b))/2<br />

do i=1,n-1<br />

sum=sum+f(a+i*h)<br />

enddo<br />

trap(1,1)=h*sum<br />

print*,trap(1,1)<br />

j=1<br />

5 do k=0,n-1<br />

sum=sum+f(a+k*h+h/2)<br />

enddo<br />

n=2*n<br />

j=j+1<br />

factor=1<br />

h=h/2<br />

trap(1,j)=h*sum<br />

print*,trap(1,j)<br />

using Trapezium rule and Romberg integration<br />

8<br />

h .<br />

do i=1,j-1<br />

factor=2**2*factor<br />

trap(i+1,j)=(factor*trap(i,j)-trap(i,j-1))/(factor-1)<br />

enddo<br />

if (abs(trap(j,j)-trap(j-1,j)).lt.tol) goto 15<br />

goto 5<br />

15 print*,'+++++++++++++++++++++++++++'<br />

do k=1,j<br />

do i=1,k<br />

print*,trap(i,k)<br />

enddo<br />

print*,'---------------------------'<br />

enddo<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

41


1.048701<br />

1.019178<br />

1.011646<br />

1.009753<br />

+++++++++++++++++++++<br />

1.048701<br />

--------------------------------------<br />

1.019178<br />

1.009338<br />

--------------------------------------<br />

1.011646<br />

1.009135<br />

1.009122<br />

--------------------------------------<br />

1.009753<br />

1.009122<br />

1.009121<br />

1.009121<br />

--------------------------------------<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

42


Problem: Write a <strong>Fortran</strong> program to find the value <strong>of</strong> the integral<br />

∫ 0.<br />

8<br />

0<br />

e<br />

t 2 dt<br />

with error <strong>of</strong> order<br />

using Simpson’s 1/3 rule and Romberg integration<br />

8<br />

h .<br />

dimension y(20),simps(20),romb(20,20)<br />

f(t)=exp(t**2)<br />

a=0; b=0.8; m=4<br />

do k=1,m<br />

n=2**k<br />

h=(b-a)/n<br />

do i=1,n+1<br />

x=a+h*(i-1)<br />

y(i)=f(x)<br />

enddo<br />

s=0<br />

do i=1,n,2<br />

s=s+(h/3)*(y(i)+4*y(i+1)+y(i+2))<br />

enddo<br />

simps(k)=s<br />

print*,s<br />

enddo<br />

print*,'---------------------------'<br />

fact=2**2<br />

do k=1,m-1<br />

fact=fact**k<br />

do j=1,m-k<br />

simps(j)=(fact*simps(j+1)-simps(j))/(fact-1)<br />

romb(k,j)=simps(j)<br />

print*,romb(k,j)<br />

enddo<br />

print*,'---------------------------'<br />

enddo<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

43


1.012070<br />

1.009338<br />

1.009135<br />

1.009122<br />

--------------------------------------<br />

1.008427<br />

1.00<strong>90</strong>67<br />

1.009117<br />

--------------------------------------<br />

1.009110<br />

1.009121<br />

--------------------------------------<br />

1.009121<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

44


Problem:<br />

Write a <strong>Fortran</strong> program to find first, second, third, and fourth<br />

3<br />

derivation <strong>of</strong> the function f ( x)<br />

= ln( x ) at x = 1 and h = 0. 01<br />

! Derivation using Different Formula<br />

f(x) = log(x**3)<br />

x=1<br />

h=0.01<br />

! df: Forward, db: Backward, dc: Central<br />

df=(f(x+h)-f(x))/h<br />

db=(f(x)-f(x-h))/h<br />

dc=(f(x+h)-f(x-h))/(2*h)<br />

print*,'Forward derivation is', df<br />

print*,'Backward derivation is', db<br />

print*,'Central derivation is', dc<br />

print*,'-----------------------------------------'<br />

! d3p: Three Point, d5p: Five Point<br />

d3p=(f(x+h)-f(x-h))/(2*h)<br />

d5p=(-1* f(x+2*h)+8*f(x+h)-8*f(x-h)+f(x-2*h))/(12*h)<br />

print*,'Three Point derivation is', d3p<br />

print*,'Five Point derivation is', d5p<br />

print*,'-----------------------------------------'<br />

! d2: Second derivation, d3: Third Derivation, d4: Fourth Derivation"<br />

d2=(f(x+h)-2*f(x)+f(x-h))/h**2<br />

d3=(f(x+2*h)-2*f(x+h)+2*f(x-h)-f(x-2*h))/(2*h**3)<br />

d4=(f(x+2*h)-4*f(x+h)+6*f(x)-4*f(x-h)+f(x-2*h))/h**4<br />

print*,'Second derivation is', d2<br />

print*,'Third derivation is ', d3<br />

print*,'Fourth derivation is ', d4<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

45


Forward derivation is 2.985099<br />

Backward derivation is 3.015098<br />

Central derivation is 3.000097<br />

-------------------------------------------------------------------<br />

Three Point derivation is 3.000097<br />

Five Point derivation is 2.999997<br />

-------------------------------------------------------------------<br />

Second derivation is -3.000144<br />

Third derivation is 6.001784<br />

Fourth derivation is -18.005940<br />

46


Problem: Write a <strong>Fortran</strong> program to find first derivation from the<br />

following data using derivation <strong>of</strong> Newton Forward<br />

polynomial.<br />

x 1 2 3 4 5 6 7<br />

y -4 -1 10 35 80 151 254<br />

! Derivative from Newton Forward<br />

dimension x(10),y(10),d(10,10)<br />

n=7<br />

data (x(i), i=1,7)/1,2,3,4,5,6,7/<br />

data (y(i), i=1,7)/-4,-1,10,35,80,151,254/<br />

!print*,(x(i), i=1,7)<br />

!print*,(y(i), i=1,7)<br />

do i=1,n-1<br />

do j=1,n-i<br />

y(j)=y(j+1)-y(j)<br />

d(i,j)=y(j)<br />

enddo<br />

enddo<br />

s=0<br />

do i=1,n-1<br />

s=s+d(i,1)/i*(-1)**(i+1)<br />

enddo<br />

fd=s/(x(2)-x(1))<br />

print*,'First derivation is', fd<br />

end<br />

3 2<br />

y = x − 2 x + 2 x − 5<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

The First Derivation= 1.000000<br />

47


Problem: Write a <strong>Fortran</strong> program to find first derivation from the<br />

following data at x = 0. 1 using derivation <strong>of</strong> Lagrange<br />

polynomial.<br />

x 0.1 0.2 0.3 0.4<br />

y 0.01 0.04 0.09 0.16<br />

! Derivation using Lagrange Formula<br />

dimension x(40),y(40)<br />

n=4<br />

xx=0.1<br />

data (x(i), i=1,4)/.1,.2,.3,.4/<br />

data (y(i), i=1,4)/.01,.04,.09,.16/<br />

d=0<br />

do i=1,n<br />

s=0<br />

do j=1,n<br />

if (j.eq.i) goto 10<br />

p=1<br />

do k=1,n<br />

if ((k.eq.j).or.(k.eq.i)) goto 20<br />

p=p*(xx-x(k))/(x(i)-x(k))<br />

20 enddo<br />

s=s+p/(x(i)-x(j))<br />

10 enddo<br />

d=d+s*y(i)<br />

enddo<br />

print*,'The First Derivation=',d<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

The First Derivation= 2.000000E-01<br />

48


Problem: Write a <strong>Fortran</strong> program to find first derivation <strong>of</strong> the function<br />

2 3<br />

f ( x)<br />

= ln(5x<br />

) + 8x<br />

−8<br />

at x =1. 1 using derivation <strong>of</strong> Lagrange polynomial.<br />

! Derivation using Lagrange Formula<br />

f(x)=log(5*x**2)+8*x**3-8<br />

n=4<br />

xo=1.1<br />

h=.1<br />

d=0<br />

do i=1,n<br />

s=0<br />

do j=1,n<br />

if (j.eq.i) goto 10<br />

p=1<br />

do k=1,n<br />

if ((k.eq.j).or.(k.eq.i)) goto 20<br />

p=p*(1-k)/(i-k)<br />

20 enddo<br />

s=s+p/((i-j)*h)<br />

10 enddo<br />

d=d+s*f(xo+h*(i-1))<br />

enddo<br />

print*,'The First Derivation=',d<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

The First Derivation= 30.8567<strong>90</strong><br />

49


Problem: Write a <strong>Fortran</strong> program to find first derivation <strong>of</strong> the function<br />

2 3<br />

f ( x)<br />

= ln(5x<br />

) + 8x<br />

−8<br />

at x =1. 1 using derivation <strong>of</strong> Lagrange polynomial.<br />

! Derivation using Lagrange Formula<br />

f(x)=log(5*x**2)+8*x**3-8<br />

n=4<br />

xo=1.1<br />

h=.1<br />

d=0<br />

do i=1,n<br />

s=0<br />

do j=1,n<br />

if (j.eq.i) goto 10<br />

p=1<br />

do k=1,n<br />

if ((k.eq.j).or.(k.eq.i)) goto 20<br />

p=p*(-h*(k-1))/(h*(i-1)-h*(k-1))<br />

20 enddo<br />

s=s+p/(h*(i-1)-h*(j-1))<br />

10 enddo<br />

d=d+s*f(xo+h*(i-1))<br />

enddo<br />

print*,'The First Derivation=',d<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

The First Derivation= 30.8567<strong>90</strong><br />

50


Problem: Write a <strong>Fortran</strong> program to find second derivation <strong>of</strong> the<br />

function:<br />

2 3<br />

f ( x)<br />

= ln(5x<br />

) + 8x<br />

−8<br />

at x =1. 1 using derivation <strong>of</strong> Lagrange polynomial.<br />

! Second Derivation using Lagrange Formula<br />

f(x)=log(5*x**2)+8*x**3-8<br />

n=4<br />

xo=1.1<br />

h=.1<br />

d=0<br />

do i=1,n<br />

s=0<br />

p=1<br />

do j=1,n<br />

if (j.eq.i) goto 10<br />

s=s+(-h*(j-1))<br />

p=p*(i-j)*h<br />

10 enddo<br />

d=d+2*s/p*f(xo+h*(i-1))<br />

enddo<br />

print*,'The Second Derivation=',d<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

The Second Derivation= 51.200130<br />

51


Problem: Write a program in <strong>Fortran</strong> to solve the differential equation<br />

′<br />

−<br />

2 x<br />

y = e − 2 y using Euler method over [ 0,2]<br />

, with y ( 0) = 0. 1,<br />

let h = 0. 1 (i.e. n = 20).<br />

! Euler's Method<br />

dimension x(0:30), y(0:30)<br />

a=0<br />

b=2<br />

n=20<br />

y(0)=0.1<br />

x(0)=a<br />

h=(b-a)/n<br />

do i=0,n-1<br />

x(i+1)=x(i)+h<br />

y(i+1)=y(i)+h*(exp(-2*x(i))-2*y(i))<br />

enddo<br />

do i=0,n<br />

print*,x(i),y(i)<br />

enddo<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

52


0 1.000000 E-01<br />

0.1 1.800000 E-01<br />

0.2 2.258731 E-01<br />

0.3 2.477305 E-01<br />

0.4 2.530655 E-01<br />

0.5 2.473853 E-01<br />

0.6 2.346962 E-01<br />

0.7 2.178764 E-01<br />

0.8 1.989608 E-01<br />

0.9 1.793583 E-01<br />

1 1.600165 E-01<br />

1.1 1.415467 E-01<br />

1.2 1.243177 E-01<br />

1.3 1.085260 E-01<br />

1.4 9.424812E-02<br />

1.5 8.147950 E-01<br />

1.6 7.016230 E-01<br />

1.7 6.020606 E-02<br />

1.8 5.150217 E-02<br />

1.9 4.393411E-02<br />

2 3.738436 E-02<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

−2<br />

x<br />

Exact solution for<br />

y′ = e − 2 y<br />

is:<br />

1 −2<br />

x − 2 x<br />

y = e − x e ⇒ y ( 2) = 0. 03846284<br />

10<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

53


Problem: Write a program in <strong>Fortran</strong> to solve the differential equation<br />

′<br />

−<br />

2 x<br />

y = e − 2 y using Runge-Kutta method over [ 0,2]<br />

, with<br />

y ( 0) = 0.1, let n = 20.<br />

! Runge Kutta Method<br />

real k1,k2,k3,k4<br />

dimension x(0:25),y(0:25)<br />

a=0<br />

b=2<br />

n=20<br />

x(0)=a<br />

y(0)=0.1<br />

h=(b-a)/n<br />

do i=0,n-1<br />

xo=x(i)<br />

yo=y(i)<br />

k1=h*(exp(-2*xo)-2*yo)<br />

xo=x(i)+h/2<br />

yo=y(i)+k1/2<br />

k2=h*(exp(-2*xo)-2*yo)<br />

xo=x(i)+h/2<br />

yo=y(i)+k2/2<br />

k3=h*(exp(-2*xo)-2*yo)<br />

xo=x(i)+h<br />

yo=y(i)+k3<br />

k4=h*(exp(-2*xo)-2*yo)<br />

y(i+1)=y(i)+1./6*(k1+2*k2+2*k3+k4)<br />

x(i+1)=x(i)+h<br />

enddo<br />

do i=0,n<br />

print*,x(i),y(i)<br />

enddo<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

54


0 1.000000 E-01<br />

0.1 1.637440 E-01<br />

0.2 2.010928 E-01<br />

0.3 2.195209 E-01<br />

0.4 2.246607 E-01<br />

0.5 2.207241 E-01<br />

0.6 2.108327 E-01<br />

0.7 1.972747 E-01<br />

0.8 1.817045 E-01<br />

0.9 1.652969 E-01<br />

1 1.488672 E-01<br />

1.1 1.329626 E-01<br />

1.2 1.179324 E-01<br />

1.3 1.039823 E-01<br />

1.4 9.121463 E-02<br />

1.5 7.965<strong>90</strong>2 E-02<br />

1.6 6.929560 E-02<br />

1.7 6.007185 E-02<br />

1.8 5.191512 E-02<br />

1.9 4.474165 E-02<br />

2 3.846299 E-02<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

−2<br />

x<br />

Exact solution for y′ = e − 2 y is:<br />

1 −2<br />

x − 2 x<br />

y = e − x e ⇒ y ( 2) = 0. 03846284<br />

10<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

55


Problem:<br />

Write a program in <strong>Fortran</strong> to solve the differential equation y'<br />

= −x<br />

using Taylor series method for values x<br />

o<br />

= 0. 0, x = 0. 1<br />

1 , x = 0. 2,<br />

2 x = 0.3 , x = 0. 4,<br />

and 5<br />

3 4<br />

x = 0. 5 , with the initial condition y ( 0) = 1 .<br />

y<br />

! Taylor Series Method<br />

dimension x(0:25),y(0:25)<br />

x(0)=0<br />

y(0)=1<br />

h=.1<br />

n=5<br />

do i=0,n-1<br />

d1=-1*x(i)*y(i)<br />

d2=(x(i)**2-1)*y(i)<br />

d3=(-1*x(i)**3+3*x(i))*y(i)<br />

d4=(x(i)**4-6*x(i)**2+3)*y(i)<br />

y(i+1)=y(i)+h*d1+h**2/2*d2+h**3/6*d3+h**4/24<br />

x(i+1)=x(i)+h<br />

enddo<br />

do i=0,n<br />

print*,x(i),y(i)<br />

enddo<br />

end<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

0 1.000000<br />

0.1 9.950042 E-01<br />

0.2 9.801826 E-01<br />

0.3 9.559749 E-01<br />

0.4 9.230893 E-01<br />

0.5 8.824676 E-01<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

2<br />

x<br />

−<br />

2<br />

0.125<br />

Exact solution is: y = e ⇒ y (0.5) = e<br />

− = 0. 08824969<br />

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br />

56

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!