Académique Documents
Professionnel Documents
Culture Documents
Numerical Analysis
BS (CS)-6
Submitted to: Sir Mohsin
Submitted by
(Muddasar yamin) 2010-arid-1880
Barani institute of information technology,
University of ARID Agriculture,
6th road, Rawalpindi
Bisection Method
> A:=proc(f,x0,x1,t,n)
local a,b,x,i;
a:=x0;b:=x1;
if evalf(f(a)*f(b))>0 then
print (" THE BISECTION METHOD IS NOT APPLICABLE");
else
for i from 1 to n do
x:=evalf((a+b)/2);
print(x);
if abs(evalf(f(x)))<t then
print ("REQUIRED ROOT=",x);
break;
end if;
if evalf(f(a)*f(x))<0 then
b:=x;
elif evalf(f(x)*f(b))<0 then
a:=x;
end if;
end do;
end if;
end proc:
> A(x->x^2-2,1,2,.00001,20);
1.500000000
1.250000000
1.375000000
1.437500000
1.406250000
1.421875000
1.414062500
1.417968750
1.416015625
1.415039062
1.414550781
1.414306640
1.414184570
1.414245605
1.414215088
"REQUIRED ROOT=" , 1.414215088
1.414215088
> A(x->x^2-2,1,2,.00001,20);
1.500000000
1.250000000
1.375000000
1.437500000
1.406250000
1.421875000
1.414062500
1.417968750
1.416015625
1.415039062
1.414550781
1.414306640
1.414184570
1.414245605
1.414215088
"REQUIRED ROOT=" , 1.414215088
1.414215088
> A(x->x^2-2,1,2,.00001,20);
1.500000000
1.250000000
1.375000000
1.437500000
1.406250000
1.421875000
1.414062500
1.417968750
1.416015625
1.415039062
1.414550781
1.414306640
1.414184570
1.414245605
1.414215088
"REQUIRED ROOT=" , 1.414215088
1.414215088
>
1.500000000
Eulers Method
> Eul:=proc(f,a,w0,h,b)
local i,n,w,x;
n:=eval((b-a)/h);
w[0]:=w0;
x[0]:=a;
for i from 1 to n do
w[i]:=evalf(w[i-1]+h*f(x[i-1],w[i-1]));
x[i]:=x[i-1]+h;
print(i,"...",x[i],"...",w[i]);
end do;
end proc;
Eul := proc(f, a, w0, h, b)
for i to n do
local i, n, w, x;
w[ i ] := evalf( w[ i 1 ] h f( x[ i 1 ], w[ i 1 ] ) ) ;
n := eval( ( b a )/h ) ;
x[ i ] := x[ i 1 ] h;
w[ 0 ] := w0;
print ( i, "..." , x[ i ], "..." , w[ i ] )
x[ 0 ] := a;
end do
end proc
> Eul((x,y)->(3*y*x^2-y^2+3*x^4),0,10,1,5);
1, "...", 1, "...", -90.
10 16
10 32
> Eul((x,y)->(3*y*x^2-y^2+3*x^4),0,10,0.001,5);
1, "...", 0.001, "...", 9.900
> ?while
> fixpoint(x->x^2+1,x->2*x,2.5,0.002,10);
"p1", 1.050000000
Newton Method
> NewtonM:=proc(f,df,p0,tol,n)
local p1,i,g0,st,pi;
p1:=evalf(p0-evalf(f(p0)/df(p0)));
print("p1",p1);
for i from 1 to n do
pi:=evalf(p1-evalf(f(p1)/df(p1)));
print("iteration=",i,"....",pi);
st:=p1-p0;
if(st<0)then
print("output",pi);
end if;
p1:=pi;
end do;
end proc;
NewtonM := proc(f, df, p0, tol, n)
local p1, i, g0, st , ;
p1 := evalf( p0 evalf( f( p0 )/df( p0 ) ) ) ;
print ( "p1" , p1 ) ;
for i to n do
:= evalf( p1 evalf( f( p1 )/df( p1 ) ) ) ;
end do
print ( "iteration=" , i, "...." , ) ;
end proc
st := p1 p0;
if st 0 then print ( "output", ) end if;
p1 :=
> NewtonM(x->x^2+1,x->2*x,2.5,0.0003,10);
"p1", 1.050000000
> Q1:=proc(x0,x1,x2,x3,x4,f0,f1,f2,f3,f4)
local fx0,fx2,fx4,fx1,fx3,h;
h:=x1-x0;
fx0:=evalf(1/12*h*((-25*f0+48*f1-36*f2+16*f3-3*f4)));
fx1:=evalf(1/2*h*(-f1+f3));
fx2:=evalf(1/2*h*((3*f0-4*f1+f2)));
fx3:=evalf(1/2*h*((3*f1-4*f2+f3)));
fx4:=evalf(1/12*h*((25*f0-48*f1+36*f2-16*f3+3*f4)));
print(fx0);
print(fx1);
print(fx2);
print(fx3);
print(fx4);
end proc:
>
Q1(1.8,1.9,2.0,2.1,2.2,10.889365,12.703199,14.778112,17.148957,1
9.855030);
0.1693801417
0.2222879000
-0.1683294500
-0.1926947000
-0.1693801417
>
Regula Falsi
> RegulaFalsi:=proc(f,p0,p1,tol,n)
local i,q0,q1,p,q,s0,s1;
s0:=p0;
s1:=p1;
q0:=f(p0);
q1:=f(p1);
for i from 2 to n do
p:=evalf(s1-q1*(s1-s0)/(q1-q0));
if((p-s1)<tol)then
print("p",p);
break;
end if;
q:=f(p);
if((q*q1)<0)then
s0:=s1;
q0:=q1;
end if;
s1:=p;
q1:=q;
end do;
end proc:
> RegulaFalsi(x->(-x)^3-cos(x),4,7,0.00002,18);
"p", 3.322275481
3.322275481
>
RK2
> RK2:=proc(f,a,y0,h,b)
local i,n,y,x,k1,k2;
n:=eval((b-a)/h);
print(n);
y[0]:=y0;
x[0]:=a;
for i from 1 to n do
k1:=evalf(f(x[i-1],y[i-1]));;
k2:=evalf(f(x[i-1]+h,y[i-1]+h*k1));
y[i]:=evalf(y[i-1]+h*(k1+k2)/2);
x[i]:=x[i-1]+h;
print(i,"...",x[i],"...",y[i]);
end do;
end proc:
> RK2((x,y)->y+x^2+1,0,0.5,0.0025,2);
800.0000000
> RK4:=proc(f,a,y0,h,b)
local i,n,y,x,k1,k2,k3,k4;
n:=eval((b-a)/h);
print(n);
y[0]:=y0;
x[0]:=a;
for i from 1 to n do
k1:=evalf(f(x[i-1],y[i-1]));
k2:=evalf(f(x[i-1]+h/2,y[i-1]+k1/2));
k3:=evalf(f(x[i-1]+h/2,y[i-1]+k2/2));
k4:=evalf(f(x[i-1]+h,y[i-1]+k3));
y[i]:=evalf(y[i-1]+(h*(k1+2*k2+2*k3+k4)/6));
x[i]:=x[i-1]+h;
print(i,"...",x[i],"...",y[i]);
end do;
end proc:
> RK4((x,y)->y+x^2+1,0,0.5,0.025,2);
80.00000000
1, "...", 0.025, "...", 0.5640693359
2, "...", 0.050, "...", 0.6309237945
3, "...", 0.075, "...", 0.7007357092
4, "...", 0.100, "...", 0.7736847734
5, "...", 0.125, "...", 0.8499583549
6, "...", 0.150, "...", 0.9297518237
7, "...", 0.175, "...", 1.013268894
8, "...", 0.200, "...", 1.100721980
9, "...", 0.225, "...", 1.192332567
10, "...", 0.250, "...", 1.288331601
11, "...", 0.275, "...", 1.388959891
12, "...", 0.300, "...", 1.494468530
13, "...", 0.325, "...", 1.605119334
14, "...", 0.350, "...", 1.721185303
15, "...", 0.375, "...", 1.842951095
16, "...", 0.400, "...", 1.970713525
17, "...", 0.425, "...", 2.104782084
18, "...", 0.450, "...", 2.245479483
19, "...", 0.475, "...", 2.393142214
20, "...", 0.500, "...", 2.548121139
21, "...", 0.525, "...", 2.710782107
22, "...", 0.550, "...", 2.881506590
23, "...", 0.575, "...", 3.060692353
24, "...", 0.600, "...", 3.248754149
25, "...", 0.625, "...", 3.446124443
26, "...", 0.650, "...", 3.653254172
27, "...", 0.675, "...", 3.870613530
28, "...", 0.700, "...", 4.098692793
> A:=matrix(3,4,[2.11,-4.21,0.921,2.01,4.01,10.2,-1.12,3.09,1.09,0.987,0.832,4.21]);
>
2.01
2.11 -4.21 0.921
10.2
-1.12 -3.09
A := 4.01
4.21
1.09 0.987 0.832
> A[1,2];
-4.21
> s1:=max(abs(A[1,1]),abs(A[1,2]),abs(A[1,3]));
s1 := 4.21
> s1:=max(seq(abs(A[1,i]),i=1..3));
s1 := 4.21
> ss[1]:=abs(A[1,1]/s1);
ss 1 := 0.5011876485
> s2:=max(seq(abs(A[2,i]),i=1..3));
s2 := 10.2
> ss[2]:=abs(A[2,1]/s2);
ss 2 := 0.3931372549
> s3:=max(seq(abs(A[3,i]),i=1..3));
s3 := 1.09
> ss[3]:=abs(A[3,1]/s3);
ss 3 := 1.000000000
> matrix(A);
2.11
4.01
1.09
-4.21
10.2
0.987
0.921
-1.12
0.832
2.01
-3.09
4.21
> B:=swaprow(A,1,3);
1.09
B := 4.01
2.11
0.987
10.2
-4.21
0.832
-1.12
0.921
4.21
-3.09
2.01
0.987
10.2
-4.21
0.832
-1.12
0.921
4.21
-3.09
2.01
> with(linalg):
> A1:=swaprow(A,1,3);
1.09
A1 := 4.01
2.11
> A2:=addrow(A1,1,2,-4.01/1.09);
#to which divid, to be
divided/ans saved,
0.987
0.832
4.21
1.09
6.568926605
-4.180844037
-18.57816514
A2 := 0.
-4.21
0.921
2.01
2.11
> A3:=addrow(A2,1,3,-A2[3,1]/A2[1,1]);
1.09
A3 := 0.
0.
0.987
6.568926605
-6.12
0.832
-4.180844037
-0.689
4.21
-18.57816514
-6.16
> Digits:=17;
Digits := 17
> s3:=max(seq(abs(A3[2,i]),i=2..3));
s3 := 6.568926605
> s4:=max(seq(abs(A3[3,i]),i=2..3));
s4 := 6.12
> ss[3]:=abs(A3[2,2]/s3);
ss 3 := 1.0000000000000000
> ss[4]:=abs(A3[3,2]/s4);
ss 4 := 1.0000000000000000
> A4:=addrow(A3,2,3,-(A3[3,2]/A3[2,2]));
0.987
0.832
1.09
6.568926605
-4.180844037
A4 := 0.
0.
-4.5841212344136093
0.
4.21
-18.57816514
-23.468515910385940
> x:=backsub(A4);
x := [ -0.43488306658589284, 0.43017130392974302, 5.1195233961538063 ]
>
Secant Method
> secentM(0,2,x->x^2+1,0.00002,10);
"iteration ", 2, "....", -0.5000000000
"stop", -0.5000000000
"iteration ", 3, "....", 1.833333333
"iteration ", 4, "....", -2.917162698
"stop", -2.917162698
"iteration ", 5, "....", 19.90561909
"iteration ", 6, "....", -1159.283481
"stop", -1159.283481
"iteration " , 7, "...." , 0.2675194192
10 8
10 18
"stop", -0.8296602314
10 18
10 44
10 105
10 105
10 209
>
Taylor order 2
> tM2:=proc(f,f1,a,y0,h,b)
local i,n,y,x;
n:=eval((b-a)/h);
y[0]:=0;
x[0]:=a;
for i from 1 to n do
y[i]:=evalf(f(x[i-1],y[i-1])+(h*f1(x[i-1],y[i-1]))/2);
x[i]:=x[i-1]+h;
print(i,"...",x[i],"...",y[i]);
end do;
end proc;
> tM2((x,y)->y-x^2+1,(x,y)->(-2)*x,0,0.5,0.2,2);
1, "...", 0.2, "...", 1.100000000
> tM4:=proc(f,f1,f2,f3,a,y0,h,b)
local i,n,y,x;
n:=eval((b-a)/h);
y[0]:=0;
x[0]:=a;
for i from 1 to n do
y[i]:=evalf(f(x[i-1],y[i-1])+(h*f1(x[i-1],y[i1]))/2+(h^2*f2(x[i-1],y[i-1]))/6+(h^3*f(x[i-1],y[i-1]))/24);
x[i]:=x[i-1]+h;
print(i,"...",x[i],"...",y[i]);
end do;
end proc;
> tM4((x,y)->y-x^2+1,(x,y)->(-2)*x,(x,y)->(-2),(x,y)>0,0,0.5,0.2,2);
1, "...", 0.2, "...", 0.9870000000
> N1:=proc(f,a,b)
local t,s,e1,ht,hs,x1;
e1:=evalf(int(f(x),x=a..b));
print("Exact Value is:",e1);
ht:=evalf(a-b);
t:=evalf(h/2*f(a)+f(b));
print("trapezidal:",t);
hs:=evalf((a+b)/2);
x1:=(a+hs);
s:=evalf(hs/3*(f(a)+4*f(x1)+f(b)));
print("simpsons:",s);
end proc:
>
> N1(x->x^2,0,2);
1 3
x
3
>
>
Five Point
>
myproc(1.9,2.0,2.0,2.1,2.2,10.232,12.121,14.1212,17.1212,19.1212
);
"X4", 0.1833400000
"X3", 1.519626667
"X2", 0.2851700000
"X1", -0.09445000000
"X0" , -0.1833400000