Question

In: Computer Science

IN C++ Using newton's method, Please create a SqRoot function. DO NOT USE sqrt ( )...

IN C++

Using newton's method, Please create a SqRoot function.

DO NOT USE sqrt ( ) , If you do I will give you zero points period.  

So create a function that takes a double and returns a double that is the square root of the number.

You must decide what number to start the calculations with,

You must decide how and when to stop computing.

For this exercise you must tell me ( use comments in your code: /* */, etc. )

There is not 'correct' answer but if you do not write and address these questions you will get a very low score.

A) how you chose the initial estimate and why , or if it is important

B) How you decided on how to end the cycles of calculation and why

C) Tell me about some of your test cases and what you learned from them.

Here is the formula is psudo C++ Code:

// A = approximation of the Square Root

// Num = number that we want to find the square root of.

// A_prime is the updated value of a after the formula

A_prime = ( ( N / A ) + A ) / 2.0;

Solutions

Expert Solution

#include <iostream>
using namespace std;

// function declaration
// takes a double as parameter and
// return a double as square root of the given number
double SqRoot(double num);

int main() {
   cout << "Square root of 4.0 is: " << SqRoot(4.0) << endl;
   cout << "Square root of 1.0 is: " << SqRoot(1.0) << endl;
   cout << "Square root of 9.0 is: " << SqRoot(9.0) << endl;
   cout << "Square root of 25.25 is: " << SqRoot(25.25) << endl;
   cout << "Square root of 100.0 is: " << SqRoot(100.0) << endl;
   cout << "Square root of 8.0 is: " << SqRoot(8.0) << endl;
   cout << "Square root of 3600.0 is: " << SqRoot(3600.0) << endl;
   cout << "Square root of 0.5 is: " << SqRoot(0.5) << endl;
   cout << "Square root of 0.0 is: " << SqRoot(0.0) << endl;

   return 0;
}

double SqRoot(double num) {

   /*
   * chose the initial estimate of Square root as 1
   * becouse square root of 1 is 1. its an identity.
   * in this method of finding square root we are adding
   * up the estimation and dividing it by 2 so chossing minimum
   * posible value as initial is good choice
   */

   // take approximation of the Square Root
   double approx = 1.0;
   // use the formula
   // A_prime = ( ( N / A ) + A ) / 2.0;
   // A_prime is the updated value of a after the formula
   // we will use the formula for calculation till we find the square root

   /*
   * we end the cycles of calculation when the square of aproximation value is equal
   * to the given number,thus by defination aproximation value is the
   * square root of the given number
   * in the loop we update approximate value based on return value of formula.
   * using this method it is easy to find square root of perfect square number
   * but its hard to find square root of imperfect number as error in floating point value
   * repesented by computer(overflow). because of this in accuracy we stop loop as we find nearest
   * value to the square root by 0.01% accuracy.
   * this allow us to get any positive number square root by 0.01% accuracy
   */
  
   while (approx*approx <= num*0.9999 || approx*approx >= num*1.0001) {
       // calculate A_prime
       approx = ((num / approx) + approx) / 2.0;
   }

   /*
   * using negative number as test case result in infinite loop
   * its not possible to find square root of neagtive number by this method.
   * its not possible to find square root of zero as its returns not a number
   * as result of division by 0 when aprox reaches its value to 0 in the loop
   */

   return approx;
}

i have added comments in the code. let me know if you have any doubts or problem running the program or want any modification in program.


Related Solutions

PLEASE USE PYTHON CODE 7. Use Newton's method to find the polynomial that fits the following...
PLEASE USE PYTHON CODE 7. Use Newton's method to find the polynomial that fits the following points: x = -3, 2, -1, 3, 1 y = 0, 5, -4, 12, 0
Use Newton's Method to approximate the zero(s) of the function. Continue the iterations until two successive...
Use Newton's Method to approximate the zero(s) of the function. Continue the iterations until two successive approximations differ by less than 0.001. Then find the zero(s) to three decimal places using a graphing utility and compare the results. f(x) = x3 − 6.9x2 + 10.79x − 4.851 Newton's method:      Graphing Utility:      x = x = (smallest value) x = x = x = x = (largest value)
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
Use Newton's method to find the absolute maximum value of the function f(x) = 3x cos(x),...
Use Newton's method to find the absolute maximum value of the function f(x) = 3x cos(x), 0 ≤ x ≤ π; correct to six decimal places.
Please C++ create a program that will do one of two functions using a menu, like...
Please C++ create a program that will do one of two functions using a menu, like so: 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 1 Enter Catalan number to calculate: 3 Catalan number at 3 is 5 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 2 Enter Fibonacci number to calculate: 6 Fibonacci number 6 is 8 Create a function of catalan that will take a parameter and return...
Write a program using Newton's method: Use your programs to find approximations to within 10^(-4) to...
Write a program using Newton's method: Use your programs to find approximations to within 10^(-4) to all zeros of the following cubic polynomials. Use |P_(n+1)-P_n| as a measure of the error in the iteration. Save all of the iterations. What are your conclusions? (a) f(x) = x^3-5x^2 + 2x (b) f(x) = x^3-2x^2-5 The program has to be used with MATLAB. I'm still learning how to use the program. I would love some help and tips on solving these methods....
Using Clion (an app that could use C++) to do this question: Create a new project...
Using Clion (an app that could use C++) to do this question: Create a new project called Lab05b. Keep the main file named main. • Write a function readAndPrint, and power with parameters noted below in the Functions.h file. //Your Last Name #ifndef FUNCTIONS_H #define FUNCTIONS_H #include <fstream> #include <iostream> #include <string> using namespace std; /*reads from a file and prints every item to the screen*/ // file contains sets of //int //string //Don't forget to use infile.ignore() between <<...
use C++ to create a function to calculate it. My requirements are very simple: function should...
use C++ to create a function to calculate it. My requirements are very simple: function should be general to deal with any size matrices. That's all.
Use Matlab to solve the system x2+xy3=9 , 3x2y-y3 =4 using Newton's method for nonlinear system....
Use Matlab to solve the system x2+xy3=9 , 3x2y-y3 =4 using Newton's method for nonlinear system. use each of initial guesses (x0,y0)=(1.2,2.5), (-2,2.5), (-1.2,-2.5), (2,-2.5) observe which root to which the method converges, the number of iterates required and the speed of convergence. Write the system in the form f(u) = 0, and report for each case the number of iterations needed for ||f(u)||2≤ 10-12−.
Use Newton's method to find a solution for the equation in the given interval. Round your...
Use Newton's method to find a solution for the equation in the given interval. Round your answer to the nearest thousandths. ? 3 ? −? = −? + 4; [2, 3] [5 marks] Answer 2.680 Q6. Use the Taylor Polynomial of degree 4 for ln(1 − 4?)to approximate the value of ln(2). Answer: −4? − 8?2 − 64 3 ? 3 − [6 marks] Q7. Consider the curve defined by the equation 2(x2 + y2 ) 2 = 25(x2 −...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT