Docsity

Computational Methods Manual, Lab Reports of Computational Methods

Toggle sidebar
bg1
Prepared by :A.Clement Raj & J.Murugesan Dept .of ECE, BHU
BULE HORA UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF ELECTRICAL & COMPUTER ENFINEERING
II YEAR / SECOND SEMESTER
ECEg2133 COMPUTATIONAL METHODS
LABORATORY MANUAL
BATCH MEMBERS
1. ………………………………………………………………………………….
2. ………………………………………………………………………………….
3. ………………………………………………………………………………….
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Computational Methods Manual and more Lab Reports Computational Methods in PDF only on Docsity!

BULE HORA UNIVERSITY

FACULTY OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF ELECTRICAL & COMPUTER ENFINEERING

II YEAR / SECOND SEMESTER

ECEg2133 COMPUTATIONAL METHODS

LABORATORY MANUAL

BATCH MEMBERS

Table of Contents

Ex. No. Date Name of the Experiment (^) SignatureMarks /

01 Basic Matrix Formation and Calculations

02 Plotting in MATLAB

03 Solving a system of equations

04 Solving a solution of non (i)Bracketing method - BISECTION method-linear equations

05 Solving a s (ii) Open methodolution of non – Newton Raphson method-linear equations

06 Solving a solution of non (ii) Open method – Secant method-linear equations

07 Solving a solution of linear equations (i) Gauss Elimination method

08 Solving a solution of linear equations (ii) Gauss Jordan method

09 Solving a solution of linea (iii) LU Decomposition method (Crout’s Method)r equations

10 Solving Lagrange Linear Interpolation

11 Numerical Integration (i) Solving Trapezoidal Rule

12 Numerical Integration (ii) Solving Simpson’s 1/3 Rule

13 Numerical Integration (iii) Solving Simpson’s 3/8 Rule

14 RungeKutta Method

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 & y c) x & y d) x & y
  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).

  1. 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’)

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

Lab Exercise – V

Solving a solution of non-linear equations

(ii) Open method – Newton Raphson method

Program

functionXs = 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 formatlong% 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

Lab Exercise – VI

Solving a solution of non-linear equations

(ii) Open method – Secant method

Program

functionXs = 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 formatlong% prints more digits Fun = @(x) …………………………; Xa=…….; Xb=……….; Err=1010^-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

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- for z=2:m if c(j,j)== 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- for i=j-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'

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

Lab Exercise – XI

NUMERICAL INTEGRATION

(i) Solving Trapezoidal Rule

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

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

x

y

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

Lab Exercise – XII

(ii) Solving Simpson’s 1/3 Rule

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

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

x

y

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

Lab Exercise – XIV

RungeKutta 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 RungeKutta 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 = hfeval(df , X(1,i), Y(1,i)); k2 = hfeval(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 = hfeval(df , X(1,i), Y(1,i)); k2 = hfeval(df , X(1,i) + h/2, Y(1,i) + k1); k3 = hfeval(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

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 = hfeval(df , X(1,i), Y(1,i)); k2 = hfeval(df , X(1,i) + h/2, Y(1,i) + k1); k3 = hfeval(df , X(1,i) + h/2, Y(1,i) + k2); k4 = hfeval(df , X(1,i) + h, Y(1,i) + k3); k = 1/6* ( k1+ 2k2 + 2k3 + 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 ');