Question
Edit the Regula Falsi code to use a functional stopping criteria that will stop the code when either the process reaches the maximum iterations or
Edit the Regula Falsi code to use a functional stopping criteria that will stop the code when either the process reaches the maximum iterations or when∣f(xn)∣<ftol for a specified tolerance ftol replacing the current error tolerance. Make the code useftol=1×10−4by default if that argument slot is left empty. Include the adapted code using the lab's naming scheme, and provide a comparison of the two stopping criteria for discussion in the pdf report. Are there any pros or cons? Is there any scenarios where one stopping criteria might be better or worse than the other?
Code:
function [r, k] = RegulaFalsi(f, a, b, kmax, tol)
%
% RegulaFalsi uses the regula falsi method to approximate a root of f(x) = 0
% in the interval [a,b].
%
% [r, k] = RegulaFalsi(f, a, b, kmax, tol), where
%
% f is an anonymous function representing f(x),
% a and b are the limits of interval [a,b],
% kmax is the maximum number of iterations (default 20),
% tol is the scalar tolerance for convergence (default 1e-4),
%
% r is the approximate root of f(x) = 0,
% k is the number of iterations needed for convergence.
%
if nargin < 5 || isempty(tol), tol = 1e-4; end
if nargin < 4 || isempty(kmax), kmax = 20; end
c = zeros(1,kmax); % Pre-allocate
if f(a)*f(b) > 0
r = 'failure';
return
end
disp(' k a b')
for k = 1:kmax,
c(k) = (a*f(b)-b*f(a))/(f(b)-f(a)); % Find the x-intercept
if f(c(k)) == 0 % Stop if a root has been found
return
end
fprintf('%2i %11.6f%11.6f',k,a,b)
if f(b)*f(c(k)) > 0 % Check sign changes
b = c(k); % Adjust the endpoint of interval
else a = c(k);
end
c(k+1) = (a*f(b)-b*f(a))/(f(b)-f(a)); % Find the next x-intercept
if abs(c(k+1)-c(k)) < tol, % Stop if tolerance is met
r = c(k+1);
return
end
end
Step by Step Solution
3.45 Rating (164 Votes )
There are 3 Steps involved in it
Step: 1
Here is the adapted code with a functional stopping criteria using the absolute error tolerance ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started