Você está na página 1de 17

BULE HORA UNIVERSITY

FACULTY OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF ELECTRICAL & COMPUTER ENFINEERING

II YEAR / SECOND SEMESTER

ECEg2133 COMPUTATIONAL METHODS

LAB MANUAL

BATCH MEMBERS

1. ………………………………………………………………………………….

2. ………………………………………………………………………………….

3. ………………………………………………………………………………….

Prepared by : A.Clement Raj Dept .of ECE, BHU


Table of Contents

Ex. No. Date Experiment Name Marks / Signature

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – I

Basic Matrix Formation and Calculations

 Create a 3*3 matrix

>> A = [3 7 -9;5 2 4;-6 1 8];

 Create another 3*3 matrix

>> B = [4 -7 2;0 9 5;-8 3 1];

 Create a 3*2 matrix


>> C = [3 -9;6 8;4 -8];

Exercise
i. Find the Product of
a) A & B
b) B & C
c) A & C
d) C & B
e) C & A
ii. Find the Transpose of matrix A.
iii. Find the inverse of matrix B.
iv. Find the determinant of matrix C.
v. Find the difference of matrix A & C ; A & B.

Solution

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – II

Plotting in MATLAB

Create the values for x & y

>> x = linspace(-5,5);

>> y = x.^2;

>> y2 = cos(x);

>> y3 = x.^3;

>> y4 = x.^4;

Exercise

1. Draw the curve between


a) x & y
b) x & y2
c) x & y3
d) x & y4

2. Draw the curve (x,y) & (x,y3) in same axes.

3. How to open two figure windows? Now open two figure windows and draw or plot the curve
between (x,y2) & (x,y4).

4. Create a new x

>> x = ( 0:0.1:2*pi);

Now plot(x,sin(x)); showing ‘x’ axis as (‘Angle(radian)’) and ‘y’ axis as (‘Amplitude’)
and the ‘title’ as (‘Sinusoidal’)

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – III

Solving a system of equations

Exercise

1. Solve a system of equations


2x – y + 3z = 5;
4x + 5z = 12;
x + y + 2z = -3;
Using all MATLAB commands, find the solution for the above equations and verify the
answers.

2. Solve a system of equations


3x1 + 4x2 – 2x3 + 2x4 = 2;
4x1 + 9x2 – 3x3 + 5x4 = 8;
-2x1 – 3x2 + 7x3 + 6x4 = 10;
x1 + 4x2 + 6x3 + 7x4 = 2;
Using all MATLAB commands, find the solution for the above equations and verify the
answers.

Solution

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – IV

Solving a solution of non-linear equations

(i) Bracketing method - BISECTION method


Program
function [x e] = mybisect (f,a,b,n)
% function [x e] = mybisect (f,a,b,n)
% Does n iterations of the bisection method for a function f
% Inputs : f -- a function
% a,b -- left and right edges of the interval
% n -- the number of bisections to do.
% Outputs : x -- the estimated solution of f(x) = 0
% e -- an upper bound on the error
format long
% evaluate at the ends and make sure there is a sign change
a = 1;
b = 2;
x = (a + b )/2;
f = @(x)…………………………………………………;
n=5;
if f(a)*f(b)>0
error ('Function has same sign at both end points')
end
disp ('x y')
for i = 1:n
% find the middle and evaluate there
x = (a + b )/2;
y = f(x);
disp ([ x y])
if y == 0.0 % solved the equation exactly
e = 0;
break % jumps out of the for loop
end
% decide which half to keep , so that the signs at the ends differ
if f(a)*y < 0
b=x;
else
a=x;
end
end
% set the best estimate for x and the error bound
x = (a + b )/2;
e = (b-a )/2;

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – V

Solving a solution of non-linear equations

(ii) Open method – Newton Raphson method


Program
function Xs = NewtonRoot (Fun,FunDer,Xest,Err,imax)
% Solves f(x) = 0 by doing n steps of Newton ’s method starting at x0.
% Inputs : f -- the function
% f1 -- it ’s derivative
% x0 -- starting guess , a number
% n -- the number of steps to do
% Output : x -- the approximate solution
format long % prints more digits
Fun = @(x) ………………………………..;
FunDer = @(x) ……………………………….;
Xest=……………..;
Err=5*10^-4;
imax=50;
for i = 1:imax
Xi = Xest - feval(Fun,Xest)/feval(FunDer,Xest);
if abs((Xi - Xest)/Xest) < Err
Xs = Xi;
break
end
Xest = Xi;
end
if i == imax
fprintf('solution was not obtained in %i iterations.\n',imax)
Xs = ('No Answer');
end

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – VI

Solving a solution of non-linear equations

(ii) Open method – Secant method


Program
function Xs = SecantRoot (Fun,Xa,Xb,Err,imax)
% Inputs : f -- the function
% xa & xb -- starting guesses , a number
% n -- the number of steps to do
% Output : x -- the approximate solution
format long % prints more digits
Fun = @(x) …………………………;
Xa=…….;
Xb=……….;
Err=10*10^-3;
imax=5;
for i = 1:imax
FunXb = feval(Fun,Xb);
Xi = Xb - FunXb*(Xa-Xb)/(feval(Fun,Xa)-FunXb);
if abs((Xi - Xb)/Xb) < Err
Xs = Xi;
break
end
Xa = Xb;
Xb = Xi;
end
if i == imax
fprintf('solution was not obtained in %i iterations.\n',imax)
Xs = ('No Answer');
end

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – VII

Solving a solution of linear equations

(i) Gauss Elimination method


Program
% solve the system of linear equations;
a=[ ];
% Gauss Elimination method
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);a(j,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
x=zeros(1,m);
for s=m: -1:1
c=0;
for k=2:m
c=c+a(s,k)*x(k);
end
x(s)=(a(s,n)-c)/a(s,s);
end
disp('Gauss elimination method:');
a
x'

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – VIII

Solving a solution of linear equations

(ii) Gauss Jordan method


Program
% Gauss - Jordan method
C=[ ];
[m,n]=size(c);
for j=1:m-1
for z=2:m
if c(j,j)==0
t=c(1,:);c(1,:)=c(z,:);
c(z,:)=t;
end
end
for i=j+1:m
c(i,:)=c(i,:)-c(j,:)*(c(i,j)/c(j,j));
end
end

for j=m:1-2
for i=j-1:-1:1
c(i,:)=c(i,:)-c(j,:)*(c(i,j)/c(j,j));
end
end

for s=1:m
c(s,:)=c(s,:)/c(s,s);
x(s)=c(s,n);
end
disp('Gauss-Jordan method');
c
x'

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – IX

Solving a solution of linear equations

(iii) LU Decomposition method (Crout’s Method)


function [L, U] = LUdecompCrout(A)
% The function decoposes the matrix A into a lower triangular matrix L
% and an Upper triangular matrix U, using Crout's method such thst A=LU.
% Input Variables:
%A The matrix of coefficients.
% Output variable:
% L Lower triangular matrix.
% U Upper triangular matrix

A =[2 -4 1
6 2 -1
-2 6 -2];

[R,C] = size (A);


for i = 1:R
L(i,1) = A(i,1);
U(i,i) = 1;
end
for j=2:R
U(1,j)= A(1,j)/L(1,1);
end
for i=2:R
for j=2:i
L(i,j)=A(i,j)-L(i,1:j-1)*U(1:j-1,j);
end
for j = i+1:R
U(i,j)=(A(i,j)-L(i,1:i-1)*U(1:i-1,j))/L(i,i);
end
end

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – X

Solving Lagrange Linear Interpolation

% Question: Given set of values of x and y (5,12),(6,13),(9,14),(11,16)

% Find the value of x corresponding to y=15 using lagrange interpolation

clc;

clear all;

close all;

y=[12 13 14 16]; %Change here for different function

x=[5 6 9 11];

a=15;

%Applying Lagrange's Interpolation:

ans1=((a-y(2))*(a-y(3))*(a-y(4)))*x(1)/((y(1)-y(2))*(y(1)-y(3))*(y(1)-y(4)));

ans2=((a-y(1))*(a-y(3))*(a-y(4)))*x(2)/((y(2)-y(1))*(y(2)-y(3))*(y(2)-y(4)));

ans3=((a-y(1))*(a-y(2))*(a-y(4)))*x(3)/((y(3)-y(1))*(y(3)-y(2))*(y(3)-y(4)));

ans4=((a-y(1))*(a-y(2))*(a-y(3)))*x(4)/((y(4)-y(1))*(y(4)-y(2))*(y(4)-y(3)));

m=ans1+ans2+ans3+ans4;

fprintf('the value of x corresponding to y=15 is %f',m);

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – XI

NUMERICAL INTEGRATION

(i) Solving Trapezoidal Rule


%Question: Evaluate the integral X^4 within limits 3 to -3

clc;

clear all;

close all;

f=@(x)x^4; %Change here for different function

a=-3;b=3; %Given limits

n=b-a; %Number of intervals

h=(b-a)/n;

p=0;

for i=a:b

p=p+1;

x(p)=i;

y(p)=i^4; %Change here for different function

end

l=length(x);

answer=(h/2)*((y(1)+y(l))+2*(sum(y)-y(1)-y(l)))

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – XII

(ii) Solving Simpson’s 1/3 Rule


%Question: Evaluate the integral X^4 within limits 3 to -3

clc;

clear all;

close all;

f=@(x)x^4; %Change here for different function

a=-3;b=3; %Given limits

n=b-a; %Number of intervals

h=(b-a)/n;

p=0;

for i=a:b

p=p+1;

x(p)=i;

y(p)=i^4; %Change here for different function

end

l=length(x);

answer=(h/3)*((y(1)+y(l))+2*(y(3)+y(5))+4*(y(2)+y(4)+y(6)))

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – XIII

(iii) Solving Simpson’s 3/8 Rule


%Question: Evaluate the integral x^4 within limits -3 to 3

clc;

clear all;

close all;

f=@(x)x^4; %Change here for different function

a=-3;b=3; %Given limits

n=b-a; %Number of intervals

h=(b-a)/n;

p=0;

for i=a:b

p=p+1;

x(p)=i;

y(p)=i^4; %Change here for different function

end

l=length(x);

answer=(3*h/8)*((y(1)+y(l))+3*(y(2)+y(3)+y(5)+y(6))+2*(y(4)))

Prepared by : A.Clement Raj Dept .of ECE, BHU


Lab Exercise – XIV

Runge Kutta Method

function a = runge_kutta(df)
% asking initial conditions

x0 = input('Enter initial value of x : ');


y0 = input ('Enter initial value of y : ');
x1 = input( 'Enter value of x at which y is to be calculated : ');
tol = input( 'Enter desired level of accuracy in the final result : ');

%choose the order of Runge-Kutta method

r = menu ( ' Which order of Runge Kutta u want to use', ...


' 2nd order ' , ' 3rd order ' , ' 4th order ');

switch r
case 1
% calculating the value of h
n =ceil( (x1-x0)/sqrt(tol));
h = ( x1 - x0)/n;
for i = 1 : n
X(1,1) = x0; Y (1,1) = y0;
k1 = h*feval( df , X(1,i), Y(1,i));
k2 = h*feval( df , X(1,i) + h , Y(1,i) + k1);
k = 1/2* ( k1+ k2);
X( 1, i+1) = X(1,i) + h;
Y( 1 ,i+1) = Y(1,i) + k;
end

case 2
% calculating the value of h
n =ceil( (x1-x0)/nthroot(tol,3));
h = ( x1 - x0)/n;
for i = 1 : n
X(1,1) = x0; Y (1,1) = y0;
k1 = h*feval( df , X(1,i), Y(1,i));
k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1);
k3 = h*feval( df , X(1,i) + h, Y(1,i) + k2);
k = 1/6* ( k1+ 4*k2 + k3);
X( 1, i+1) = X(1,i) + h;
Y( 1 ,i+1) = Y(1,i) + k;
end

Prepared by : A.Clement Raj Dept .of ECE, BHU


case 3
% calculating the value of h
n =ceil( (x1-x0)/nthroot(tol,3));
h = ( x1 - x0)/n;
for i = 1 : n
X(1,1) = x0; Y (1,1) = y0;
k1 = h*feval( df , X(1,i), Y(1,i));
k2 = h*feval( df , X(1,i) + h/2, Y(1,i) + k1);
k3 = h*feval( df , X(1,i) + h/2, Y(1,i) + k2);
k4 = h*feval( df , X(1,i) + h, Y(1,i) + k3);
k = 1/6* ( k1+ 2*k2 + 2*k3 + k4);
X( 1, i+1) = X(1,i) + h;
Y( 1 ,i+1) = Y(1,i) + k;
end

end

%displaying results

fprintf( 'for x = %g \n y = %g \n' , x1,Y(1,n+1))

%displaying graph

x = 1:n+1;
y = Y(1,n+1)*ones(1,n+1) - Y(1,:);
plot(x,y,'r')
xlabel = (' no of interval ');
ylabel = ( ' Error ');

Prepared by : A.Clement Raj Dept .of ECE, BHU

Você também pode gostar