find the root(s) of the following functions using both Newton's method and the secant method, using tol = eps.
function [root] = newtonRaphMethod(f,fd,x0,tol,maxits)
figure
root_not_found = true;
iter = 1;
xn = x0;
while iter<maxits
% X = x - f/f'
%Xn = xn - (f(xn)/fd(xn));
% simplified to Babylonian method
Xn = (xn + 2 / xn) / 2;
%termination criterion when |X-x|<tol
abs_error = abs(Xn-xn);
if abs_error <tol
root = Xn;
root_not_found = false;
break;
end
%plotting absolute error vs iterations
errors(iter) = abs_error;
iters = 1:iter;
semilogy(iters,errors,'-*')
xlabel('iteration')
ylabel('absolute error')
title(['Newton''s (p0 = ' num2str(x0) ')'])
grid on
xn = Xn;
iter = iter+1;
end
if root_not_found
fprintf(['\n Root not found. Method failed at ' num2str(iter) '
iterations \n'])
root =[];
end
end
------------------------------------------
function root = secantMethod(f,x0,x1,tol,maxits)
figure
x(1) = x0;
x(2) = x1;
n = 2;
iter = 1;
root_not_found = true;
while iter<maxits;
n = n+1;
x(n) = x(n-1) - ( ( f(x(n-1) )*( x(n-1) - x(n-2) ))/( f(x(n-1)) -
f(x(n-2)) ) );
%termination criterion |x(n)-x(n-1)|<tol
abs_error = abs(x(n)-x(n-1));
if abs_error<tol
root = x(n-1);
root_not_found = false;
break;
end
%plotting absolute error vs iterations
errors(iter) = abs_error;
iters = 1:iter;
semilogy(iters,errors,'-*')
xlabel('iteration')
ylabel('absolute error')
title(['Secant (p0 = ' num2str(x0) ', p1 = ' num2str(x1)
')'])
grid on
iter = iter+1;
end
if root_not_found
fprintf(['\n Root not found. Method failed at ' num2str(iter) '
iterations \n'])
root = [];
end
end
--------------------------------------------------------------------------
%command
clear
clc
close all
% let f(x) = x^2-4
f = @(x) x.^2 - 2
% plotting the function for visual
x_min = -5;
x_max = 5;
spacing = 100; % 1/100
x = linspace(x_min,x_max,spacing);
plot(x,f(x))
title('f(x) = x^2 - 2')
xlabel('x')
ylabel('f(x)')
grid on
%setting termination criteria
maxits = 100;
tol = 1e-6;
%definig derivative fp and x_init initial guess for newton
method
fp = @(x) 2*x;
x_init = -2.5;
%calling newton function to compute root
root_newton = newtonRaphMethod(f,fp,x_init,tol,maxits)
%{
% X = x - f/f'
find the root(s) of the following functions using both Newton's method and the secant method, using...
You are given the following function () 6a +11 6.1 You are to find the roots of this function using the secant method with z 2.5-and zi = 3.5 1.(6 points) Develop a function m-file that calculates the root of the above function using the secant method. The function should have the following five inputs only. 1. (2 points) The equation or function whose roots need to be found 2.(2 points) Initial guess -1 3. (2 points) Initial guess A4.(2...
2. (a) Suppose we have to find the root xof x); that is, we have to solve )0. Fixed-point methods do this by re-writing the equation in the form x·= g(x*) , and then using the iteration scheme : g(x) Show this converges (x-→x. as n→o) provided that K < 1 , for all x in some interval x"-a < x < x*+a ( a > 0 ) about the rootx 6 points] (b) Newton's method has the form of...