Question

in C++. Write a function squareRoot that uses the Newton’s method of approximate calcu-lation of the...

in C++. Write a function squareRoot that uses the Newton’s method of approximate calcu-lation of the square root of a number x. The Newton’s method guesses the square root in iterations. The first guess is x/2. In each iteration the guess is improved using ((guess + x/guess) / 2 ) as the next guess. Your main program should prompt the user for the value to find the square root of (x) and how close the final guess should be to the previous guess (for example, 0.001), and pass these values to the squareRoot function. As a third argument of the function squareRoot pass an error processing function, to be called if the other arguments do not pass validation. For example the error processing function can set the wrong arguments to some default values, or ask the user for new values, or do some other appropriate action. Test squareRoot with at least two error processing functions.

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

`Hey,

Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.

#include <iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
bool error1(double x)
{
if(x<=0)
return false;
return true;
}
bool error2(double x)
{
if(x<=0||x>1)
return false;
return true;
}
double square_root(double x,double err)
{
double x1,x0=x/2.0;
while(1)
{
x1=(x0+x/x0)/2.0;
if(fabs(x1-x0)<err)
break;
x0=x1;
}
return x1;
}
int main()
{
cout<<"Enter x to find square root: ";
double x;
cin>>x;
while(!error1(x))
{
cout<<"Value less than 0 error\n";
cout<<"Enter x to find square root: ";
cin>>x;
}
cout<<"Enter error: ";
double err;
cin>>err;
  
while(!error2(err))
{
cout<<"accuracy value incorrect error\n";
cout<<"Enter error: ";
cin>>err;
}
cout<<"Square root is "<<square_root(x,err)<<endl;
return 0;
}

main.cpp 1 #include <iostream> 2 #include<cstdlib> 3 #include<cmath> 4 using namespace std; 5 bool error1(double x) 6- { 7 if

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
in C++. Write a function squareRoot that uses the Newton’s method of approximate calcu-lation of the...
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
  • 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...

  • A. Implement the False-Position (FP) method for solving nonlinear equations in one dimension. The...

    A. Implement the False-Position (FP) method for solving nonlinear equations in one dimension. The program should be started from a script M-file. -Prompt the user to enter lower and upper guesses for the root. .Use an error tolerance of 107. Allow at most 1000 iterations The code should be fully commented and clear " 1. Use your FP and NR codes and appropriate initial guesses to find the root of the following equation between 0 and 5. Plot the root...

  • II. Using Newton’s method, write a MATLAB program to find the fixed point of the following...

    II. Using Newton’s method, write a MATLAB program to find the fixed point of the following function: ?(?) = √? + ?? accurate to at least 8 decimal places. (HINT: finding the fixed point of f(x) is the same as finding the zero of g(x) = f(x) − x. ) The output of this program should display in a single table (i) the solution for the fixed point, (ii) the initial guess, (iii) the number of iterations it took to...

  • Please code in MatLab or Octave Output should match Sample Output in Second Picture Thank you...

    Please code in MatLab or Octave Output should match Sample Output in Second Picture Thank you 5. In this problem we will investigate using the Secant Method to approximate a root of a function f(r). The Secant Method is an iterative approach that begins with initial guesses , and r2. Then, for n > 3, the Secant Method generates approximations of a root of f(z) as In-1-In-2 n=En-1-f (x,-1) f(Fn-1)-f(-2) any iteration, the absolute error in the approximation can be...

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

  • Task 2 (25 points + 4 points for commenting): Write computer code to perform the Fixed.Point...

    Task 2 (25 points + 4 points for commenting): Write computer code to perform the Fixed.Point method and use your code to find the root of the following equation using an initial guess of 3 and a stopping criterion of 0.001%; f(x) e -4x For Fixed-Point, you do not have to code Matlab to take the derivatives of the function and check the g'(x). You can do that step by hand, and then show me your hand cal ulations 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...

  • 4.10 Team Activity 4 Create a program that will approximate the roots of an equation (i.e....

    4.10 Team Activity 4 Create a program that will approximate the roots of an equation (i.e. solution to the equation f(x) = 0) using Newton's Method. Newton's Method is an iterative process that approximates the solution using the equation: alt text For this activity, you will approximate the solution for f(x) = x^2. (The derivative of this function is f'(x) = 2x). Your program should accept two user inputs: the number of iterations to perform, i (integer) the starting guess...

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

  • Part B (Based off Week 3 Content) Newtons Method approximates a root of a function by iterating t...

    must be done in matlab Part B (Based off Week 3 Content) Newtons Method approximates a root of a function by iterating through the equation where n is the nth estimate for the root of the function f(z). In order to it- erate through this method, we need to provide an initial guess for the root, For example, if we apply this method to f(z) = sin(z) using note that f(cos(r) = 1, we sin(1) =-0.5574 cos(1) sin(-0.5574 ) cos(-0.5574)...

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