You are on page 1of 7

Bisection Method

function p = bisection(f,a,b)
% provide the equation you want to solve with R.H.S = 0 form.
% Write the L.H.S by using inline function
% Give initial guesses.
% Solves it by method of bisection.
% A very simple code. But may come handy
if f(a)*f(b)>0
disp('Wrong choice bro')
else
p = (a + b)/2;
err = abs(f(p));
while err > 1e-7
if f(a)*f(p)<0
b = p;
else
a = p;
end
p = (a + b)/2;
err = abs(f(p));
end
end

Gauss Seidel
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code finds the solution to a system of simultaneous linear equation
% using Gauss_Seidel Method.
% Written By: Mohammad Y. Saadeh, on 06/14/2010, University of Nevada Las
% Vegas
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%A%%%%%%%%%%%%%%%%%%%%%%%%%
clear;clc
format compact
%% Read or Input any square Matrix
A = [-6 2 1 2 1;
3 8 -4 1 0;
-1 1 4 10 1;
3 -4 1 9 2;
2 0 1 3 10];% coefficients matrix
C = [3;4;-2 ;12;1];% constants vector
n = length(C);
X = zeros(n,1);
Error_eval = ones(n,1);
%% Check if the matrix A is diagonally dominant
for i = 1:n
j = 1:n;
j(i) = [];
B = abs(A(i,j));
Check(i) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than the remaining
row values combined?
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
end
end
%% Start the Iterative method
iteration = 0;
while max(Error_eval) > 0.001
iteration = iteration + 1;
Z = X; % save current values to calculate error later
for i = 1:n
j = 1:n; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i) = (C(i) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xsolution(:,iteration) = X;
Error_eval = sqrt((X - Z).^2);
end
%% Display Results
GaussSeidelTable = [1:iteration;Xsolution]'
MaTrIx = [A X C]

%% Gauss Seidel Method


%% Solution of x in Ax=b using Gauss Seidel Method
% * _*Initailize 'A' 'b' & intial guess 'x'*_
%%
A=[5 -2 3 0;-3 9 1 -2;2 -1 -7 1; 4 3 -5 7]
b=[-1 2 3 0.5]'
x=[0 0 0 0]'
n=size(x,1);
normVal=Inf;
%%
% * _*Tolerence for method*_
tol=1e-5; itr=0;
%% Algorithm: Gauss Seidel Method
%%
while normVal>tol
x_old=x;

for i=1:n

sigma=0;

for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end

for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end

x(i)=(1/A(i,i))*(b(i)-sigma);
end

itr=itr+1;
normVal=norm(x_old-x);
end
%%
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d iterations',x,itr);

Regula Falsi
clc
clear all
syms x;
fun=input('enter the function as a variable of x:');
f=inline(fun);
% to find the value of a and b for given function
for j=0:inf
g=f(j);
g1=f(j+1);
if sign(g)~=sign(g1)
a=j;
b=j+1;
break
end
end

for i=0:inf
y=(a*f(b)-b*f(a))/(f(b)-f(a));
if sign(f(y))==sign(f(a))
a=double(y);
b=b;
elseif sign(f(y))==sign(f(b))
b=double(y);
a=a;
end
y1=(a*f(b)-b*f(a))/(f(b)-f(a));
if y==y1
answer=y;
break
end
end
fprintf('the total number of iterations are: ')
i
answer

function ModRegFal = ModRegFal(a, b, n)


format long;
a = input('Enter a value for lower boundary a: ');
b = input('Enter a value for upper boundary b: ');
n = input('How small should should the error be (to what -power)? ');
if (f(a)*f(b) > 0 )
disp ('Invalid values of a and b. Program Closing')
return;
end;
F = f(a);
G = f(b);
w0 = a;
while (1)
wn = (G*a-F*b)/(G-F);
disp([a b wn w0]) %%just checking where the values are, and it they look correct
if f(a)*f(wn) > 0
disp('ranif 1')%%just checking where the values are, and it they look correct
b = wn;
G = f(wn);
if f(w0)*f(wn) > 0
F = F/ 2; end;
disp('ranif 2')%%just checking where the values are, and it they look
correct
disp([a b wn w0])%%just checking where the values are, and it they look
correct
else
a = wn;
F = f(wn);
if f(w0)*f(wn) > 0
disp('ranif 3')%%just checking where the values are, and it they look
correct
disp([a b wn w0])%%just checking where the values are, and it they
look correct
G = G/ 2; end;
end
disp([a b wn w0])
if (abs((wn - w0)/wn) < 0.5*10^-n)
disp ('The root is: ')
disp (wn)
break;
else
w0 = wn;
end
end

Newton Raphson
x = 0.05;
x_old = 100;
x_true = 0.0623776;
iter = 0;
while abs(x_old-x) > 10^-3 && x ~= 0
x_old = x;
x = x - (x^3 - 0.165*x^2 + 3.993*10^-4)/(3*x^2 - 0.33*x);
iter = iter + 1;
fprintf('Iteration %d: x=%.20f, err=%.20f\n', iter, x, x_true-x);
pause;
end
You can plot the function with, for example:
x = -10:0.01:10;
f = x.^3 - 0.165*x.^2 + 3.993*10^-4;
figure;
plot(f)
grid on

Secant Method
% Find the roots of using Secant Method
% func --> function is taken by user
% like @(x)(sin(x)) or @(x)(x^2 + x - 1) or any other function
% but use the same format i.e. use paranthesis as used above with '@' and 'x'
% maxerr --> Maximum Error in finding Root
clc;
clear;
maxiter = 100; % max number of iteration before get the answer
f = input('Enter Function in terms of x: ');
xn_2 = input('Ener Lower Limit: ');
xn_1 = input('Ener Upper Limit: ');
maxerr = input('Enter Maximum Error: ');
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));
disp('xn-2 f(xn-2) xn-1 f(xn-1)
xn f(xn)');
disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));
flag = 1;
while abs(f(xn)) > maxerr
xn_2 = xn_1;
xn_1 = xn;
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));

disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));

flag = flag + 1;
if(flag == maxiter)
break;
end
end
if flag < maxiter
display(['Root is x = ' num2str(xn)]);
else
display('Root does not exist');
end

iteration method
% Fixed-Point Iteration Numerical Method for finding the x root of f(x) to make f(x) = 0
% by Roche de Guzman, Ph.D.
% Hofstra University
function [xR,err,n,xRV,errV,AFD1,AFD2] = FixedPointNM(AF,xi,ed)
% Inputs: with examples
% AF = anonymous function equation: AF = @(x) 1-((20^2)./(9.81*(((3*x)+
((x.^2)/2)).^3))).*(3+x);
% xi = initial guess x = xR, where xR = x root: xi = 0.5;
% ed = desired approximate relative error = |(current - previous)/current|: ed = 0.01;
% Outputs
% xR = x root
% err = approximate relative error
% n = number of iterations
% xRV = x root vector
% errV = approximate relative error vector
% AFD1 = anonymous function 1st derivative
% AFD2 = anonymous function 2nd derivative
%% Computations
% Derivatives
AFSYM = sym(AF); % symbolic math function
AFD1SYM = diff(AFSYM); % symbolic math function 1st derivative
AFD2SYM = diff(AFD1SYM); % symbolic math function 2nd derivative
AFD1 = matlabFunction(AFD1SYM); % anonymous function 1st derivative
AFD2 = matlabFunction(AFD2SYM); % anonymous function 2nd derivative
% Fixed-Point Iteration Method
err = 1; % initial approximate relative error = 100%
k = 1; % initial counter
while ed < err % compares desired versus calculated error
xR = xi+AF(xi); % x root using fixed point iteration method
xRV(k+1) = xR; % stores the x root per iteration in a vector
err = abs((xRV(k+1) - xRV(k))/xRV(k+1)); % approximate relative error
errV(k+1) = err; % stores the error into a vector
xi = xR; % new guess x = xR, where xR = x root
k = k+1; % increase counter
end
n = k - 1; % number of iterations
xRV = xRV(2:end); % readjust x root vector
errV = errV(2:end); % readjust approximate relative error vector

% Finding the nontrivial root of


% f(x) = sin(x) - x^2
% using the Simple Fixed-Point Iteration
clear all
x = 1.0 %initial guess
Es = 0.1 %tolerance
Ea = 1000; %randomly large relative approximate error
xold = x;
n = 0; %iteration counter
while Ea > Es
x = sqrt(sin(x));
Ea = abs((x-xold)/x)*100;
xold = x;
n = n + 1;
end
x %the root
n %number of iterations

You might also like