code in MATLAB Roots of Non-Linear Equations For the problems below, you may ass
ID: 3817735 • Letter: C
Question
code in MATLAB
Roots of Non-Linear Equations For the problems below, you may assume that all (numeric) function input arguments are scalars.
QUESTION:Write a function with the header: function [xNew] = myRegulaFalsi(f, xL, xR, tol) which takes as input f: a function handle a: the initial left bracket around a root b: the initial right bracket around a root tol: a tolerance above which the algorithm will keep iterating. And uses the False Position method to return the root (xNew).
Tips: • Be sure to include an iteration counter which will stop the while-loop if the number of iterations get greater than 100. • It is not necessary to print out a convergence table within the while loop. (I.e., there should be no fprintf statements in your code)
Test Case:
>> format long
>> f = @(x) 2*(1-cos(x))+4*(1-sqrt(1-(0.5*sin(x)).^2)) - 1.2;
>> [root] = myRegulaFalsi(f, 0, 1, .0001)
root =
0.958190408393233
Explanation / Answer
function root = MyRegulaFalsi(f,a,b,tolerance) // Function of Regula Falsi
if (nargin < 3)
error('Wrong number of input arguments. Please enter all arguments'); // no of input arguments checked
elseif (nargin == 3 || nargin == 4)
i = 0;
tol = 1e-6;
g = 1;
while(g > tol && tol<100) // Tolerance is <100
i = i + 1;
if (f(a) == f(b)) // conditions checked for roots
disp('Denominator in Regula falsi is zero');
root = 'Regula falsi can''t compute the root';
return;
end
c = a - ((f(a)*(b-a))/(f(b) - f(a))); // c is declared
if(f(c)*f(a) > 0) // Conditions checked for the given function
b = c;
g = f(b);
root = b;
else
a = c;
g = f(a);
root = a;
end
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.