0% found this document useful (0 votes)
14 views2 pages

Regular Falsi (Main) .M

Uploaded by

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

Regular Falsi (Main) .M

Uploaded by

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

% Clearing Screen

clc;

% Setting x as symbolic variable


syms x;

% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first initial guess: ');
b = input('Enter second initial guess: ');
e = input('Enter tolerable error: ');
N = input('Enter maximum number of steps: ');

% Initializing step counter


step = 1;

% Check initial values


fa = eval(subs(y, x, a));
fb = eval(subs(y, x, b));

if fa * fb > 0
disp('Error! Function has the same sign at both endpoints.');
return;
end

% Starting the iteration


while step <= N
fa = eval(subs(y, x, a));
fb = eval(subs(y, x, b));

c = double((a * fb - b * fa) / (fb - fa));


fc = eval(subs(y, x, c));
tol = abs(b-a)/2;

fprintf('Step = %d\ta = %f\tb = %f\tc = %f\tf(c) = %f\n', step, a, b, c, fc);

if tol < e
fprintf('Root is %f\n', c);
break;
end

% Prepare for next iteration


if fa * fc < 0
b = c; % Root lies between a and c
else
a = c; % Root lies between c and b
end

% Storing iteration data for plotting


iterationData(step, :) = [step, tol];
step = step + 1;
end

if step > N
disp('Maximum number of steps exceeded.');
end

% Plotting error
figure(1);
plot(iterationData(:,1), iterationData(:,2), 'r-o');
xlabel('Number of iteration');
ylabel('Error');
title('Regula-Falsi Method');
legend('Error evolution');

You might also like