always wondered how a computer might go about doing this...
i'm taking calculus at university... and we went over newton's method.
i found this interesting...
------------------------------
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
double f1(double x);
double f2(double x, double y);
int main(int argc, char *argv[])
{
double x = 5;
cout << f1(x) << endl;
cout << sqrt(x) << endl;
system("PAUSE") ;
return EXIT_SUCCESS;
}
double f1(double x)
{
return f2(x,x/2);
}
double f2(double x, double y)
{
double z = y - (y*y-x)/(2*x);
if( y == z ) return z;
else return f2(x,z);
}
---------------------------
will work for cube roots and others if you just tweak the formula.
i'm taking calculus at university... and we went over newton's method.
i found this interesting...
------------------------------
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
double f1(double x);
double f2(double x, double y);
int main(int argc, char *argv[])
{
double x = 5;
cout << f1(x) << endl;
cout << sqrt(x) << endl;
system("PAUSE") ;
return EXIT_SUCCESS;
}
double f1(double x)
{
return f2(x,x/2);
}
double f2(double x, double y)
{
double z = y - (y*y-x)/(2*x);
if( y == z ) return z;
else return f2(x,z);
}
---------------------------
will work for cube roots and others if you just tweak the formula.
Comment