Professional Documents
Culture Documents
com
SCIENTIFIC COMPUTING
LAB FILE
www.mcdtu.wordpress.com |
www.mcdtu.wordpress.com
ASSIGNMENT 1
BISECTION METHOD
SOURCE CODE:
% Bisection method to find the root of the function f(x)=0
% INPUT
%
%
- f is the function
- a and b are the left and right endpoints
- tol is the tolerance
% OUTPUT
%
%
- c is the root
- yc= f(c)
- err is the error estimate for c
www.mcdtu.wordpress.com
if yc==0
fprintf('The equation is sin(x) - tan(x^2) + exp(x)and the root
is x = %g \n',c);
elseif yb*yc>0
b=c;
yb=yc;
else
a=c;
ya=yc;
end
end
c=(a+b)/2;
err=abs(b-a);
yc=f(c);
fprintf('\n The equation is sin(x) - tan(x^2) + exp(x) and the root is
x = %g \n',c);
fprintf('\n Number of iterations are = %g \n',count);
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
www.mcdtu.wordpress.com
ASSIGNMENT 2
% INPUT
%
%
%
%
f is the function
f1 is the derivative of f
xn is previous value
tol is the tolerance
fxn=f(xn)
% OUTPUT
%
-------
------
www.mcdtu.wordpress.com |
-----
----5
www.mcdtu.wordpress.com
xn=-1; % Initial guess
tol=.000001; count=0;
disp(sprintf('The initial approximation of the first root is %g',xn));
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-fxn/f1xn;
while( abs(xn1-xn) > tol)
count=count+1;
if fxn==0
xn1=xn
disp(sprintf('The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
else
xn=xn1;
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-(fxn/f1xn);
end
end
disp(sprintf('\n The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
disp(sprintf('\n Number of iterations are = %g',count));
% SECOND ROOT
disp('------------SECOND ROOT OF f(x)
-----------')
xn=0;% Initial guess
tol=.000001; count=0;
disp(sprintf('\nThe initial approximation of the second root is
%g',xn));
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-fxn/f1xn;
while( abs(xn1-xn) > tol)
count=count+1;
if fxn==0
xn1=xn
disp(sprintf('The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
else
xn=xn1;
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-(fxn/f1xn);
end
end
disp(sprintf('\n The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
disp(sprintf('\n Number of iterations are = %g',count));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
www.mcdtu.wordpress.com
ASSIGNMENT 3
% OUTPUT
- x, y, z and w are the solutions to the given system of
% equations.
% PROGRAM STARTS HERE
clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 3, Gauss - Seidel Method
--')
disp('--Date - 20/03/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
disp(sprintf('\nSystem of equations is:\n1. 4x+y+w=2\n2. x+4y+z=-2\n3.
y+4z+w=2\n4. x+z+4w=-2'));
x=0; % Initial approximations
y=0;
z=0;
w=0;
tol=.000001; % tol is the limit of while loop.
count=1; % count is used to calculate no. of iterations.
disp(sprintf('\nInitial guess =[%g %g %g %g]',x,y,z,w));
disp('--------------')
% SOLUTION starts here!
------
-----
X0=[x;y;z;w];
x= 1/4*(2-y-w);
y= 1/4*(-2-x-z);
www.mcdtu.wordpress.com |
www.mcdtu.wordpress.com
z= 1/4*(2-y-w);
w= 1/4*(-2-x-z);
X1=[x;y;z;w];
while(max(X1-X0) > tol)
count=count+1;
X0=[x;y;z;w];
x= 1/4*(2-y-w);
y= 1/4*(-2-x-z);
z= 1/4*(2-y-w);
w= 1/4*(-2-x-z);
X1=[x;y;z;w];
end
disp(sprintf('\nNo. of iterations are: %g',count));
disp(sprintf('\nThe solution of system of system of equations =[%g %g
%g %g]',x,y,z,w));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
10
www.mcdtu.wordpress.com
ASSIGNMENT 4
SECANT METHOD
SOURCE CODE:
% Secant Method
% INPUT
%
%
%
%
f is the function
xn and xn0 are the previous values
tol is the tolerance
fxn=f(xn)
fxn0=f(xn0)
% OUTPUT
%
-----
11
www.mcdtu.wordpress.com
fxn0=f(xn0);
xn1=xn-fxn/(fxn-fxn0)*(xn-xn0);
while( abs(xn1-xn) > tol)
count=count+1;
if fxn==0
xn1=xn;
xn=xn0;
disp(sprintf('The equation is cos(x)-x*(e^x) and the root is x
= %g',xn1));
else
xn=xn1;
fxn=f(xn);
fxn0=f(xn0);
xn1=xn-fxn/(fxn-fxn0)*(xn-xn0);
end
end
disp(sprintf('\n The equation is cos(x)-x*(e^x) and the root is x =
%g',xn1));
disp(sprintf('\n Number of iterations are %g',count));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
12
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
13
www.mcdtu.wordpress.com
ASSIGNMENT 5
CROUTS METHOD
SOURCE CODE:
% Crout's method is used to find the solution of a system of
% equations!
% INPUT
%
%
%
%
%
% OUTPUT
% equations.
-----
l11=A(1,1);
l21=A(2,1);
l31=A(3,1);
l41=A(4,1);
u12=A(1,2)./l11;
www.mcdtu.wordpress.com |
14
www.mcdtu.wordpress.com
u13=A(1,3)./l11;
u14=A(1,4)./l11;
l22=A(2,2)-l21.*u12;
l32=A(3,2)-l31.*u12;
l42=A(4,2)-l41.*u12;
u23=(A(2,3)-l21.*u13)./l22;
u24=(A(2,4)-l21.*u14)./l22;
l33=A(3,3)-l31*u13-l32*u23;
l43=A(4,3)-l41*u13-l42*u23;
u34=(A(3,4)-l31*u14-l32*u24)/l33;
l44=A(4,4)-l41*u14-l42*u24-l43*u34;
disp(sprintf('\nLower triangular matrix is\n'));
L=[l11 0 0 0;l21 l22 0 0;l31 l32 l33 0; l41 l42 l43 l44]
disp(sprintf('\nUpper triangular matrix is\n'));
U=[1 u12 u13 u14; 0 1 u23 u24; 0 0 1 u34; 0 0 0 1]
Z=(inv(L))*B;
disp(sprintf('\nThe solution of system of system of equations is'));
X=(inv(U))*Z
Ainv=inv(U).*inv(L)
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
15
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
16
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com |
17
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com |
18
www.mcdtu.wordpress.com
ASSIGNMENT 6
19
www.mcdtu.wordpress.com
for j = 2 : n - 1
for k = 1 : n - j
d(k, j) = (d(k+1, j - 1) - d(k, j - 1))/(x(k+j) - x(k));
end
end
for j = 2 : n
a(j) = d(1, j-1);
end
Df(1) = 1;
c(1) = a(1);
for j = 2 : n
Df(j)=(p - x(j-1)) .* Df(j-1);
c(j) = a(j) .* Df(j);
end
fp=sum(c);
disp(sprintf('\nWe get value as %g',fp)); % Finding sum
disp(sprintf('\n\nApplying Newton Backward Interpolation difference
formula for x = %g',x1(l)));
% Newton's Backward Interpolation Formula
for i=1:n
diff(i,1)=y(i); % Finite backward difference operator matrix
end
for j=2:n
for i=n:-1:j
diff(i,j)=diff(i,j-1)-diff(i-1,j-1);
end
end
answer=y(n);
h=x(n)-x(n-1);
s=(p-x(n))/h;
for i=1:n-1
term=1;
for j=1:i
term=term*(s+j-1)/j;
end
answer=answer+term*diff(n,i+1); % Finding sum
end
disp(sprintf('\nWe get value as %g',answer));
end
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
20
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
21
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com |
22
www.mcdtu.wordpress.com
ASSIGNMENT 7
TRAPEZOIDAL METHOD
SOURCE CODE:
% TRAPEZOIDAL METHOD to perform Numerical Integration!
% INPUTS
- f is the fucntion e^-(x^2)=0 we need to integrate.
%
- x0 is the lower limit of integration.
%
- x1 is the upper limit of integration.
%
- n is the number of mesh points
%
i.e no of intervals between x0 and x1.
%
- h is the length of the interval.
% Area gives the result of integration of curve f between f0 and f1.
% PROGRAM STARTS HERE
clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 7, Trapezoidal Method
--')
disp('--Date - 10/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS:Enter the following:
f=inline('exp(-x^2)');%inline function definition
x0=0;
x1=1;
n=20; % No. of intervals to be present between 0 and 1.
f0=f(x0);
f1=f(x1); % Computation of function values at end points.
h=(x1-x0)/n; % Value of length of the intervals.
f2=0;%initialize f2 to zero
a=x0;
for i=1:n-1
x2= a+i*h;
f2= f2+ f(x2);
end
disp('The answer for integration is: ');
area= h*(f0+f1)/2+ h*f2 % Prints the result of integration.
www.mcdtu.wordpress.com |
23
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com |
24
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
25
www.mcdtu.wordpress.com
ASSIGNMENT 8
SIMPSONS METHOD
SOURCE CODE:
% Simpson's Method is used for Numerical Integration!
% INPUT
%
%
- f is the function
- f0 and fn are 1st and last values
- h is the difference between 2 values of x
% OUTPUT
- fx is the required function
% PROGRAM STARTS HERE
clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 8, Simpsons Method
--')
disp('--Date - 11/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
f=inline('exp(-x^2)'); % Function
f0=f(0); % Value of the function at 0
fn=f(1); % Value of the function at 1
h=0.05;
n=20; % No. of divisions
fx=h/3*(f0+fn);
if mod(n,2)==0 % mod is used to find the remainder
for i=1:19
k=i*h;
if mod(i,2)==0
fx=fx+2*h/3*f(k); % Formula when i is a multiple of 2
else
fx=fx+4*h/3*f(k); % Formula when i is not a multiple of 2
end
end
end
disp(sprintf('\nThe equation is exp(-x^2) and its integration from x
= 0 to 1 by Simpsons method is\n %g',fx));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
26
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
27
www.mcdtu.wordpress.com
ASSIGNMENT 9
ROMBERGS METHOD
SOURCE CODE:
% Romberg's Method is a method used for Numerical Integration
% INPUT
%
%
- f is the function.
- f0 and fn are 1st and last values.
- h1,h2,h3 are the difference between 2 values of x.
% OUTPUT
%
%
28
www.mcdtu.wordpress.com
h3=h2/2;
n3=(pi/2-pi/4)/h3;
Q3=h3/3*(f0+fn); % Applying Simpson's rule
if mod(n3,2)==0 % mod is used to find remainder
for i=1:n3
k=i*h3;
if mod(i,2)==0
Q3=Q3+2*h3/3*f(pi/4 + k); % Formula for i multiple of 2
else
Q3=Q3+4*h3/3*f(pi/4 + k); % Formula for not i multiple of 2
end
end
end
R2=(16*Q3-Q2)/15;
S=(16*R2-R1)/15;
disp(sprintf('\nThe equation is (cos(x)*log(sin(x)))/(1+(sin(x)^2))
and its integration \nfrom x = pi/4 to pi/2 by Rombergs Method is\n
%g',S));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
29
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
30
www.mcdtu.wordpress.com
ASSIGNMENT 10
PICARDS METHOD
SOURCE CODE:
% Picard's Method is used to find solution of a differential equations
% INPUT
%
- f is the function
- y0 is the solution at initial value of x=0.4
% OUTPUT
%
31
www.mcdtu.wordpress.com
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y) \nby Picards method at x = 0.8 is \n%g\n',Y1));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
32
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
33
www.mcdtu.wordpress.com
ASSIGNMENT 11
EULERS METHOD
SOURCE CODE:
% Euler's method is used to find solution of a differential equation.
% INPUT
%
- f is the function.
- y0 is the solution at initial value of x=0.4.
% OUTPUT
%
www.mcdtu.wordpress.com |
34
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
35
www.mcdtu.wordpress.com
ASSIGNMENT 12
- f is the function.
- y0 is the solution at initial value of x=0.4.
% OUTPUT
%
36
www.mcdtu.wordpress.com
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y)\nby Runge Kutta 4th Order Method at x = 0.8 is \n%g\n',Y1));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |
37
www.mcdtu.wordpress.com
OUTPUT:
www.mcdtu.wordpress.com |
38