0% found this document useful (0 votes)
11 views7 pages

all methods

The document provides a comprehensive overview of various numerical methods for root-finding, including the Newton-Raphson method, Fixed-point iteration, Secant method, Modified Secant method, Bisection method, and False position method. Each method is illustrated with MATLAB code snippets that demonstrate the implementation and convergence of the algorithms. Additionally, graphical representations are included to visualize the convergence of the methods.

Uploaded by

fabihaanbar
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)
11 views7 pages

all methods

The document provides a comprehensive overview of various numerical methods for root-finding, including the Newton-Raphson method, Fixed-point iteration, Secant method, Modified Secant method, Bisection method, and False position method. Each method is illustrated with MATLAB code snippets that demonstrate the implementation and convergence of the algorithms. Additionally, graphical representations are included to visualize the convergence of the methods.

Uploaded by

fabihaanbar
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/ 7

NR:

% Initial guess and accepted error


xr = 0;
accepted_error = 0.0005;
ea = 1;

% Initial outside loop step


no_iterations = 1;
x_n = xr - (f(xr) / g(xr));
xr = x_n;

% Inside loop
while ea > accepted_error
x_n = xr - (f(xr) / g(xr));
ea = abs((x_n - xr) / x_n);
xr = x_n;
no_iterations = no_iterations + 1;
end

fprintf("Root from Newton-Raphson= %f\n", x_n)


fprintf("Number of iterations required in Newton-Raphson= %d\n",
no_iterations)
NR with graph:
clc
clear all
syms x
f_sym = x^10 - 1; % Symbolic function
g_sym = diff(f_sym, x); % Symbolic derivative

f = matlabFunction(f_sym); % Function handle for f(x)


g = matlabFunction(g_sym); % Function handle for g(x)

% Initial guess and accepted error


xr = 0.5;
accepted_error = 0.0005;
ea = 1;

% Initialize iteration storage


iterations = [];
no_iterations = 1;

% Initial step
x_n = xr - (f(xr) / g(xr));
iterations(end + 1) = xr; % Store initial guess
xr = x_n;

% Loop until error is below accepted threshold


while ea > accepted_error
x_n = xr - (f(xr) / g(xr));
ea = abs((x_n - xr) / x_n);
iterations(end + 1) = xr; % Store current iteration point
xr = x_n;
no_iterations = no_iterations + 1;
end
fprintf("Root from Newton-Raphson= %f\n", x_n)
fprintf("Number of iterations required in Newton-Raphson= %d\n",
no_iterations)

% Plotting
x_vals = linspace(0, 1.5, 1000); % Limit x range for clearer view
y_vals = f(x_vals); % Function values for f(x)

figure
plot(x_vals, y_vals, 'b', 'LineWidth', 2) % Plot f(x) in blue
hold on
plot(iterations, f(iterations), 'ro', 'MarkerSize', 8) % Plot iteration
points in red
yline(0, 'r--', 'Final Root', 'LineWidth', 1.5) % Add horizontal line at y=0
hold off

% Adjust plot properties


xlim([0, 1.5]) % Limit x-axis range
ylim([-1, 2]) % Adjust y-axis for better view
xlabel('x')
ylabel('f(x)')
title('Newton-Raphson Method Convergence')
legend('f(x)', 'Iterations', 'Final Root')
grid on

Fixed_point iteration
i=1;
imax=10;
xo=0;
tolerance=0.1;
ea=zeros(1,imax);
val=zeros(1,imax);
f=@(x)exp(-x);
while i<imax
xnew=f(xo);
xold=xo;
val(i)=xnew;
ea(i)=abs((xnew-xold)/xnew)*100;
if ea(i)<tolerance
break
end
xo=xnew;
i=i+1;
end
xo

Secant method

clc
clear all
close all
f=inline(input("Enter the function",'s'))
a=input("Enter the upper limit")
b=input("Enter the lower limit")
maxerr=input("enter max err in %")
c=(a*f(b)-b*f(a))/(f(b)-f(a));
while abs(a-b)>maxerr
a=b;
b=c;
c=b-(f(b)*(a-b)/(f(a)-f(b)));
end
x=0:0.5:10
disp(['Root: ' num2str(c)])
plot(x,f(x));grid
title('Entered function ')
hold on
plot([c,c],[5,-5],'g','linewidth',2)

modified secant method:

clc
clear all

syms x
format long
f(x) =0.95*x^3-5.9*x^2+10.9*x-6

disp("Initial Guesses")

Initial Guesses

x1=2.5
x0=3.5

accepted_error=0.000001;
ea=1;
i=0;
while ea>accepted_error
dx=eval(f(x1)-f(x0))/(x1-x0);
xp=x1;
x1=x1-eval(f(x1))/dx;
x0=xp;
ea=abs(x1-x0)/x1;
i=i+1;
end
root=x1

no_of_iteration=i

Bisection method:
clc
clear all
format long
%function input
f=inline(input("Enter function = ",'s'))
%acceptable error percentage input
ae=input("Enter the accepted error percentage value = ");
xl=input("Enter lower limit:")
xu=input("Enter upper limit:")

ea=100;
%first iteration outside loop
i=1;
xr= (xl+xu)/2;
x_prev=xr;

%continuing iterations inside loop with error criterion


while ea>ae
if f(xr)*f(xl)<0
xu=xr;
elseif f(xr)*f(xl)==0
break
else xl=xr;
end
xr= (xl+xu)/2;
ea=abs((x_prev-xr)/xr)*100;
i=i+1;
x_prev=xr;
end

xr
i

False position method:


clc
clear all
format long
%function input
f=inline(input("Enter function = ",'s'))
%acceptable error percentage input
ae=input("Enter the accepted error percentage value = ");
xl=input("Enter lower limit:")
xu=input("Enter upper limit:")

ea=100;
%first iteration outside loop
i=1;xr= (xu*f(xl)-xl*f(xu))/(f(xl)-f(xu));xr= (xl+xu)/2;
x_prev=xr;

%continuing iterations inside loop with error criterion


while ea>ae
if f(xr)*f(xl)<0
xu=xr;
elseif f(xr)*f(xl)==0
break
else xl=xr;
end
xr= (xu*f(xl)-xl*f(xu))/(f(xl)-f(xu));
ea=abs((x_prev-xr)/xr)*100;
i=i+1;
x_prev=xr;
end

xr
i

False position using graph:

clc;
clear all;
format long;

% Input the function and parameters


f = str2func(['@(x) ' input("Enter function = ", 's')]); % Create
function handle from input
ae = input("Enter the accepted error percentage value = "); % Accepted
error
xl = input("Enter lower limit: "); % Lower bound
xu = input("Enter upper limit: "); % Upper bound

% Initialize variables
ea = 100; % Initial error (100% to
enter loop)
i = 1; % Iteration counter
xr = (xl + xu) / 2; % First midpoint
estimate
x_prev = xr; % Store previous root
estimate
iterations = [xr]; % Store root estimates
for plotting

% Start iterative process with error criterion


while ea > ae
% Check the sign of f(xr) and adjust bounds
if f(xr) * f(xl) < 0
xu = xr;
elseif f(xr) * f(xl) == 0
break; % Exact root found
else
xl = xr;
end

% Update root estimate using the False Position method


xr = (xu * f(xl) - xl * f(xu)) / (f(xl) - f(xu));

% Calculate error and update previous root estimate


ea = abs((x_prev - xr) / xr) * 100;
iterations(end + 1) = xr; % Store current root estimate
x_prev = xr; % Update previous estimate for next
iteration
i = i + 1; % Increment iteration count
end

% Display final root and number of iterations


fprintf("Estimated Root = %f\n", xr);
fprintf("Number of iterations = %d\n", i);

% Plotting the function and root convergence


x_vals = linspace(xl, xu, 1000); % X values for plot
range
y_vals = arrayfun(f, x_vals); % Function values for
plotting

figure;
plot(x_vals, y_vals, 'b', 'LineWidth', 2); % Plot function in blue
hold on;
plot(iterations, arrayfun(f, iterations), 'ro', 'MarkerSize', 8); % Plot
iteration points in red
yline(0, 'r--', 'Final Root', 'LineWidth', 1.5); % Horizontal line at y=0
for root
hold off;
% Set plot limits and labels
xlabel('x');
ylabel('f(x)');
title('Root-Finding Method Convergence');
legend('f(x)', 'Iterations', 'Final Root');
grid on;

You might also like