-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); 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); return pow(x+10, 1/4);
int temp = x;
x = temp;
x = x / pow(10, acc);
return x;
} }
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; return 4 * pow(x, 3) - 1;
long double b = B; }
long double c;
long double nlgs::bisektion(long double A, long double B , int limit)
{
long double a = A, b = B, c;
for (int i = limit; i--;) for (int i = limit; i--;)
{ {
c = (a + b) / 2; c = (a + b) / 2;
c = round(c, acc);
if (function(a)*function(c) > 0) { a = c; } if (function(a)*function(c) > 0) { a = c; }
if (function(c)*function(b) > 0) { b = 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: private:
long double function(long double); long double function(long double);
long double round(long double, int); long double function_fixedpoint(long double );
long double function_derived(long double);
public: public:
float bisektion(long double, long double, int, int ); //a, b, number of iterations, accuracy long double bisektion(long double, long double, int); //a, b, number of iterations
float fixedpoint(); long double fixedpoint(long double, int); //start, number of iterations
float newton(); long double newton(long double, int); //start, number of iterations
float secant(); long double secant();
nlgs(){}; nlgs(){};
~nlgs(){}; ~nlgs(){};
}; };