0% found this document useful (0 votes)
286 views

MATLAB Code Gauss Eliminations Method: % Forward Elimination

The document contains MATLAB code for numerical integration techniques: 1) Trapezoidal rule to evaluate the integral of a function from a to b using n trapezoids. 2) Simpson's 3/8 rule to evaluate the integral of a function from a to b using Simpson's 3/8 formula with na intervals. 3) Simpson's 1/3 rule to evaluate the integral of a function from a to b using Simpson's 1/3 formula with n intervals in a single application and multiple applications.

Uploaded by

etayhailu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
286 views

MATLAB Code Gauss Eliminations Method: % Forward Elimination

The document contains MATLAB code for numerical integration techniques: 1) Trapezoidal rule to evaluate the integral of a function from a to b using n trapezoids. 2) Simpson's 3/8 rule to evaluate the integral of a function from a to b using Simpson's 3/8 formula with na intervals. 3) Simpson's 1/3 rule to evaluate the integral of a function from a to b using Simpson's 1/3 formula with n intervals in a single application and multiple applications.

Uploaded by

etayhailu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MATLAB code Gauss eliminations method

clear all
clc
A1 = [2 8 7 5;4 2 3 9;2 6 4 5;8 2 7 9];
b = [5; 8; 9; 19];
n = length(A1);
aX = inv(A1)*b;
A = [A1 b];
for j = 1:n-1
k = 0;
for i = j+1:n
% forward elimination
k = k+1;
m(k) = A(i,j)/A(j,j);
A(i,j:n+1) = A(i,j:n+1)-A(j,j:n+1)*m(k);
end
end
X = zeros(n,1);
% backward substitution
X(n,1) = A(n,n+1)/A(n,n);
for i = n-1:-1:1
X(i,1) = (A(i,n+1)-A(i,n:-1:i+1)*X(n:-1:i+1,1))/A(i,i);
end
[aX X]

MATLAB code for LU decomposition


Save it as “lud”
clear
clc save it as “frw”
n = 5; function [U,L] = Frw(A)
A=randi([1 9],n,n); n=length(A);
B=randi([1 9],n,1); k=0;
[U,L]=Frw(A); U=zeros(n,n);
X=Sub(U,L,B); for j=1:n-1
for i=j+1:n
X1 = A\B; k=k+1;
[X X1] M(k)=A(i,j)/A(j,j);
A(i,j:n) = A(i,j:n) - M(k)*A(j,j:n);
U = A;
Save it as “sub” end
function X = Sub(U,L,B)
end
n=length(L);
L=zeros(n,n);
D=zeros(n,1);
k=0;
D(1,1)=B(1,1);
for j = 1:n-1
for i=2:n
for i = j+1:n
D(i,1)=B(i,1)-(L(i,1:i-1)*D(1:i-1,1));
k=k+1;
end
L(i,j)=M(k);
X=zeros(n,1);
end
X(n,1) = D(n,1)/U(n,n);
end
for i=n-1:-1:1
for i=1:n;
X(i,1) = (D(i,1) - (U(i,n:-1:i+1)*X(n:-
L(i,i)=1;
1:i+1,1)))/U(i,i);
end
end
MATLAB code for bisection and false MATLAB code for newton Raphson
position
clear all
Save it as “bisecpro” close all
clc
function [ f ] = bisecpro( x ) x=10;
f = sin(x)+x.*cos(x); xr=0;
end p= xr;
n = 100;
save it as “bisecsol” f=@(x) cos(x)-3*x+1;
df=@(x) -sin(x)-3;
clear all for i=1:n
clc x=x-(f(x)/df(x));
f=@bisecpro; xr=x;
xl=-5; err(i)=f(xr)-f(p);
xu=5; end
xr = 0; plot(err,'-r')
p = xr; fprintf('Approximate Root is %f',x)
n = 100; xlabel('Number of iterations')
if f(xl)*f(xu)>0 ylabel('Error')
disp('wrong choice of initial values') grid on;
else title('Error Vs. Number of iterations')
for i=1:n
xr=(xl+xu)/2;
%xr = xu-((f(xu)*(xl-xu))/f(xl)-f(xu)); MATLAB code for Fixed point iteration

err(i)=f(xr)-f(p); clear all


if f(xr)*f(xl)<0 close all
xu=xr; clc
else f= @(x) sin(x)-x.^2;
xl=xr; x = 1;
end n = 100;
end xold = x;
end for i = 1:n
fprintf('Root of given equation is %f',xr) x = sqrt(sin(x));
plot(err); err(i) = f(x)-f(xold);
grid on; xold = x;
title('Plot of error') end
xlabel('Number of iterations') plot(err,'-r')
ylabel('Error') fprintf('the root of the eqation sin(x)-
hold off x.^2=0 is %f',x)
title('plot of erro vs iteration')
xlabel('number of iteration')
ylabel('error')
grid on
MATLAB code for trapezoidal MATLAB code for Simpson’s 3/8
a = 0;
clear all b = 0.8;
clc na = 100;
format long f = @(x) 0.2 + 25*x - 200*x.^2 + 675*x.^3 -
a = 0; 900*x.^4 + 400*x.^5;
b = 0.8; np=3*na+1;
n = 100; %for single (n=1) x=linspace(a,b,np);
f = @(x) 0.2 + 25*x - 200*x.^2 + 675*x.^3 - u=x;
900*x.^4 + 400*x.^5; s=0;
h = (b-a)/n; for i=1:np
I = f(a)+f(b); u(i)=f(x(i));
for i = (a+h):h:(b-h) if mod(i,3)==0
I = I + 2*f(i); s=s+2*u(i);
end else
I = I*h/2 s=s+3*u(i);
end
end
MATLAB code for single application of Simpson 1/3 s=s-u(1)-u(np);
A=3*s*(x(2)-x(1))/8
close all
clc
a = 0;
b = 0.8;
n = 2;
f = @(x) 0.2 + 25*x - 200*x.^2 + 675*x.^3 -
900*x.^4 + 400*x.^5;
h = (b-a)/n;
I = f(a)+f(b);
for i = (a+h):h:(b-h)
I = I + 4*f(i);
end
I = h/3*I

MATLAB code for multiple application of Simpson 1/3


clear all
close all
clc
a = 0;
b = 0.8;
n = 100;
f = @(x) 0.2 + 25*x - 200*x.^2 + 675*x.^3 -
900*x.^4 + 400*x.^5;
h = (b-a)/n;
I1 = f(a)+f(b);
for i = (a+h):2*h:(b-h)
I1 = I1 + 4*f(i);
end
I = I1;
for k = (a+(2*h)):2*h:(b-(2*h))
I = I + 2*f(k);
end
I = h/3*I

You might also like