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

(MATLAB A1) Linear+Nonlinear

The document provides algorithms and code implementations for numerical methods to solve non-linear equations, including: 1. The bisection method for root finding in one dimension. 2. Regular false position, Newton-Raphson, and secant methods for solving non-linear equations. 3. Gaussian elimination, Gaussian-Jordan elimination, Gauss-Jacobi, and Gauss-Seidel methods for solving systems of linear equations. Code examples are given for each method.

Uploaded by

Ahsan Farooq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

(MATLAB A1) Linear+Nonlinear

The document provides algorithms and code implementations for numerical methods to solve non-linear equations, including: 1. The bisection method for root finding in one dimension. 2. Regular false position, Newton-Raphson, and secant methods for solving non-linear equations. 3. Gaussian elimination, Gaussian-Jordan elimination, Gauss-Jacobi, and Gauss-Seidel methods for solving systems of linear equations. Code examples are given for each method.

Uploaded by

Ahsan Farooq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

NON-LINEAR EQUATIONS METHODS

BISECTION METHOD
function [x,w]=bisection(a,b,n,fun)
for w=1:n
fa=fun(a);
fb=fun(b);
c=(a+b)/2;
fc=fun(c);
fprintf('x%d = %.4f\n',1,c)
if fa*fc<0
b=c;
fb=fc;
else if fb*fc<0
a=c;
fa=fc;
end
x=c;
end
end
REGULAR FALSE METHOD
function [xr] = regulafals1(x1,xu,es,fun)
i=0;
fprintf(' iter x1 xr xu f(xr)
ea\n');
f1=fun(x1);
fu=fun(xu);
xr=((x1*fu)-(xu*f1))/(fu-f1);
ea=es+1;
while ea>es;
xold=xr;
fr=fun(xr);
fprintf('%3d %12.8f %12.8f %12.8f %12.8f %12.8e\n',
[i,x1,xr,xu,fr,ea]')
if f1*fr<0
xu=xr;
elseif fu*fr<0
x1=xr;
else
break
end
i=i+1;
xr=((x1*fu)-(xu*f1))/(fu-f1);
ea=abs((xr-xold)/xr);
end
fprintf('iteration no is %d\n',i)
fprintf('root is %f\n',xr)
fprintf(' function value at xr is %f\n',fr)
end
NEWTON RAPHSON METHOD
function [xr]=newton1(xi,xu,es,fun,dif)
i=0;
ea=es+1;
fprintf(' iter x fx fd xi ea\n');
while ea>es
x=xi;
fx=fun(x);
fd=dif(x);
xi=x-(fx/fd);
ea=abs((xi-x)/xi);
fprintf('%3d %12.8f %12.8f %12.8f %12.8f %12.8f\n',[i,x,fx,fd,xi,ea]')
i=i+1;
end
sol=xi;
fr=fun(sol);
fprintf('iteration no is %d\n',i)
fprintf('root is %f\n',sol)
fprintf('function value at xr is %f\n',fr)
end
SECANT METHOD
function[R]=secantW(a,b,c,fun)
i=0;
fprintf(' iter a R B f(R) o\
n')
fa=fun(a);
fb=fun(b);
R=((a*fb)-(b*fa))/(fb-fa);
o=c+1;
while o>c
Rold=R;
fr=fun(R);
fprintf('%3d %12.8f %12.8f %12.8f %12.8f %12.8e\n',
[i,a,R,b,fr,o]')
a=b;
b=R;
fa=fun(a);
fb=fun(b);
i=i+1;
R=((a*fb)-(b*fa))/(fb-fa);
o=abs((R-Rold)/R);
end
LINEAR EQUATIONS METHODS

GAUSS ELIMINATION METHOD

function [y]=gausselimination2(A)
for n = 1: (length(A)-1)
% step 1: make the row N's Nth term 1 by dividing
% the whole row by it
s = A(n,:)
s = s/s(n)
A(n,:) = s
% step 2: for every other row add to it -1 * that rows Nth term *
% the Nth row
for k = 1:(length(A)-1)
if n<k
A(k,:) = s*(-1*A(k,n)) + A(k,:)
end
end
end
x=zeros(3,1)
x(3)=A(3,end)/A(3,3)
x(2)=A(2,end)-(A(2,3)*x(3))/A(2,2)
x(1)=(A(1,end)-(A(1,3)*x(3))-(A(1,2)*x(2)))/A(1,1)
x
end
GAUSS JORDON ELIMINATION METHOD

function [y]=gaussjordon2(A)
for n = 1: (length(A)-1)
% step 1: make the row N's Nth term 1 by dividing
% the whole row by it
s = A(n,:)
s = s/s(n)
A(n,:) = s
% step 2: for every other row add to it -1 * that rows Nth term *
% the Nth row
for k = 1:(length(A)-1)
if n~=k
A(k,:) = s*(-1*A(k,n)) + A(k,:)
end
end
end
x=zeros(3,1)
x(3)=A(3,end)/A(3,3)
x(2)=A(2,end)-(A(2,3)*x(3))/A(2,2)
x(1)=(A(1,end)-(A(1,3)*x(3))-(A(1,2)*x(2)))/A(1,1)
x
end
GAUSS JACOBI METHOD
%define A and B
A=[83,11,-4,;7,52,13;3,8,29];
B=[95;104;71]
%tolerance
tol=1e-2;
[M,N]=size (A);
%check for diagonally dominant
for m=1:M
row=abs(A(m,:));
d=sum(row)-row(m);
if row(m)<=d
error('A is not diagonally dominant but it should be');
end
end
x=zeros(M,1);
err=tol+1
i=0;
while err>tol
for j=1:N
x(j)=(B(j)-A(j,1:j-1)*x(1:j-1)-A(j,j+1:N)*x(j+1:N))/A(j,j);
end
i=i+1;
err=max(abs((A*x)-B));
end
fprintf('number of iteration is %d',i)
x
GAUSS SIEDEL METHOD
A=input('Enter a coefficient matrix A:');
B=input ('Enter source vector B:');
P=input('enter initial guess vector:');
n=input('enter no of iterations:');
e=input('enter your tolerance:');
N=length(B);
X=zeros(N,1);
for j=1:n
for i=1:N

X(1)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
P(i)=X(i)
end
fprintf('iteration no %d\n',j)
X
end

You might also like