- #1
Orcinus
- 13
- 0
Homework Statement
4. Implement a simple method to find the square root of a double precision floating point number x. A simple method is to consider the error produced by a “guess” y of the solution. Square the value y and compare with the value x. If y is correct, the error e=|y2-x| where || is the magnitude will be zero and as the value of y becomes more wrong, the error e will increase. Hence the solution is to home in on the minimum for the error function or some value close enough for our purposes. This problem actually has two solutions +/-y but we can just consider +y. Write a C program to solve this problem using a repetition method. Hint: use a while loop and devise a way to find when you get to the minimum for the error e.
This is in C by the way (Linux gcc) and obviously without using maths libraries.
The question itself is simple, I quickly devised the solution that the question's looking for: http://pastebin.com/cQGzGXN4
But I was curious of doing it another way, and after just learning about Taylor series in maths I wanted to try doing it that way. So far it's not working out so well.
Course: Engineering Programming 100, we've yet to even learn functions yet so yeah I'm going beyond the course and hence beyond my knowledge level.
Homework Equations
I don't know whether this is right, but a plot on www.desmos.com/calculator seems to work
[tex]f(x) = \sqrt{x} [/tex]
[tex]
f(x) = 1+ \sum_{n=1}^\infty\frac{\frac{(2n-2)!(-1)^{n-1}(x-1)^n}{2^{2n-1}(n-1)!}}{n!}
[/tex]
The Attempt at a Solution
Here's what I've come up with:
Code:
#include <stdio.h>
double power(double base, int index) //Note that this function does not allow fractional indicies
{
int x;
int inverse = 0;
double result = 1;
if(index<0)
{
index = -index;
inverse = 1;
}
if(index==0)
{
return 1;
}
for(x=0;x<index;x++)
{
result = base*result;
}
if(inverse == 1)
{
return (1/result);
}
else
{
return result;
}
}
int factorial(int num)
{
int x;
int result = 1;
for(x=1;x<=num;x++)
{
result = result * x;
}
return result;
}
double absolute( double num)
{
double result;
result = num;
if(result < 0)
{
result = -result;
}
return result;
}
double squareroot(double square)
{
int x = 1;
double result = 1;
while(absolute(result*result-square) > 0.000001)
{ //hello taylor series! I have no idea whether this works.
result = result + factorial(2*x-2)*power(-1, x-1)/power(2,2*x-1)/factorial(x-1)*power(square - 1, x)/factorial(x);
x++;
printf("%lf\n", result);
}
return result;
}
int main(void)
{
double square;
double result;
printf("Type the number you'd like to find the square root of: ");
//scanf("%lf", square);
square = 2; // for debugging
result = squareroot(square);
printf("The square root of %lf is %lf.\n", square, result);
return 0;
}
Note that "printf("%lf\n", result);" in squareroot function and "square = 2; // for debugging" in main function are for debugging purposes. I'm writing this on my tablet so can't compile properly but am using http://codepad.org/W5yNE67K to debug. Hence I manually enter numbers into "square = 2" to test different numbers.
The problem being that my result is coming out nothing like [itex]\sqrt{x}[/itex], the while loop spits out a NaN for some reason after a while and this brings together so much new stuff for me that I don't know where the problem(s) might lie.