Question

In: Computer Science

In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0,...

In MATLAB write a function secant.m to apply the secant method.

function [x,i] = secant(f, x0, x1, tol, maxiters) [x,i] = secant(f, x0, x1, tol, maxiters)

performs the secant method with f(x), starting at x_0 = x0 and x_1 = x1, and continuing until either |x_i+1 - x_i| <= tol, or maxiters iterations have been taken.

The number of iterations, i, is also returned. An error is raised if the first input is not a function handle. A warning is raised if the maximum number of iterations is reached without achieving the tolerance.

Test the function on the problem f(x) = 0.9 cos(x) - sqrt(x) = 0, with starting values x0 = 0 and x1 - 1, and a tolerance of 10^-8 and maxiters of 20.

Do not use previous answer on Chegg as it's not working

Thank you!

Solutions

Expert Solution

MATLAB code for the problem is provided below, please comment if any doubts related to program running or other quires related to this answer:

MATLAB code:

%Test code to define the function
%%function definition is at the end
%assign the given function
f = @(x)( 0.9*cos(x) - sqrt(x))

%set the inital values given in the question
x0 = 0;
x1 = 1;

%set the tolerance value
tol = 10^-8;

%set maximum iters value
maxiters = 20;

%execute the method using above values
[x, i] =secant(f,x0,x1,tol,maxiters)

%print the results
fprintf('The root, x = %f', x)
fprintf('f(x)= %f', f(x))


%%secant function definition

function [x,i] = secant(f,x0,x1,tol,maxiters)

   %find the first assumption
root = (x0*f(x1) - x1*f(x0))/(f(x1) - f(x0));

%set the iteration count as 0
iterationCount = 0;

%the infinte while loop to repeat till
%root gets with in the tolerance or
%exceed the maximum iteration number
while (1)
x0 = x1;
x1 = root;
  
%%Check if the tolerance limit reached
if(abs(x0 - x1 )< tol)
break;
end
  
       %find the next approximation
root = (x0*f(x1) - x1*f(x0))/(f(x1) - f(x0));

       %increase the iteration count
iterationCount = iterationCount + 1;
  
%%terminate if not toleration limit
   %reached with maximum iteration
   %with warning
if(iterationCount == maxiters)
disp('Warning!!!maximum iteration limit reached')
break;
end
end
  
   %assign the results
x=root;
i=iterationCount;
end

Output:


Related Solutions

Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all...
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all in one function). Your function must have the following signature function output = solve(f,options) % your code here end where the input is • f: the function in f(x) =0. options: is a struct type with the following fields o method: bisection, newton or secant tol: the tolerance for stopping the iterations. maximum_iterations: the maximum number of iterations allowed. initial_guess: that is P_0; if...
Use the secant Method to find a root for the function: f(x) = x^3 + 2x^2...
Use the secant Method to find a root for the function: f(x) = x^3 + 2x^2 + 10x -20, with x_0 = 2, and x_1 = 1.
Please use python or matlab. The function e^x −100x^2 =0 has three true solutions.Use secant method...
Please use python or matlab. The function e^x −100x^2 =0 has three true solutions.Use secant method to locate the solutions with tolerance 10^(−10).
On Matlab use BFGS Method to find the minimum of the following function: f(x) = x13...
On Matlab use BFGS Method to find the minimum of the following function: f(x) = x13 - 2x2x12 + x12 - x1using initial point (x0, y0) = (1, 2)T to start, and stop when f changes less than 0.0001
script for Secant Method. Using Matlab or Octave. The data: y = x.^2 - 1.2 x_lower...
script for Secant Method. Using Matlab or Octave. The data: y = x.^2 - 1.2 x_lower = 0.4 x_upper = 0.6 Es = 0.5*10^(2-n) n = 5
Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of...
Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of scalar nonlinear equa-tions. Use these programs to compute approximations to real roots of the following equations: exp(x)−3x^2=0, (1) x^3=x^2+x+1, (2) exp(x) =1/(0.1 +x^2), (3) and x= 1 + 0.3 cos(x). (4) Use an error tolerance tol=10^(−12). Display the obtained approximations to the roots of these equations, and compare the number of iterations, starting with the same initial values x0 for bisection and Newton methods,...
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (curve fit), and x for f(x).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (linear, quadratic, or cubic), and x for f(x).
Starting from the point (x0, y0) = (2,3) of the function f (x) = 4x ^...
Starting from the point (x0, y0) = (2,3) of the function f (x) = 4x ^ 2 - 4xy + 2y ^ 2, it is to choose to proceed according to the Steep Descent Method (x2, y2).
For the given function f(x) = cos(x), let x0 = 0, x1 = 0.25, and x2...
For the given function f(x) = cos(x), let x0 = 0, x1 = 0.25, and x2 = 0.5. Construct interpolation polynomials of degree at most one and at most two to approximate f(0.15)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT