









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










ECEg2133 COMPUTATIONAL METHODS
LABORATORY MANUAL
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
between (x,y2) & (x,y4).
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
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 ');