Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please solve problem 3 only, I attached problem 1 as a reference. Please solve i

ID: 3789593 • Letter: P

Question


Please solve problem 3 only, I attached problem 1 as a reference.
Please solve it by MATLAB. 2. Problems solved with Matlab. Problem 3 Write a MATLAB user-defined function that solves a nonlinear equation f(x) 0 with the regula- falsi method. Name the function Xs RegulaRoot(Fun,a,b,Ea), where the output argument Xs is the solution. The input argument Fun is a name for the function that calculate fx) for a given x that bracket the root, and Ea the estimated relative error. The program should include the following features: Check if points a and b are on opposite sides of the solution. If not, the program should stop and display an error message. The number of iterations should be limited to 100 (to avoid an infinite loop). If a solution with the required accuracy is not obtained in 100 iterations, the program should stop and display an error message. Use the function RegulaRoot to solve Problem 1 Use Ea 0.01%

Explanation / Answer

function root = regularoot(fun,a,b,ea)


if (nargin < 3)
error('Argument is wrong');

elseif (nargin == 3)
Iteration = 0;
ea = 1e-4;   
g1 = 1;
while(g1 > ea)
Iteration = Iteration + 1;
if (fun(a) == fun(b))
fprintf('Function is ame at %d', Iteration);
disp('Denominat is zero');
root = 'can''t compute the root';
return;
end
x = a - ((fun(a)*(b-a))/(fun(b) - fun(a)));
if(f(x)*f(a) > 0)
b = x;
g1 = fun(b);
root = b;
else
a = x;
g1 = fun(a);
root = a;
end
fprintf('%4d', Iteration);
fprintf('%f', a);
fprintf('%f', b);
fprintf('%f', x);
fprintf('%f', fun(a));
fprintf('%f', fun(b));

end
  
elseif (nargin == 4)
Iteration = 0;
g1 = 1;
  
while(g1 > ea)
Iteration = Iteration + 1;
if (fun(a) == fun(b))
fprintf('Function is same at %d', Iteration);
disp('Denominator is zero');
root = 'can''t compute the root';
return;
end
x = a - ((fun(a)*(b-a))/(fun(b) - fun(a)));
if(fun(x)*fun(a) > 0)
b = x;
g1 = fun(b);
root = b;
else
a = x;
g1 = fun(a);
root = a;
end
fprintf('%3d', Iteration);
fprintf('%f', a);
fprintf('%f', b);
fprintf('%f', x);
fprintf('%f', fun(a));
fprintf('%f', fun(b));
fprintf(' ');
end
  
elseif (nargin > 4)
error('Too many input arguments');
end