-added fixed point, newton and bisektion

This commit is contained in:
Patrice Matz 2017-11-06 17:01:36 +01:00
parent a65120eb3f
commit 20b9c945f7
2 changed files with 42 additions and 16 deletions

View File

@ -9,26 +9,50 @@ long double nlgs::function(long double x)
return (pow(x, 4) - x - 10);
}
long double nlgs::round(long double x, int acc)
long double nlgs::function_fixedpoint(long double x)
{
x = x * pow(10, acc);
int temp = x;
x = temp;
x = x / pow(10, acc);
return x;
return pow(x+10, 1/4);
}
float nlgs::bisektion(long double A, long double B , int limit, int acc)
long double nlgs::function_derived(long double x)
{
long double a = A;
long double b = B;
long double c;
return 4 * pow(x, 3) - 1;
}
long double nlgs::bisektion(long double A, long double B , int limit)
{
long double a = A, b = B, c;
for (int i = limit; i--;)
{
c = (a + b) / 2;
c = round(c, acc);
if (function(a)*function(c) > 0) { a = c; }
if (function(c)*function(b) > 0) { b = c; }
}
return c;
}
long double nlgs::fixedpoint(long double start, int limit)
{
long double x = start;
for(int i = limit ; i-- ;)
{
x = function_fixedpoint(x);
}
return x;
}
long double nlgs::newton(long double start, int limit)
{
long double x = start;
for (int i = limit; i--;)
{
x = x - function(x)/function_derived(x);
}
return x;
}

View File

@ -3,12 +3,14 @@ class nlgs
{
private:
long double function(long double);
long double round(long double, int);
long double function_fixedpoint(long double );
long double function_derived(long double);
public:
float bisektion(long double, long double, int, int ); //a, b, number of iterations, accuracy
float fixedpoint();
float newton();
float secant();
long double bisektion(long double, long double, int); //a, b, number of iterations
long double fixedpoint(long double, int); //start, number of iterations
long double newton(long double, int); //start, number of iterations
long double secant();
nlgs(){};
~nlgs(){};
};