Question
Please solve in Matlab Problem 1: The Secant Method In MATLAB, roots of functions are determined numerically (i.e., rather than symbolically) using numeric approximation algorithms.
Please solve in Matlab
Problem 1: The Secant Method
In MATLAB, roots of functions are determined numerically (i.e., rather than symbolically) using numeric approximation algorithms. The Successive Numeric Approximation module describes Newton's Method for finding roots and you got experience implementing Newton's Method in Team Lab 9. In this problem, you will implement a different numeric approximation algorithm for finding roots, the Secant Method. For the purposes of this problem, the function whose roots you will be finding is
This function is defined for you already (as a function handle named fctn) inside the provided homework4.m script.
((% function we wish to find the roots of
fctn = @(x)x.^3 + 4*x.^2 - 6*x - 0.3*exp(x);))
Add code to your homework4 script to plot f(x) from x = -6 to x = 8. Use the grid ON command and use the axis command to scale the plot so the y-axis ranges from -20 to 40.
A function which implements Newton's Method for finding roots has been implemented for you.
function [root, iters] = newton(func, f_prime, x0)
% newton
%
% Uses Newton's method to find a root of func(x) near x0.
%
% Newton's method is used to find a root for func(x) starting with the
% initial guess, x0. Successive approximations are done until either
% func(x) is within epsilon of 0 (where epsilon = 10^-15)
% or
% 100 iterations have been done (i.e., 100 new guesses have been
% computed)
%
% Input:
% func a function handle for function whose root is being determined
% f_prime a function handle for function representing the first
% derivative of func(x)
% x0 the initial guess for the location of the root
%
% Output: [root, iters] where
% root the final estimate for the root
% iters the number of iterations (until either func(x) is within
% epsilon of 0 or until 100 iterations have been done)
%
epsilon = 1e-15;
iters = 0;
root = x0;
while abs(func(root)) > epsilon && iters
root = root - func(root)/f_prime(root);
iters = iters + 1;
end
Recall that Newton's Method uses the first derivative of the function in its computation. In order for the newton function to work, it requires that 3 arguments be given to it: a function handle for the function for which we wish to find the root, a function handle for the function that computes the derivative of the function, and an initial guess for the location of the root.
Add code to your homework4 script to define a function handle fctnPrime representing the derivative of f(x).
The Secant Method takes two initial guesses (x0 and x1), which must be distinct. It is very similar to Newton's method except that instead of using the derivative to find the slope of the tangent line at a point on the function, it estimates the slope of the tangent line by using the slope of the line between two nearby points on the function (i.e., the secant line).
The formula for determining the next guess (based on the two previous guesses) is:
Starting with the initial guesses, x0 and x1, a new guess, x2, is then obtained by finding where the line between the function values at the initial guesses intersects the x-axis; x1 and x2 are then used to compute a new guess, x3, and the process repeats until the desired precision is reached (or two guesses in succession are identical).
Define a MATLAB function secant which takes 3 arguments:
func - a function handle for the function whose root is being determined
x0 - one initial guess, x0
x1 - the second initial guess, x1
and returns a vector containing 2 values:
root - the value for the root obtained by implementing the Secant Method
iters - the number of iterations it took for the Secant Method to find the root (where the func(root) is within ? of 0 or two successive guesses are identical) or 100 (the maximum number of iterations the method will perform)
The first line of the secant function should be:
function [root, iters] = secant(func, x0, x1)
The secant function must implement the following algorithm:
if x0 and x1are the same value, quit with the error message "x0 and x1 must be distinct" (hint: use the error function)
initially, curr is x1 and prev is x0 while |func(curr)| > ? and # iterations curr is not the same as prev compute a new guess using formula above update curr and prev
Use ? = 10-15 and don't forget to keep track of the number of iterations. Tip: look at the newton function and use it as a guide for the overall structure of step 2.
f (x) = x3 + 4x2-62-0.3ezStep 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