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

lab

The document provides MATLAB code implementations for various numerical methods to find roots of functions, including Fixed Point Iteration, Newton Raphson, Bisection Method, Regula Falsi, and Secant Method. Each method prompts the user for necessary inputs such as the function, initial guesses, tolerance, and number of iterations, and outputs the results of each iteration. The code includes checks for convergence and conditions for failure in the Newton Raphson and Bisection methods.

Uploaded by

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

lab

The document provides MATLAB code implementations for various numerical methods to find roots of functions, including Fixed Point Iteration, Newton Raphson, Bisection Method, Regula Falsi, and Secant Method. Each method prompts the user for necessary inputs such as the function, initial guesses, tolerance, and number of iterations, and outputs the results of each iteration. The code includes checks for convergence and conditions for failure in the Newton Raphson and Bisection methods.

Uploaded by

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

%Fixed point itteration

g = input('Enter your functon: ');


x0= input ('Enter initial guess: ');
e = input('Enter tolarance: ');
n = input('Enter on of iterations: ');
for i=1:n
x1 = g(x0);
fprintf('x%d = %.5f\n',i,x1)
if(x1-x0)<e
break
end
x0 = x1;
end

%Newton Raphson
f = input ('Enter your function: ');
df = input ('Enter derivative of this fuction: ');
e = input ('Enter tolerance: ');
x0 = input ('Enter the initial guess: ');
n = input ('Enter the no of iterration: ');
%processing
if df(x0)~=0
for i=1:n
x1 = x0 -f(x0)/df(x0);
fprintf('x%d=%.4f\n',i,x1)
if abs(x1-x0)<e
break
end
x0 = x1;
end
else
disp('Newton Raphson Failed')
end

%bisection method
f = input ('Enter yout function: ');
a = input ('Enter left side of interval: ');
b = input ('Enter Right side of interval: ');
n = input ('Enter no of iterations: ');
e = input ('Enter tolerance: ');
if f(a)*f(b)<0
for i=1:n
c = (a+b)/2;
fprintf('p%d=%.4f\n',i,c)
if abs(c-b)<e || abs(c-a)<e
break
end
if f(a)*f(c)<0
b=c;
elseif f(b)*f(c)<0
a=c;
end
end
else
disp ('No root between given method')
end

%Regula Falsi
f = input ('Enter yout function: ');
a = input ('Enter left side of interval: ');
b = input ('Enter Right side of interval: ');
n = input ('Enter no of iterations: ');
e = input ('Enter tolerance: ');
if f(a)*f(b)<0 && a<b
for i=1:n
c = (a*f(b)-b*f(a))/(f(b)-f(a));
fprintf('p%d=%.4f\n',i,c)
if abs (f(c))<e
break
end
if f(a)*f(c)<0
b=c; % putting the value of "c" to "b"
elseif f(b)*f(c)<0
a=c; % putting the value of "c" to "a"
end
end
else
disp ('No root between given method')
end

%Secant method
f=input('Enter the function:');
x0=input('Enter the first intial guess:');
x1=input('Enter the 2nd intial guess:');
e=input('Enter the tolerance:');
n=input('Enter no of iteration:');
%processing
for i=2:n
x2=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
fprintf('x%d=%0.10f\n',i,x2)
if abs(x2-x1)<e
break
end
x0=x1;
x1=x2;
end

You might also like