Question
Write a function m-file that implements the secant method. Your function should accept the following inputs (in order): A function defining the roots problem A
Write a function m-file that implements the secant method. Your function should accept the following inputs (in order):
- A function defining the roots problem
- A vector of two initial guesses (first element is the first guess, second element is second guess)
- A stopping criterion ( es ) for the numerical solution with a default value of 1e-5
- A maximum iteration count for the numerical solution with a default value of 30
- An arbitrary number of parameter values associated with the roots problem
Your function should output the following four scalar outputs (in order):
- The root estimate
- The residual in the numerical solution
- The approximate relative error in the numerical solution
- The number of iterations required for convergence
I recommend you use the examples newtonraphson.m and/or bisect_param.m as starting points from which you develop your own function mfile for the secant method.
Note: Test cases 3, 4, and 5 test the functionality of the default values for unspecified inputs and the parameter passing through varargin.
****here is my code so far...Please help
function [xroot,residual,ea,iter_count] = student_solution(fun,xi,es,max_it,varargin)
if nargin < 2, error('Two few inputs, read help comments'), end
if nargin<3||isempty(es), es=1e-5;end
if nargin<4||isempty(max_it), max_it=30;end
xroot = xi; %initial guesses
for iter = 1:max_it
xr_old = xroot;
xr_older = xr_old;
xroot = xr_old - (fun(xr_old,varargin{:}).*(xr_older-xr_old))./(fun(xr_older,varargin{:}-fun(xr_old,varargin{:})));
if xroot ~= 0, ea = (xroot-xr_old)./xroot; end
if abs(ea) <= es, break, end
iter_count= max_it;
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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