Question

Please help me fix this halleyroot funtion , in matlab

function [x, er, n] = Halleyroot(fun,x1,maxtol,maxitr)
if (nargin < 5), maxitr = 25; end
if (nargin < 4), maxtol = 1e-4; end
k = 0;
er = 1;
x = x1;
funder= derivative(fun);
while er >= maxtol && k < maxitr
k = k+1; % increment the iteration number
xold = x;
x = xold-fun(xold)/funder(xold);
er = abs((x-xold)/x);
fprintf('iter = %i\t x = %f\t f(x) = %f\t er = %f \n',k,x,fun(x),er);
end
n = k; % the number of iterations performed
end

.-------------------------------------------------------------- mistake was pointed out down below

er 1 x xl funder derivative (fun) while er >= maxtol && k < maxitr k k+1; %increment the iteration number xold x x = xold-fun

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

MATLAB Function

function Halleyroot(f,x1,maxtol,error)
i = 1;
dx = diff(f); %first and second derivatives of f(x)
ddx = diff(dx);
while i <= maxtol
p = x1 - (f(x1)/dx(x1))*(1 - (f(x1)*ddx(x1)/dx(x1)^2))^(-1); %Implementation og Halleys Methof (i.e. g(x)).
  
if (abs(p - x1)/abs(p)) < error %stopping criterion when difference between iterations is below tolerance
fprintf('Solution is %f \n', double(p))
return
end

i = i + 1;
x1 = p; %update p0
end

fprintf('Solution did not coverge within %d iterations at a required precision of %d \n', maxtol, error) %error for non-convergence within N iterations

end

MATLAB script to call function

clc
clear
close
syms x
f(x)= x^2 - 6;
x1 = 1;
maxtol = 100; %maximum number of iterations
error = 0.00000001;
Halleyroot(f,x1,maxtol,error)

Result

Solution is 2.449490

Add a comment
Know the answer?
Add Answer to:
Please help me fix this halleyroot funtion , in matlab function [x, er, n] = Halleyroot(fun,x1,maxtol,maxitr)...
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
  • Please do this in Matlab. Not sure if you need this code: e cofunction [x, er,...

    Please do this in Matlab. Not sure if you need this code: e cofunction [x, er, n] = FixedPoint(g, x1, maxtol, maxitr) if nargin < 4, maxitr = 25; end if nargin < 3, maxtol = 1e-3; end k = 0 ;    er = 1; x = x1;    while er >= maxtol && k < maxitr k=k+1; xold = x; x=g(x); er=abs((x-xold)/x); fprintf('iter = %i, x = %e, er = %e ', k,x,er); end n=k; if n ==...

  • I'm working on the newton's method on matlab, could someone help me and show what two...

    I'm working on the newton's method on matlab, could someone help me and show what two lines are needed to be added in the codes in order to make this function work? function sample_newton(f, xc, tol, max_iter) % sample_newton(f, xc, tol, max_iter) % finds a root of the given function f over the interval [a, b] using Newton-Raphson method % IN: % f - the target function to find a root of % function handle with two outputs, f(x), f'(x)...

  • 5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f,...

    5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f, f1,x0,n) Xx0; for ilin x = x - f(x)/f1(x); disp (li if f(x) <0.01 f(x))) break end end end Matlab Code from Chapra function [root, ea, iter)=newtraph (func,dfunc, xr, es,maxit,varargin) newtraph: Newton-Raphson root location zeroes 8 [root, ea, iter)-newtraph (func, dfunc, xr, es,maxit,pl,p2, ...): $uses Newton-Raphson method to find the root of fune input: func- name of function 8dfunc = name of derivative of...

  • 45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using...

    45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using Newton-Rephson Method without showing any iteration. Also find the root of equation, f(x) = x 9-3x -10, take initial guess, Xo=2 العقدة College of 9:05 mybb.qu.edu.ca Numerical Methods (Lab.) GENG 300 Summer 2020 5.1.2 Open Methods - Newton-Raphson Method f(x) *1+1 = x; - Matlab Code Example:4 function mynewtraph.t1.x0,-) XXO for ilin x - x - x)/1 x) disp 1 x) <0.01 break end...

  • This is Matlab Problem and I'll attach problem1 and its answer for reference. We were unable...

    This is Matlab Problem and I'll attach problem1 and its answer for reference. We were unable to transcribe this imageNewton's Method We have already seen the bisection method, which is an iterative root-finding method. The Newton Rhapson method (Newton's method) is another iterative root-finding method. The method is geometrically motivated and uses the derivative to find roots. It has the advantage that it is very fast (generally faster than bisection) and works on problems with double (repeated) roots, where the...

  • Newton's Method in MATLAB During this module, we are going to use Newton's method to compute...

    Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x° + 3x² – 2x – 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its...

  • MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts...

    MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts in order to get full credit. Help me out, thank you f LAB SECTION NAME HW6P3 (30 points) following are infinite series representations of the function, un pra i a script file HW6P3 that determines the value of the function from the sum of the series for a given value of x. The program (1 pt) must prompt the user to enter a value...

  • in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge wh...

    in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge when using Function 4.3.1? Is the root obtained the one that is closest to that guess? )xo = 1.5, (b) x0 = 2, (c) x.-3.2, (d) xo = 4, (e) xo = 5, (f) xo = 27. Function 4.3.1 (newton) Newton's method...

  • Matlab Regula Falsi Method A zero of f(x) = x^2 -4x-12.2 is known to exist on...

    Matlab Regula Falsi Method A zero of f(x) = x^2 -4x-12.2 is known to exist on the interval [1.2 , 2.2 , 3.2,...9.2] and respective right endpoints x1 =[2.2 ,3.2, 4.2....10.2], find the sub-interval in which the zero exists. Set the left endpoint of this sub-interval =a and the right endpoint=b With tolerance of 10^-9, use the Regular Falsi method to compute the root of (f) in [a,b] User input is required at ##### CONTENTS Close Courses LMS Integration Documentation...

  • help me with this. Im done with task 1 and on the way to do task...

    help me with this. Im done with task 1 and on the way to do task 2. but I don't know how to do it. I attach 2 file function of rksys and ode45 ( the first is rksys and second is ode 45) . thank for your help Consider the spring-mass damper that can be used to model many dynamic systems -- ----- ------- m Applying Newton's Second Law to a free-body diagram of the mass m yields the...

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