the regula falis method. f(x)=x2 −6x+4. in matlab

main.m
% Define the function
f = @(x) x^2 - 6*x + 4;

% Define the interval [a,b] (initial guesses for the root)
a = 0;
b = 3;

% Set the tolerance level
tol = 1e-6;

% Set the maximum number of iterations
max_iter = 1000;

% Implement the Regula Falsi method
for i = 1:max_iter
    c = (a*f(b) - b*f(a)) / (f(b) - f(a));
    
    if f(c) == 0 || (b-a) < tol
        disp('Root found at:');
        disp(c);
        break;
    end
    
    if f(c)*f(a) < 0
        b = c;
    else
        a = c;
    end
end
501 chars
30 lines

related categories

gistlibby LogSnag