1.bisection Method
1.bisection Method
BISECTION METHOD
function[x,err,xx]=bisection(f,a,b,toler,maxiter)
clc
close all
% f denote the input function
% maxiter denote maximum no of iterations
% toler denote the maximum tollerance error
% a denote the left end of the interval
%b denote the right end of the interval
X1=a;
X2=b;
fa=feval (f,a);fb=feval(f,b)
If
fa*fb>0
fprintf(‘there is no root lies between %-5.2f’,a,b)
else
for k=1:maxiter
xx(k)=(a+b)/2
fx=feval(f,xx(k))
err=(b-a)/2
if abs(fx)<tolfun || abs(fx)<toler
break
else if fx*fa>0
a=xx(k); fa=fx
else
b=xx(k)
end end end
if k==maxiter
fprintf(“the maximum number of iteration is executed”)
end
x=xx(k);
Z=feval(f,x);
Xh=[x1:0.05:x2];
Y=feval(f,xh);hold on
plot(xh,y)
plot(x,fx,’y*’) end
1
Q.Find the root of the function f(x)=x^3-x-1 upto 3 decimal places using bisection
method?.
Ans=
f=@(x) x. ^3-x-1
bisection(f, 1,2,0.001.100)
=1.3247
2
2.FIXED POINT METHOD
function[x,err,xx]=fixed(g,x0,tolerr,maxiter)
% g is the function by rewritting f(x) =0 of the form x=g(x)
% this method will only work when |g’(x)|<1
% maxiter denotesmaximum number of iteration
clc
close all
if nargin <4
maxiter=100;
end
if nargin<3
tolerr=10^-6;
end
xx(1)=x0;
for k =2:maxiter
xx(k)=feval(g,xx(k-1));
if abs(xx(k)-xx(k-1))<tolerr
break
end
end
x=xx(k);
if k == maxiter
fprintf(‘the maximum number of iteration is executed ‘)
end
xh=[-100:0.005:100]
y=feval(f,xh);
z=feval(f,x);
hold on
plot(xh,y)
plot(x,z,’r*’)
end
3
Q. Find the approximate root of the equation x^3+4x^2-10=0 upto 30 iteration choose
initial approximation x0=1.5 upto 4 decimla places.
>>g=@(x)(1/2)*(10-x. ^3).^(1/2)
>>fixed(g, 1.5,0.00001,100)
Ans=1.3652
4
3.REGULA FALSI METHOD.
function [x,err,xx] = rf(f,a,b,tolerr,maxiter)
clc
close all
if nargin <5 maxiter = 100
end
if nargin <4tolerr=10^(-8)
end
fa = feval(f,a);
fb=feval(f,b);
if f(a)*f(b)>0
fprintf(‘there is no root lies between %d and % d’,a,b)
end
for k=1:maxiter
xx(k)=b-((fb*(b-a))/(fb-fa));
fx=feval(f,xx(k));
if abs(fx-fb)<tolerr ||abs(fx-fa)<tolerr
break
end
if fx*fa<0
b=xx(k);fb=fx;
else
a=xx(k); fa=fx;
end end
x=xx(k);
if k==maxiter
fprintf(‘the maximum number of iteration is executed and the root of f(x) is %d’,x)
fprintf(‘the root of given f(x) is % d achieved in %d iteration ‘,x,k)
else
fprintf(‘the root of the given f(x) is % d achived in % d iteration’,x,k)
end
xh= [-100:0.05:100];
y=feval(f,xh);
z=feval(f,x)
plot(xh,y);
hold on
plot(x,z’r*’) end
5
Q. Find the root of the function f(x)=cosx- x by using regular falsi Method
>> f=@(x) cos(x)-x
F=
@(x)cos(x)-x
>>falseposition(0,pi/4,10^(-8),100)
6
4.Newton Raphson Method
7
Q. Find the root of the function f(x)=cosx- x by using Newton raphson Method
>> f=@(x) cos(x)-x
F=
@(x)cos(x)-x
>>nr(0,pi/4,10^(-8),100)
The root of the given f(x) is 7.390851e-01 achived in 4 iteration
Ans =
0.7391
8
5.Secant Method
function [x,err, xx] = secant(f,x0,tolerr,maxiter,varargin)
clc
close all
H=1e-4;
H2=2*h;
Tolfun=eps;
Xx(1)=x0;
Fx=feval(f,x0,varargin{:});
For k=1:maxiter
If k<=1
Dfdx=(feval(f,xx(k)+h,varargin{:})-feval(f,xx(k)-h,varargin{:}))/h2;
Else
Dfdx=(fx-fx0)/dx;
End
Dx=(-fx)/dfdx;
Xx(k+1)=xx(k)+dx;
Fx0=fx;
Fx=feval(f,xx(k+1));
If abs(fx)<tolfun ||abs(dx)<tolerr
Break;
End
End
X=xx(k+1);
If k==maxiter
Fprintf(‘The maximum number of iteration is executed and the root of f(x) is %d’,x)
Fprintf(‘The root is not within the tolerrance error % d’,tolerr)
Else
Fprintf(‘The root of the given f(x) is % d achived in % d iteration ‘,x,k)
End
Xh=[-100:0.05:100];
Y=feval(f,xh);
Z=feval(f, x)
Plot(xh,y);
Hold on
Plot(x,z,’r*’) End
9
Q. Find the root of the function f(x)=cosx- x by using Newton raphson Method
F=
Function_handle with value:
8.8818e-16
Ans =
0.7391
10
6. Steffensen’s Method
fa=feval(f,a);
fb=feval(f,b);
if fa*fb>0
fprintf(‘there is no root lies between %d and %d’, a,b)
else x0=(a+b)/2;
xx(1)=x0;
for k=1:maxiter
fx0=feval(f,xx(k));
f1x=feval(f,xx(k)+fx0);
xx(k+1)=xx(k)-fx0^2/(f1x-fx0);
fx=feval(f,xx(k+1));
if abs(fx)<tolerr
break;
end
end
x=xx(k)
if k==maxiter
fprintf(‘the maximum number of iteration is executed and the root of f(x) is %d’,x)
fprintf(‘the root of given f(x) is % d achieved in %d iteration ‘,x,k)
else
fprintf(‘the root of the given f(x) is % d achived in % d iteration’,x,k)
end
xh=[-100:0.05:100];
y=feval(f,xh);
z=feval(f,x);
hold on
plot(xh,y)
plot(x,z,’r*’)
end
end
11
Q.Find the root of the equation x^3-x-1 in the interval [1, 2]
>> f=@(x) x.^(3)-x-1
f=
@(x)x.^(3)-x-1
>>sts(f,1,2,10^-4,5)
X=
1.3248
The maximum number of iteration is executed and the root of f(x) is 1.324804e+00The
root of given f(x) is 1.324804e+00 achieved in 5 iteration
Ans =
1.3248
12
7. Gauss jaccobian method
function x = gj(a, b, initialguess, maxiter)
% gjadi: solves a system of linear equations using gauss-jacobi iteration
clc
close all
if nargin < 4
maxiter = 100;
end
if nargin < 3
initialguess = zeros(size(b)); % start from a zero vector if no guess is provided
tolerance = 1e-6;
end
da = size(a,1);
x = initialguess;
at = zeros(da, da);
for m = 1:da
for n = 1:da
if n ~= m
at(m, n) = -a(m, n) / a(m, m);
end
end
b(m,:)= b(m, ☺ / a(m, m); % normalize rows of b
end
for k = 1:maxiter
x_old = x; % store the solution from the previous iteration
x = at * x_old + b;
13
Q.27x+6y-z=85
6x+15y+2z=72
X+y+54z=100
>> A = [27 6 -1; 6 15 2; 1 1 54]
A=
27 6 -1
6 15 2
1 1 54
>>B=[85;72;110]
B=
85
72
100
>>GJ(A,B)
Ans =
2.4119
3.6032
1.7405
14
8. Gaussledon method.
function [x] = gausssledon(a,b,x0,maxiter)
if nargin<4
tol=1e-6;
maxiter=100;
elseif tol==1e-6
end
if nargin<3
x0=zeros(size(b));
end
da=size(a,1);
x=x0;
for k=1:maxiter
x(1,:)=(b(1,:)-a(1,2:da)*x(2:da,:))/a(1,1);
for n=2:da-1
x(n,:)=(b(n,:)-a(n,1:n-1)*x(1:n-1,:)-a(n,n+1:da)*x(n+1:da,:))/a(n,n);
end
x(da,:)=(b(da,:)-a(da,1:da-1)*x(1:da-1,:))/a(da,da);
if norm(x-x0)/norm(x0+eps)< tol
break
x0=x
end
end
if nargout==0
disp(x);
end
end
15
Q.27x+6y-z=85
6x+15y+2z=72
X+y+54z=100
A=
27 6 -1
6 15 2
1 1 54
>>B=[85;72;100]
B=
85
72
100
>>gausseldon(A, B)
Ans =
2.4119
3.6032
1.7405
16
9. Gauss elimination method.
function [x] = guasselimi(a,b)
tic;
clc
close all
na=size(a,1);
[nb1,nb]=size(b);
if nb1~=na
error(‘dimension of a & b are mismatch’)
else
n=na+nb;
ab=[a(1:na,1:na) b(1:na,1:nb)];
epss=eps*ones(na,1);
for k=1:na
[akx,kx]=max(abs(ab(k:na,k))./max(abs([ab(k:na,k+1:na) epss(1:na-k+1)]’))’);
if akx<eps
error(‘singular matrix and no unique solution’)
end
mx=k+kx-1;
if kx>1
temparrow=ab(k,k:n);
ab(k,k:n)=ab(mx,k:n)
ab(mx,k:n)=temparrow;
end
ab(k,k+1:n)=ab(k,k+1:n)/ab(k,k);
ab(k,k)=1;
for m=k+1:na
ab(m,k+1:n)=ab(m,k+1:n)-ab(m,k)*ab(k,k+1:n);
ab(m,k)=0;
end
end
x(na,:)=ab(na,na+1:n);
for m=na-1:-1:1
x(m,:)=ab(m,na+1:n)-ab(m,m+1:na)*x(m+1:na,:);
end
end
toc;
end
17
Q.27x+6y-z=85
6x+15y+2z=72
X+y+54z=110
>> A = [27 6 -1; 6 15 2; 1 1 54]
A=
27 6 -1
6 15 2
1 1 54
>>B=[85;72;110]
B=
85
72
110
>>gausselimi(A,B)
Ans =
2.4255
3.5730
1.9260
18
Forward substitution.
function [x]=forsubs(l,b)
n=size(l,1);
x(1,:)=b(1,:)/l(1,1);
for m=2:n
x(m,:)=(b(m,:)-l(m, 1:m-1)*x(1:m-1,:))/l(m, m);
end
end
Backward substitution.
19
10. LU decomposition.
function [p,l,u,x] = lu(a,b)
na=size(a,1);
ap=[a,eye(na)];
for k=1:na-1
[akx,kx]=max(abs(ap(k:na,k)));
if akx<eps
error(‘the given matrix is singular’)
else
mx=k+kx-1;
if kx>1
temprow=ap(k,:);
ap(k,:)=ap(mx,:);
ap(mx,:)=temprow;
end
for m=k+1:na
ap(m,k)=ap(m,k)/ap(k,k);
ap(m,k+1:na)= ap(m,k+1:na)-ap(m,k)*ap(k,k+1:na);
end end
%pemutation matrix
p=ap(1:na,na+1:na+na)
for m=1:na
for n=1:na
if m==n
l(m,m)=1;
u(m,m)=ap(m,m);
else if m>n
u(m,n)=0;
l(m,n)=ap(m,n);
else
l(m,n)=0;
u(m,n)=ap(m,n);
end end end end
if nargout==0
disp(‘l*u=p*a’);
l,u,p
end
x=backsubst(u,forsubs(l,p*b)) end
20
Q. x1+x2+x3=1
3x1+x2-3x3=5
X1-2x2-5x3=10
>>A=[1 1 1;3 1 -3;1 -2 -5]
A=
1 1 1
3 1 -3
1 -2 -5
>>B=[1;5;10]
B=
1
5
10
>>Lu(A, B)
X=
6.0000
-7.0000
2.0000
Ans =
0 1 0
0 0 1
1 0 0
21
11. Power Method
function[lambda,v]=eigenlar(a,x0,maxiter,toler)
c1a=size(a,2)
if nargin<3
maxiter=100;
toler=1e-8;
end
x=[1:c1a]’;
lambda=0;
x=x0;
for k=1:maxiter
x1=x; lambda1=lambda;
x=a*x/norm(x,inf);
[xm,m]=max(abs(x));
lambda=x(m);
if norm(x-x1)<toler || norm(lambda-lambda1)<toler
break;
end
end
if k==maxiter
fprintf(‘we reached maxiter there is no need to continue’)
end
v=x/norm(x,inf);
a*v
end
22
Q. A= 6 -2 2
-2 3 -1
2 -1 3
and let X0 = 1
C1A =
Ans =
8.0000
23
12. Inverse power method.
function[lambda,v]=invpower(a,x0,maxiter,toler)
s=det(a);
c1a=size(a,2)
if nargin<3
maxiter=100;
toler=1e-8;
end
x0=ones(1,c1a)’;
lambda=0;
x=x0
if s==0
fprintf(‘a is singular’)
fprintf(‘the least eigenvalue in the magnitude sense is zero’)
else
b=a^(-1);
eigenlar(b,x0,maxiter,toler)
lambda1=1/lambda;
end
fprintf(‘the least eigen value of a is %-d’,lambda1);
end
24
Q. A = 6 -2 2
-2 3 -1
2 -1 3
and let X0 = 1
>> A=[6 -2 2 ; -2 3 -1 ; 2 -1 3]
A=
6 -2 2
-2 3 -1
2 -1 3
>>x0= [1;1;1]
X0= 1
1
1
>> invpower (A, x0)
C1A = 3
X=
1
1
1
C1A =3
Ans =
0.5000
25
13.Gershgorin Disc
function[]=gerschgorin(a)
n=size(a,1);
if n~=size(a,2)
fprintf(‘a is not a square matrix’)
else
centre=diag(a);
circler=zeros(n,201);
circlec=circler;
raddic=sum(abs(a)-diag(centre));
one=ones(1,201);
cosisin=exp(1i*[0:pi/100:2*pi]);
xlabel(‘re’);
ylabel(‘im’);
for k=1:n
circlec(k,:)=centre(k)*one+raddic(k)*cosisin;
patch (real(circlec(k,:)),imag(circlec(k,:)),’b’);
hold on
plot(real(circlec(k,:)),imag(circlec(k,:)),’k-‘,real(circlec(k,1)),imag(circlec(k,1)),’kx’);
end
figure(1)
axis image
hold off
end
26
Q.Draw greschgorin disc of the given matrix A = 6 -2 2
-2 3 -1
2 -1 3
>> A=[6 -2 2 ; -2 3 -1 ; 2 -1 3]
A=
6 -2 2
-2 3 -1
2 -1 3
Gerschgorin(A)
27
14. Shifted Power Method.
28
Q. Find the eigen value close to 1 for the given matrix A = 6 -2 2
-2 3 -1
2 -1 3
Given x0 = 1
A=
6 -2 2
-2 3 -1
2 -1 3
>> x0 = [1;1;1]
X0 =
1
1
1
>> Shiftpower(A, 1,x0)
2.0000
29
15. Lagrange Interpolation Method
30
Q.Find f(2) for the given data
x 1 3 5 7
F(x) 24 120 336 720
>> x = [1,3,5,7]
X=
1 3 5 7
>> y=[24,120,336,720]
Y=
24 120 336 720
>> lagrange(x,y)
At which point do you want to find the value.
2
S= 2
fx = 60
The interpolation value is 60.
31
16. Newton Divided Difference.
32
Q.Find f(2) for the given data
X 1 3 5 7
>> x = [1,3,5,7]
X =
1 3 5 7
>> y=[24,120,336,720]
Y=
24 120 336 720
>> nd(x,y)
At which point do you want to find the value.
2
S= 2
Fx = 60
The interpolation value is 60
Ans =
24 48 15 1
120 108 21 0
336 192 0 0 720 0 0 0
33
17. Trapezoidal Rule
function[if]=trap(f,a,b,n)
if b-a<eps || n<=0
error(‘n has to be positive number’)
else
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
end
if = (h/2)*(feval(f,x(1))+feval(f,x(n+1))+2*sum(feval(f,x(2:n))));
fprintf(‘the integral of given f(x) is in the interval [%d,%d] is % f’, a,b,if);
end
end
34
Q. Find the integral if f(x) = in the interval [ 1, 2] by using trapezoidal rule
>>f=@(x) 1./x
>> trap(f,1,2,30)
0.6932
35
18. Simpson’s Rule.
function [if] = simps(f,a,b,n)
if mod(n,2)~=0
n=n+1;
end
h=(b-a)/n;
for i=1:n+1
x(i)=a+(i-1)*h;
end
if=(h/3)*(feval(f,x(1))+feval(f,x(n+1))+2*sum(feval(f,x(2:2:n)))+4*sum(feval(f,x(3:2:n-
1))))
fprintf(‘the integral of given f(x) in the interval f(x) [%d,%d] is %f’,a,b,if)
end
36
Q. Find the integral if f(x) = in the interval [ 1, 2] by using trapezoidal rule
>>f=@(x) 1./x
>> simps(f,1,2,30)
Ans =
0.6766
37
19. Euler Method
38
Q.Find the integral if dy/dx=1-y in the interval [1,2] with N = 8 and y(0)=0
>>f=@(t,y) 1-y
F=
Function_handle with value:
@(y,t)1-y
>>tspan=[0,2]
tspan =
0 2
euler(f, tspan, 0,8)
ans =
0
0.2500
0.4375
0.5781
0.6836
0.7627
0.8220
0.8665
0.8999
39
20. Modified Euler Method
40
Q..Find the integral if dy/dx=1-y in the interval [1,2] with N = 8 and y(0)=0
>>f=@(t,y) 1-y
f=
@(y,t)1-y
>>tspan=[0,2]
tspan =
0 2
meuler(f, tspan, 0,8)
ans =
0
0.2188
0.3896
0.5232
0.6275
0.7090
0.7726
0.8224
0.8612
41
21. Range kutta 4 th order Method.
function [t,y] = rk4(f,tspan,N,y0)
%tspan=[t0,tf]
h=(tspan(2)-tspan(1))/N;
y(1,:)=y0’;
t=tspan(1)+h*[0:N];
For k=1:N
f1=h*feval(f,t(k),y(k,:));
f1=f1(:)’;
f2=h*feval(f,t(k)+(h/2),y(k,:)+f1*(h/2));
f2=f2(:)’;
f3=h*feval(f,t(k)+(h/2),y(k,:)+f2*(h/2));
f3=f3(:)’;
f4=h*feval(f,t(k)+(h/2),y(k,:)+f3*h);
f4=f4(:)’;
Y(k+1,:)=y(k,:)+(f1+2*(f2+f3)+f4)/6
end
Plot(t,y,’r-‘);
Hold on
G=@(t)1-exp(-t);
Y1=feval(g,t);
Plot(t,y1,’b--‘) end
42
Q.Find the integral if dy/dx=1-y in the interval [1,2] with N = 8 and y(0)=0
>>f=@(t,y) 1-y
f=
Function_handle with value
@(y,t)1-y
>>tspan=[0,2]
tspan =
0 2
rk4(f, tspan, 0,8)
y=
Ans =
0
0.2423
0.4260
0.5651
0.6705
0.7503
0.8108
0.8567
0.8914
43
22. Shooting Method.
function[dx]=dff(t,x)
dx(1)=x(2);
dx(2)=(2*x(1)^2)+4*t*x(2)*x(1);
end
44
Q.Find the solution of 2x^2(t)+4x(t)tx'(t) with x(0)=1/4 and x(1)=1/3.
>>Shoot(‘dff’,0,1,1/4,1/3,100,1e-8,100)
45
23. Poisson Equation
function[u]=po(d,mx,my,g,f,tol,maxiter,bx0 ,bxf,by0,byf)
%uxx+uyy+gu=f
x0=d(1);xf=d(2);y0=d(3);yf=d(4);
dx=(xf-x0)/mx;
dy=(yf-y0)/my;
x=x0+[0:mx]*dx;
y=y0+[0:my]*dy;
mx1=mx+1;
my1=my+1;
%boundary values
for m=1:my1
u(m,[1,mx1])=[bx0(y(m)) bxf(y(m))]; end
for m=1:mx1
u([1,my],m)=[by0(x(m)) byf(x(m))] end
%intializing the average boundary value
sumofbv=sum(sum([u(2:my,[1 mx1]) u([1 my1],2:mx)’]));
u([2:my,2:mx])=sumofbv/(2*(mx+my-2));
for i=1:my
for j=1:mx
f(i,j)=f(x(j),y(i)) ; g(i,j)=f(x(j),y(i));
end end
dx2=dx*dx; dy2=dy*dy; dxy2=2*(dx2+dy2);
rx=dx2/dxy2;
ry=dy2/dxy2;
rxy=((dx2*dy2)/dxy2)*2;
for iter=1:maxiter
for j=2:mx
for i=2:my
u(i,j)=rx*(u(i+1,j)+u(i-1,j))+ry*(u(i,j-1)+u(i,j+1))+rxy*(g(i,j)*u(i,j)-f(i,j));
end end
if iter>1 && max(max(abs(u-u0)))<tol
break;
end
u0=u;
end
mesh(x,y,u)
hold on
46
Q. solve the laplace equation ^2 (u)= 0 , u (0,y) = e^y-cos y , u (4,y) =e^y cos(4)- cosy
e^4 , U (x,0)= cos x -e^x , u (x,4) = e^4 cosx - e^x cos 4, 0≤x≤4 , 0≤y≤4.
>>D=[0 4 0 4]
>>bx0=@(y) exp(y)-cos(y)
>>bxf=@(y) cos(4)*exp(y)-exp(4)*cos(y)
>>by0=@(x)cos(x)-exp(x)
>>byf=@(x)exp(4)*cos(x)-exp(x)*cos(4)
>>po(D, 100,100,g, f, 100,10^(-8),bx0,bxf, by0,byf)
47