-added integrals

This commit is contained in:
Patrice Matz 2017-12-12 15:12:54 +01:00
parent 51e8f5cb0e
commit 2caaebac69
4 changed files with 101 additions and 26 deletions

43
SCE/SCE/INTEGRAL.cpp Normal file
View File

@ -0,0 +1,43 @@
#include "INTEGRAL.h"
#include <cmath>
using namespace std;
INTEGRAL::INTEGRAL()
{
}
INTEGRAL::~INTEGRAL()
{
}
long double INTEGRAL::funkt(long double x)
{
return sin(x);
}
long double INTEGRAL::trapz(long double x0, long double x1, long double precision)
{
long double A = 0, a = x0, b = a + precision;
for(; a < x1; a += precision)
{
A += (b - a)*((funkt(b) + funkt(a))/2);
b += precision;
}
return A;
}
long double INTEGRAL::quad(/*long double (* funk)(long double), */long double x0, long double x1, long double precision)
{
long double A = 0, a = x0, b = a + precision;
int fak = 1;
for (int i = 0; a < x1; a += precision, i++)
{
A += fak*funkt(a);
i % 2 ? fak = 2 : fak = 4;
}
return A*precision/3;
}

11
SCE/SCE/INTEGRAL.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
class INTEGRAL
{
public:
INTEGRAL();
~INTEGRAL();
long double funkt(long double);
long double trapz(long double, long double, long double);
long double quad(/*long double (*funk)(long double), */long double, long double, long double);
};

View File

@ -9,6 +9,6 @@ OPTIMIZATION::~OPTIMIZATION()
long double OPTIMIZATION::g_von_theta(long double x, long double y) long double OPTIMIZATION::g_von_theta(long double x, long double y)
{ {
return -(x*x + y*y); return -(2*x + y*y);
} }

View File

@ -4,6 +4,10 @@
#include "nlgs.h" #include "nlgs.h"
#include "DIFF.h" #include "DIFF.h"
#include "OPTIMIZATION.h" #include "OPTIMIZATION.h"
#include "INTEGRAL.h"
#include <math.h>
#define PI 3.14159265358979323846
using namespace std; using namespace std;
void cylinder(); void cylinder();
@ -11,6 +15,8 @@ void lgs();
void nlgs(); void nlgs();
void optimization(); void optimization();
void diff(); void diff();
void integ();
void print_array(long double* a, int size) void print_array(long double* a, int size)
{ {
cout << endl; cout << endl;
@ -41,10 +47,12 @@ int main()
cout << "3 n.LGS loesen" << endl; cout << "3 n.LGS loesen" << endl;
cout << "4 optimization" << endl; cout << "4 optimization" << endl;
cout << "5 numerische differentation" << endl; cout << "5 numerische differentation" << endl;
cout << "6 numerische Integration" << endl;
cout << "e Beenden" << endl; cout << "e Beenden" << endl;
cin >> auswahl; cin >> auswahl;
cout << endl;
switch(auswahl) switch(auswahl)
{ {
case '1': cylinder(); break; case '1': cylinder(); break;
@ -52,6 +60,7 @@ int main()
case '3': nlgs(); break; case '3': nlgs(); break;
case '4': optimization(); break; case '4': optimization(); break;
case '5': diff(); break; case '5': diff(); break;
case '6': integ(); break;
default: cout << "Okay, bye" << endl; break; default: cout << "Okay, bye" << endl; break;
} }
@ -90,47 +99,51 @@ void optimization()
{ {
OPTIMIZATION opt; OPTIMIZATION opt;
long double lower_bound = -2, upper_bound = -2; //upper / lower - _bounds instead of theta-vector long double x = -5, y = -5,
long double increments = 0.1; xnew, ynew,
increments = 0.1,
z = opt.g_von_theta(x, y),
znew = 0, zold = 1,
zinitial = z;
int xTOy = 1, iAThalf = 100; //iAThalf: max iterations/2
long double z = opt.g_von_theta(lower_bound, upper_bound); cout << "i" << "\t" << "z" << "\t" << "x" << "\t" << "y" << endl;
long double which_theta = 1;
vector<long double> thetanew(2); for(int i = 0 ; i <= 2 * iAThalf + 1; ++i) // add. exit crit. needed
cout << "i" << "\t" << "z" << "\t" << "lb" << "\t" << "ub" << endl;
for(int i = 0; i < 41; i++)
{ {
cout << i << "\t" << z << "\t" << x << "\t" << y << endl;
cout << i << "\t" << z << "\t" << lower_bound << "\t" << upper_bound << endl; if (xTOy == 1)
if (which_theta == 1)
{ {
thetanew.at(0) = lower_bound + increments; xnew = x;
thetanew.at(1) = upper_bound; ynew = y;
if (zinitial / 2 == z) //appropriate criteria?
{
iAThalf = i;
ynew = y + increments;
}
else
xnew = x + increments;
} }
else else
{ {
thetanew.at(0) = lower_bound; xnew = x;
thetanew.at(1) = upper_bound + increments; ynew = y + increments;
} }
long double znew = opt.g_von_theta(thetanew.at(0), thetanew.at(1)); znew = opt.g_von_theta(xnew, ynew);
zold = z;
if(znew > z) if(znew > z)
{ {
z = znew; z = znew;
lower_bound = thetanew.at(0); x = xnew;
upper_bound = thetanew.at(1); y = ynew;
} }
else else
{ if (xTOy == 1)
if (which_theta == 1) xTOy = 2;
which_theta = 2;
}
} }
cout << endl;
} }
void diff() { void diff() {
@ -153,3 +166,11 @@ void diff() {
print_array(xvalues.data(), diff.mid_diff(AbfullY, AbfullX).data(), AbfullX.size()); print_array(xvalues.data(), diff.mid_diff(AbfullY, AbfullX).data(), AbfullX.size());
} }
void integ()
{
INTEGRAL integ;
cout << integ.trapz(0, (PI / 2), 0.001) << endl << endl;
cout << integ.quad(0, (PI / 2), 0.001) << endl << endl;
}