0% found this document useful (0 votes)
24 views47 pages

1.bisection Method

The document outlines various numerical methods for finding roots of equations, including the Bisection Method, Fixed Point Method, Regula Falsi Method, Newton-Raphson Method, Secant Method, Steffensen’s Method, and methods for solving systems of linear equations such as Gauss-Jacobi and Gauss-Elimination. Each method is accompanied by MATLAB code snippets and example problems demonstrating their application. The document provides specific examples and results for functions such as f(x) = cos(x) - x and systems of equations.

Uploaded by

mranonymous11211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views47 pages

1.bisection Method

The document outlines various numerical methods for finding roots of equations, including the Bisection Method, Fixed Point Method, Regula Falsi Method, Newton-Raphson Method, Secant Method, Steffensen’s Method, and methods for solving systems of linear equations such as Gauss-Jacobi and Gauss-Elimination. Each method is accompanied by MATLAB code snippets and example problems demonstrating their application. The document provides specific examples and results for functions such as f(x) = cos(x) - x and systems of equations.

Uploaded by

mranonymous11211
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

1.

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=

Function_handle with value:

@(x)cos(x)-x
>>falseposition(0,pi/4,10^(-8),100)

The root of the given f(x) is 7.390851e-01 achived in 4 iteration


Ans=0.7391

6
4.Newton Raphson Method

function [x,err,xx] = nr(f,df,x0,tolerr,maxiter)


clc
close all
h=1e-4;
h2=2*h;
tolfun=eps;
if nargin==4 & isnumeric(df)
maxiter=tolerr;
tolerr=x0;
x0=df; end
xx(1)=x0;fx=feval(f,xx(1));
for k=1:maxiter
if ~isnumeric(df)
dfdx=feval(df,x0)
else
dfdx= (feval(f,xx(k)+h)-feval(f,xx(k)-h))/h2;
end
dx=-fx/dfdx;
xx(k+1)=xx(k)+dx;
if abs(fx)<tolfun||abs(xx(k+1)-xx(k))<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);
plot(xh,y);
hold on
plot(x,fx,’r*’)
end

7
Q. Find the root of the function f(x)=cosx- x by using Newton raphson Method
>> f=@(x) cos(x)-x

F=

Function_handle with value:

@(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=@(x) cos(x)-x

F=
Function_handle with value:

The root of the given f(x) is 7.390851e-01 achived in 5 iteration


Z=

8.8818e-16

Ans =

0.7391

10
6. Steffensen’s Method

function [x,err] = sts(f,a,b,tolerr,maxiter)


clc
close all

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=

Function_handle with value:

@(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;

if norm(x – x_old) / norm(x_old + eps) < tolerance


break
end
end end

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]

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)

Elapsed time is 0.010000 seconds.

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.

function [x] =backsubst(u,b)


n=size(u,1);
x(n,:)=b(n,:)/u(n,n);
for m=n-1:-1:1
x(m,:)=(b(m,:)-u(m,m+1:n)*x(m+1:n,:))/u(m,m);
end
end

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

Find the largest eigen value?


>> 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
>> eigenlar (A,x0)

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

Find the least eigen value?

>> 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

The least eigen value of A is Inf


Ans =0

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.

function [lambdas,v] = shftpower(a,s,x0,tolerr,maxiter)


if nargin <3
maxiter =100;
tolerr=1e-8;
end
cla=size(a,2);
x0=x0;
b=[a-s*eye(size(a))]^-1;
[lambda,v]=eigenlar(b,x0)
lambdas=(1/lambda)+s;
fprintf(‘the eigen value near to s’)
end

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]

A=
6 -2 2
-2 3 -1
2 -1 3
>> x0 = [1;1;1]
X0 =
1
1
1
>> Shiftpower(A, 1,x0)

The eigen value near to s


Ans =

2.0000

29
15. Lagrange Interpolation Method

function [lp] = lagrange(x,y)


% x=[x0,x1,…,xn]
% y=[y0,y1,…,yn]
%l-coefficient of lagrange polynomial
%l-lagrange coefficient
n=length(x)-1;
l=0;
for m=1:n+1
p=1;
for k=1:n+1
if k~=m
p=conv(p,[1 -x(k)])/(x(m)-x(k));
end
end
l(m,:)=p;
l=l+p*y(m);
end
s=input(‘at which point do you want to find the value.’)
if x(1)<s & x(n+1)>s
fx=polyval(l,s)
fprintf(‘the interpolation value is %d’,fx)
plot(x,y,’r-‘)
else
error(‘the given value is out of range of x,so interpolation is not possible.’)
end
end

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.

function [dd,n] = nd(x,y)


clc
% x=[x0,x1,…,xn]
% y=[y0,y1,…,yn]
% n=newton’s polynomial of degree n
n=length(x)-1;
dd=zeros(n+1,n+1);
dd(1:n+1,1)=y’;
for k=2:n+1
for m=1:n-k+2
dd(m,k)=(dd(m+1,k-1)-dd(m,k-1))/(x(m+k-1)-x(m))
end
end
a=dd(1,:);
n=a(n+1);
for k=n:-1:1
n=[n a(k)]-[0 n*x(k)]
end
s=input(‘at which point do you want to find the value.’)
fx=polyval(n,s)
fprintf(‘the interpolation value is %d’,fx)
plot(x,y,’r-*’);hold on
end

32
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
>> 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)

The integral of given f(x) is in the interval [1,2] is 0.693217


Ans =

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)

The integral of given f(x) is in the interval [1,2] is 0.676619

Ans =

0.6766

37
19. Euler Method

function [y] = euler(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
y(k+1,:)=y(k,:)+(h*feval(f,t(k),y(k,:)));
end
plot(t,y,'r-');
hold on
g=@(t)1-exp(-t);
y1=feval(g,t);
plot(t,y1,'g-')
end

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

Function [y] = meuler(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
Y(k+1,:)=y(k,☺+(h/2)*(feval(f,t(k),y(k,:))+feval(f,t(k+1),y(k,:)+h*feval(f,t(k),y(k,:))));
End
Plot(t,y,’r-‘);
Hold on
G=@(t)1-exp(-t);
Y1=feval(g,t);
Plot(t,y1,’b--‘)
End

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=

Function_handle with value:

@(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

function [x] = shoot(f,t0,tf,x0,xf,n,tol,kmax)


close all
if nargin<8
kmax=100
end
if nargin<7
tol=1e-10
end
if nargin<6
n=10
end
dx0(1)=(xf-x0)/(tf-t0);
[t,x]=rk4(f,[t0 tf],n,[x0 dx0(1)]);
plot(t,x(:,1),’r-‘)
hold on
e(1)=x(end,1)-xf;
dx0(2)=dx0(1)-.1*sign(e(1));
for k=2:kmax-1
[t,x]=rk4(f,[t0 tf],n,[x0 dx0(k)]);
plot(t,x(:,1),’b—‘);
e(k)=x(end,1)-xf;
ddx=dx0(k)-dx0(k-1);
if abs(e(k))<tol |abs(ddx)<tol
break
end
deddx=(e(k)-e(k-1))/ddx;
dx0(k+1)=dx0(k)-e(k)/deddx;
end
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

You might also like