Question

Matlab problem using newton raphson to find square root of number

Need help modifying my Matlab script below (myscript calculates the square root of a number. using a Newton-Raphson method with 1 as the initial guess, calculates true and estimated error, and shows each iteration).

-I need to create three new functions each of which should be called in the main script. These functions are needed to replace code that is currently in my script shown below.

-I need to create these functions:

A function to find f(x)

A function to find f '(x) ?

A function to determine the next guess, xn+1

-Need to plot estimated error vs iteration number, using both a marker and a line.

On the same plot, show actual error versus iteration, using different colors, markers, and line types.


Here are the functions I created:

%function to find f(x)

function x = func_eval(x_o,z)
x = (x_o)^2-z;

return


%function to find f'(x)
function x = deriv_eval(x_o)
x = 2*x_o;
return

 %next_val.m

 function x_new = next_val(x)
 if x == 0
     x_new = 1;
     return;


Here is my code without these functions implemented:

%Matlab script to calculate the square root of a number.

clear all;

close all;

clc;

z = input('Input a number to find square root:');

disp('Exact value is:')


x_o=1; %Initial guess of 1

disp(sqrt(z))


%Improved initial guess code

%  if z>=1

%      x_o = 1;

%      while (x_o)^2 < z

%       

%         x_o = x_o*10;

%      end 

%  x_o = x_o/2; 

%  else

%      x_o = 1;

%      while x_o^2> z

%          x_o = x_o/10;

%      end

%      x_o = x_o*2;

%  end

%  

         

 

tolerance_lim = 0.0000000001; % tolerance limit is 10 significant figures

i = 0; % iteration number

disp ('Iteration Number: ')

disp(i)


func_eval  = (x_o)^2-z;

deriv_eval = 2*x_o;

y = func_eval/deriv_eval;

x_new = x_o - y;

est_error = (x_new - x_o);

true_error =  sqrt(z)-x_o;


disp('Initial guess is:')

disp(x_o)


disp('Estimated error:')

disp(est_error)

disp('Exact error:')

disp(true_error)



func_eval  = (x_o)^2-z;

deriv_eval = 2*x_o;

y = func_eval/deriv_eval;

x_new = x_o - y;

est_error = (x_new - x_o);



while abs(est_error/x_o) >= tolerance_lim

    i = i+1;

 disp('Iteration Number:')

 disp(i)

 

x_o = x_new;

func_eval= (x_o)^2-z;

deriv_eval = x_o*2;

y = func_eval/deriv_eval;

x_new = x_o - y;

 

disp('Current guess is:')

disp( x_new)


est_error = x_new - x_o;

true_error =  sqrt(z)-x_new;


disp('Estimated error:')

disp(est_error)

disp('Exact error:')

disp(true_error)


end

disp('Converged!')


0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Matlab problem using newton raphson to find square root of number
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • B. Implement the Newton-Raphson (NR) method for solving nonlinear equations in one dimension. The...

    B. Implement the Newton-Raphson (NR) method for solving nonlinear equations in one dimension. The program should be started from a script M-file. Prompt the user to enter an initial guess for the root. -Use an error tolerance of 107, -Allow at most 1000 iterations. .The code should be fully commented and clear 2. a) Use your NR code to find the positive root of the equation given below using the following points as initial guesses: xo = 4, 0 and-1...

  • 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...

  • (la) Determine the root of the x – ez* + 5 = 0 using the Newton-Raphson...

    (la) Determine the root of the x – ez* + 5 = 0 using the Newton-Raphson method with equation initial guess of xo = 1. Perform the computation until the percentage error is less than 0.03%. (1b) Employ bisection method to determine the root of the f(x)=x* – 3x + 7 =0) using equation two initial guesses of x; =-2.1 and x;, =-1.8 . Perform three iterations and calculate the approximate relative error for the third iteration. What is the...

  • Xs 2x2 Use the MAT AB code for Newton-Raphson method to find a root of he function table. x 6x 4 ...

    xs 2x2 Use the MAT AB code for Newton-Raphson method to find a root of he function table. x 6x 4 0 with he nitial gues& xo 3.0. Perfonn the computations until relative error is less than 2%. You are required to fill the followi Iteration! 뵈 | f(x) | f(x) | Em(%) 1. Continue the computation of the previous question until percentage approximate relative error is less 2. Repeat computation uing theial guess o1.0 xs 2x2 Use the MAT...

  • MATLAB Question: TASK 5 12 MARKS -L06N] The rounded-square-root (RSR) of a positive integer n is...

    MATLAB Question: TASK 5 12 MARKS -L06N] The rounded-square-root (RSR) of a positive integer n is defined as the square root of n rounded to the nearest integer. Adapting Heron's method to integer arithmetic allows the rounded-square-root of n to be calculated as follows. Let d be the number of digits of number n d-1 If d is odd, then Xo = 2 × 107- If d is even, then Xo-7 × 107- where xo is the starting guess for...

  • clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...

    clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold = 0.5; % this is the initial guess test = 1; % this simply ensures we have a test value to enter the loop below. %If we don't preallocate this, MATLAB will error when it trys to start the %while loop below k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it %to allow the while loop to...

  • Write three functions that compute the square root of an argument using three different methods. The...

    Write three functions that compute the square root of an argument using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number is the values such that x . For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that is in the range 0.0 to 100.0 You program should have a main() that asks the user for x and an accuracy...

  • 6.5 Employ the Newton-Raphson method to determine a real root for 4x20.5 using initial guesses of...

    6.5 Employ the Newton-Raphson method to determine a real root for 4x20.5 using initial guesses of (a) 4.52 f(x) 15.5x Pick the best numerical technique, justify your choice and then use that technique to determine the root. Note that it is known that for positive initial guesses, all techniques except fixed-point iteration will eventually converge. Perform iterations until the approximate relative error falls below 2 %. If you use a bracket- ing method, use initial guesses of x 0 and...

  • 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...

  • Write a function named “NewtonRaphson” that implements the Newton-Raphson method. The inputs to this function are...

    Write a function named “NewtonRaphson” that implements the Newton-Raphson method. The inputs to this function are the name of the function, name of the function’s derivative function, initial guess, maximum number of iterations, and tolerance for the relative convergence error. The output is the root. Use the problem in Homework #3 to test your function.    Hw 3 that we are pulling from %Newton-Raphson method to find upward velocity of a rocket clear all; clc; u=2200; %m/s m0=160000; %kg q=2680;...

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