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

Problems to be solved using self-developed seripts (and/or) functions in MATLAB

ID: 2247909 • Letter: P

Question

Problems to be solved using self-developed seripts (and/or) functions in MATLAB . Textbook: 3.21 Modified Regula Falsi Method Frequently, in the regula falsi method, one of the endpoints of the interval stays the same in all iterations, while the other endpoint advances toward the root (Fig.1). In the modified regula falsi method, when this situation occurs, the straight line that connects the endpoints of the interval is replaced with a line that has a smaller slope. As shown in Fig.2, this is done by dividing by 2 the value of the function at the end point that stays the same. Consequently, the line intersects the x-axis closer to the root. Mx) ai Aetual b olution Figure 1: Regula Falsi method 0) Write a MATLAB user-defined function that solves a non-linear equation of the form (f(x with the modified regula falsi method. The program should check if the endpoints stay the same; if an endpoint is unchanged in three iterations, the value of the function at that point should be divided by 2. If the same endpoint is unchanged for three more iterations, the value of the function at that point should be divided again by 2(the true value now is divided by 4), and so on. Name the function Xs Regula Root Mod(Fun, a, b, ErrMaz), where the output argument Xs is the solution. The input argument Fun is the name of the function that calculates f(r) for a given r, a and b are two points that bracket the root, and Err Mar the maximum estimated relative error I ction at that ree more iterations, the true value now is a Root Mod( Fun, a, b, ErrMar), where the input argument Fun is the name of the function that and b are two points that bracket the root, and ErrMaz the ve error

Explanation / Answer

Code:


function a = regula_falsi( f )

f = @(x) x^3 -2*x - 5;
   % for Regula Falsi method
  % f is a function for which root to be found.
% asking for the range
R= input ( 'You are looking for the roots in [ x_min , x_max] : ');

% check for range
[ nr , mr ] = size( R);
if nr ~= 1 || mr~= 2
disp( 'Input error ..The matrix should be 1x2 matrix')
return
end
% if root lies in the boundary

if feval( f , R( 1,1) )* feval( f , R(1,2)) == 0
if feval( f , R( 1,1) ) == 0
R(1,1)
return
else
feval( f , R(1,2)) == 0
R(1,2)
return
end
end
% condition for convergence
if feval( f , R( 1,1) )* feval( f , R(1,2)) > 0
disp ( 'Either NO root lies in the given range or EVEN no of roots')
disp( 'lie in the given range and hence this method cannot be applied.');
return
end
%error allowed in final answer
tol = abs(input(' Enter the error allowed in the final answer :'));
% no of iterations to be performed
n = input('Enter the maximum number of iterations to be performed :');
%initializing the value of k and matrix X
k=1;
X= zeros(n+1,3);
%initial display of table & initializing values for look
disp(sprintf(' iterate value of x error'));
x0= R(1,1); x1= R(1,2); x_disp= x0; err = x1-x0;
disp(sprintf (' %3d %11.5f %11.5f ', 0, x_disp,err));
% iteration loop starts

while k <=n && abs(err) > tol
x = x1 - (x1-x0)/( feval(f,x1)-feval(f,x0) ) *feval(f,x1);%REGULA FALSI formula
if feval(f , x0) * feval(f , x) == 0
x
return
else
if feval(f,x0) * feval(f,x) <0
err = x - x1;
x1 = x;
x_disp=x1;
X(k,2) = x1;
else
err = x-x0;
x0 = x;
x_disp = x0;
X(k,2) = x0;
end
end
% storing values in the form of matrix
X(k,1) = k;
X(k,3) = abs(err);
disp(sprintf (' %3d %11.5f %11.5f ', k, x_disp,err));
k = k + 1;
end
if abs(err) > tol
  disp(sprintf ('The final answer obtained after %3d iterations is %10.10f with an error %10.10f ' , n , X(n,2),X(n,3)))
  disp('Try more no of iterations for desired accuracy')
  else
disp(sprintf ('The final answer obtained after %3d iterations is %10.10f with an error %10.10f ' , (k-1) , X((k-1),2),X((k-1),3)))
end

return
end

Output:

% pass f = @(x) (3/4 - ( (2*(sin(x)^3) ) / (3 ( x - (sin(x)*cos(x) )))))

% pass min and max range

% pass error allowed and interations

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote