Question

find the root(s) of the following functions using both Newton's method and the secant method, using tol = eps.

3 Find the root s of the following functions using both Newtons ulethod and the anat inethod using tol epa. . You will vood

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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'

root newton -1.4142 root ecant -1.4142 (x) tanh (x) root newton 1.4142 root secantf(x) = x2-2 20 15 10 -5 -5 -3-2 0 13 45Newtons (po =-2.5) 10 10 O 40-2 辷10. CD 10 10 10-5 1.5 2 2.5 iteration 3 3.5 4Secant 10 10 O 40-2 .3 D 10 10-4 10-5 1 1.5 22.5 33.5 4 4.5 5 iteration5 4 3 2 1864 202468 3Newtons (p0= 1.09) 10 -1 10 5 10 -3 10 104 1 1.2 14 1.6 18 22.224 2.6 2.8 3 iterationSecant (po =-1 , p1 = 1) 10 米 10.1 L 0 0.2 040.6 0.8 1 12 1416 18 2 iteration

Add a comment
Know the answer?
Add Answer to:
find the root(s) of the following functions using both Newton's method and the secant method, using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT