August 25th, 2013, 08:56 PM

Function to perform Square Root...
Hello. ^_^
I'm working on a program that will estimate the square root of a given number with < 1/10 error. This is what I have so far:
Code:
#include <stdio.h>
double mysqrt(double x);
int main(void)
{
double mysqrt(double x);
{
double x;
double a;
int n = 0;
printf("Please enter the number you want to find the square root for:\n");
scanf("%lf", &a);
if(a < 0)
{
printf("Invalid number entered.\n");
return 0;
}
x = a;
do
{
x = (1/2.0 * (x + (a / x)));
n = n + 1;
}
while(n < 20);
printf("The square root of that number is %f.\n", x);
}
return 0;
}
It seems to be working fine on the output. My question is, how do I run a check vs. error < 1/10 to make sure it meets that requirement? Can someone point me in the correct direction?
August 26th, 2013, 01:31 AM

Well the first question would be, 1/10th of what?
The initial value?
The actual result?
In the actual loop, you do things like
error = fabs( x*x  a ); // the original input
or
error = fabs(xa);
August 26th, 2013, 02:35 AM

Initial Value..
Originally Posted by salem
Well the first question would be, 1/10th of what?
The initial value?
The actual result?
In the actual loop, you do things like
error = fabs( x*x  a ); // the original input
or
error = fabs(xa);
Sorry, 1/10 of the initial value, which I believe would be checked against a/2, correct?
August 26th, 2013, 02:51 AM

> Sorry, 1/10 of the initial value, which I believe would be checked against a/2, correct?
This doesn't make much sense.
10% of 10000 is 1000.
The square root of 10000 is 100.
Saying the answer is 100 +/ 1000 isn't an especially accurate result.
It gets much worse with larger numbers.
August 26th, 2013, 11:52 AM

Apologies...
Originally Posted by salem
> Sorry, 1/10 of the initial value, which I believe would be checked against a/2, correct?
This doesn't make much sense.
10% of 10000 is 1000.
The square root of 10000 is 100.
Saying the answer is 100 +/ 1000 isn't an especially accurate result.
It gets much worse with larger numbers.
I'm sorry, I'm terrible at explaining what I'm thinking... Our initial guess is supposed to be a/2 then run iterations from there. The relative error should be < 1/10%.
August 26th, 2013, 01:21 PM

OK, let's start with a concrete example.
sqrt(123456) is 351.363060096
Now one interpretation of 1/10 is 0.1, which may mean computing the answer to 1 decimal point.
Perhaps this means you want to stop if the computed answer is anywhere between 351.27 to 351.45.
That is, fabs( sqrt(123456)  mysqrt(123456) ) < 0.1
Another interpretation is that the square of the result should be within 0.1 of the original number.
That is, fabs( 123456  mysqrt(123456)*mysqrt(123456) ) < 0.1
Or it could be something else.
The point is, you need to find out exactly what the intention is, if you're going to write the correct code to meet the objective.
August 26th, 2013, 02:36 PM

Originally Posted by salem
The point is, you need to find out exactly what the intention is, if you're going to write the correct code to meet the objective.
This is exactly what is written:
" (x_(k+1)  x_(k))/(x_(k+1))<0.001 (i.e. error is less than 1/10%) "
So, basically it wants us to iterate until the difference between x_(k) and x_(k+1) is less than the relative error.
If I am understanding this correctly, I need to figure out how to set the first iteration to begin a/2 and continue until the outcome is meets the relative error parameter. So, I'm not sure if I need to change the n< equation to a different number of iterations to achieve that, or if I need to compare the result and continue the iteration until the result meets the parameter...
August 26th, 2013, 03:44 PM

Well the first thing to do is write the code using the same variable names as the spec.
Eg.
Code:
xk = a / 2;
do
{
xk_1 = (1/2.0 * (xk + (a / xk)));
n = n + 1;
// calculate error term here
xk = xk_1;
}
while(n < 20);
August 26th, 2013, 04:15 PM

Thank you!!!
Got it working! Thank you! ^_^