Question
I know myBisection matlab code down below works, but how can I change it to make it mySecant and get the same outputs? the initial
I know myBisection matlab code down below works, but how can I change it to make it mySecant and get the same outputs? the initial estimates should be xm1 and x0 and If the requested tolerance in function cannot be reached within N = 80 iterations, the function shall display an error message to the screen and return a value of x = realmax()
function [ x,toleranceSolution, toleranceFunction,ere,n ] = myBisect(f,a,b,epsok) %find root in interval [a,b] using the bisection method %Input: %f: anonymous functuon to find the root f(x) = 0 %a,b: lower and upper bound of the initial interval [a,b] %epsok: requested tolerance in solution for the root x %output %x: root (solution) %tolerance solution of x %tolerance function of x %ere: estimated relative error of x %n: number of iterations performed to find x
%check if the given initial interval [a,b] contains a root fa = f(a); fb = f(b); if fa*fb>0 fprintf('f(a) and f(b) have the same sign. Error! '); x = realmax(); return end
%calculate the number of required iterations to reach a tolerance in soln of epsok N = ceil(log((b-a)/(2*epsok))/log(2));
%calc the initial guess and its function value x = (b+a)/2; fx = f(x);
%iteration to find the solution for n = 1:N %find the new solution interval if fa*fx 0 a = x; fa = fx; else break end %Store prior numerical solution to calculate ere xold = x;
%update the soln x = (b+a)/2; fx = f(x); end
%calc all remaainningoutput variables toleranceSolution = (b-a)/2; toleranceFunction = abs(fx); ere = abs((x-xold)/x);
end
Develop a Matlab function that finds a root of a function f(x) starting from the given initial estimates x(-1) and x(0) with a tolerance in function of at least cok using the secant method. Name the function mySecant using as input the anonymous function f, the initial estimates xm1 and x0, and the required tolerance in function epsok. As output, the function shall return four scalar variables: the numerical solution x, its tolerance in function tolFunc, its estimated relative error ere, and the number of iterations performed n. If the requested tolerance in function cannot be reached within N 80 iterations, the function shall display an error message to the screen and return a value of realmax (). Other than this potential error message, do not print out any results to screen or do any plotting within the function. Minimize as much as possible the number of function calls required per iteration, by storing function call results in temporary storage variables. You should require only 1 function call to f to update the solution per iteration. = XStep 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