Question

Write a MATLAB function newtonRaphson(fx, x0, sigfig, maxIT) that will return a root of the funct...

Write a MATLAB function newtonRaphson(fx, x0, sigfig, maxIT) that will return a root of the function f(x) near x=x0 using the Newton-Raphson method, where sigfig is the accuracy of the solution in terms of significant figures, and maxIT is the maximum number of iterations allowed. In addition to the root (xr), the function newtonRaphson is expected to return the number of iterations and an error code (errorCode). As such, the first line of the function m file newtonRaphson.m must read as: function [xr, nIT, errorCode] = newtonRaphson(fx, x0, sigfig, maxIT)

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

%the function script is

function [xr nIT errorCode]=newtonRaphson(f,x0,sigfig,maxit)
syms x
dfn =diff(f,x);
df=inline(dfn,'x');
for i=1:maxit
    x(i)=x0-f(x0)/df(x0);
    tol1(i)=abs(x(i)-x0);
    x0=x(i);
    
   nIT=i;
   xr=double(x(i));
     errorCode=double(tol1(i));
   if tol1(i)<sigfig
       break;
   end
end

%the input script for calling function is

clc;clear all;
f = @(x) x^2-4*x-7;
x0=5;
format short
sigfig=0.0001;
maxit=100;
[xr nIT errorCode]=newtonRaphson(f,x0,sigfig,maxit)

%output

DesktoP Command Window xr = 5.3166 errorCode 4.1876e-05 fx >

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB function newtonRaphson(fx, x0, sigfig, maxIT) that will return a root of the funct...
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
  • 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 function is a modified versio of the newtmult function obtained % from % “Ap...

    ____________ % This function is a modified versio of the newtmult function obtained % from % “Applied Numerical Methods with MATLAB, Chapra, % 3rd edition, 2012, McGraw-Hill.” function [x,f,ea,iter]=newtmult(func,x0,es,maxit,varargin) % newtmult: Newton-Raphson root zeroes nonlinear systems % [x,f,ea,iter]=newtmult(f,J,x0,es,maxit,p1,p2,...): % uses the Newton-Raphson method to find the roots of % a system of nonlinear equations % input: % f = the passed function % J = the passed jacobian % x0 = initial guess % es = desired percent relative error...

  • Use the following pseudocode for the Newton-Raphson method to write MATLAB code to approximate the cube...

    Use the following pseudocode for the Newton-Raphson method to write MATLAB code to approximate the cube root (a)1/3 of a given number a with accuracy roughly within 10-8 using x0 = a/2. Use at most 100 iterations. Explain steps by commenting on them. Use f(x) = x3 − a. Choose a = 2 + w, where w = 3 Algorithm : Newton-Raphson Iteration Input: f(x)=x3−a, x0 =a/2, tolerance 10-8, maximum number of iterations100 Output: an approximation (a)1/3 within 10-8 or...

  • Write a MATLAB function (IncNR) combining the capabilities of both the Incremental Search Method and the...

    Write a MATLAB function (IncNR) combining the capabilities of both the Incremental Search Method and the Newton-Raphson Method to find all the roots in a given function [xR, err, n] = IncNR(AF, xb, ed) AF = anonymous function xb = initial guess x bracket = [xL xU], where xL = lower x and xU = upper x ed = desired error Outputs: xR = vector of roots err = vector of errors corresponding to the roots n = vector of...

  • . (25 points) The recurrence relation for the Newton's Raphson method is a)0.1.2 f(r.) F(z.) The ...

    . (25 points) The recurrence relation for the Newton's Raphson method is a)0.1.2 f(r.) F(z.) The derivative of the function can be approximately evaluated using finite-difference method. Consider the Forward and Centered finite-difference formulas Forward Finite-Difference Centered Finite-Difference 2h It is worthwhile to mention that modified secant method was derived based on the forward finite- difference formula. Develop a MATLAB functions that has the following syntax function [root,fx,ea,iter]-modnetraph (func,x0,h,es,maxit,sethod, varargin) % modnevtraph: root location zeroes of nonlinear equation f (x)...

  • in matlab Using the polynomial entered and the starting guess , compute the root of the...

    in matlab Using the polynomial entered and the starting guess , compute the root of the polynomial using the Newton-Raphson method. Repeat the method until the percent error between the most recent two iterations is less than the percent error entered by the user . Determine the percent error of the most recent two iterations using the formula. Output the final value of the root found with the Newton-Raphson method and the number of iterations the method took to converge...

  • Write a Matlab function for: 1. Root Finding: Calculate the root of the equation f(x)=x^3 −5x^2...

    Write a Matlab function for: 1. Root Finding: Calculate the root of the equation f(x)=x^3 −5x^2 +3x−7 Calculate the accuracy of the solution to 1 × 10−10. Find the number of iterations required to achieve this accuracy. Compute the root of the equation with the bisection method. Your program should output the following lines: • Bisection Method: Method converged to root X after Y iterations with a relative error of Z.

  • Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

    Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root ^n and the number of steps n taken to reach the solution. Use function mynewton.m to perform...

  • Write a MATLAB code employing Secant method and for loop to calculate the root for the following function: ?=?6−?−1 Use 7 iterations with initial guesses x0 = 2 and x1 = 1.

    Write a MATLAB code employing Secant method and for loop to calculate the root for the following function: f=x6-x-1Use 7 iterations with initial guesses x0 = 2 and x1 = 1

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