- added basic optimization

This commit is contained in:
Patrice Matz 2017-12-09 21:20:28 +01:00
parent 35562c0698
commit 00cbbe80f2
3 changed files with 69 additions and 15 deletions

View File

@ -11,7 +11,7 @@ DIFF::~DIFF()
}
vector<long double> DIFF::diff()
vector<long double> DIFF::diff(vector<long double> yvalues)
{
vector<long double> diff = yvalues;
vector<long double> xdiff (diff.size()-1);
@ -23,7 +23,7 @@ vector<long double> DIFF::diff()
return xdiff;
}
vector<long double> DIFF::backward_diff(vector<long double> y)
vector<long double> DIFF::backward_diff(vector<long double> y, vector<long double> xvalues)
{
vector<long double> temp;
temp.resize(y.size());
@ -35,7 +35,7 @@ vector<long double> DIFF::backward_diff(vector<long double> y)
temp.at(y.size()-1) = 0;
return temp;
}
vector<long double> DIFF::forward_diff(vector<long double> y)
vector<long double> DIFF::forward_diff(vector<long double> y, vector<long double> xvalues)
{
vector<long double> temp;
temp.resize(y.size());
@ -72,10 +72,12 @@ vector<long double> DIFF::mid_diff(vector<long double>y, vector<long double>x)
temp.at(i) = (y.at(i + 1) - y.at(i)) / (x.at(i + 1) - x.at(i));
temp.at(i) += (y.at(i + 1) - y.at(i - 1)) / (x.at(i + 1) - x.at(i - 1));
temp.at(i) /= 2;
for (; i < y.size() - 2; ++i)
{
temp.at(i) = (y.at(i + 1) - y.at(i - 1)) / (x.at(i + 1) - x.at(i - 1));
}
temp.at(i) += (y.at(i) - y.at(i - 1)) / (x.at(i) - x.at(i - 1));
temp.at(i) /= 2;
i++;

View File

@ -5,13 +5,9 @@ using namespace std;
class DIFF
{
public:
vector<long double> xvalues = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250};
vector<long double> yvalues = {0, 2926, 10170, 21486, 33835, 45251, 55634, 65038, 73461, 80905, 87368, 92852, 97355, 100878, 103422, 104986, 106193, 110246, 119626, 136106, 162095, 199506, 238775, 277065, 314375, 350704};
vector<long double> diff();
vector<long double> forward_diff(vector<long double>);
vector<long double> backward_diff(vector<long double>);
vector<long double> diff(vector<long double>);
vector<long double> forward_diff(vector<long double>, vector<long double>);
vector<long double> backward_diff(vector<long double>, vector<long double>);
static vector<long double> central_diff(vector<long double>, vector<long double>);
static vector<long double> mid_diff(vector<long double>, vector<long double>);

View File

@ -3,11 +3,13 @@
#include "lgs.h"
#include "nlgs.h"
#include "DIFF.h"
#include "OPTIMIZATION.h"
using namespace std;
void cylinder();
void lgs();
void nlgs();
void optimization();
void diff();
void print_array(long double* a, int size)
{
@ -37,6 +39,7 @@ int main()
cout << "1 Berechungen an einem Zylinder" << endl;
cout << "2 LGS loesen" << endl;
cout << "3 n.LGS loesen" << endl;
cout << "4 optimization" << endl;
cout << "5 numerische differentation" << endl;
cout << "e Beenden" << endl;
@ -47,6 +50,7 @@ int main()
case '1': cylinder(); break;
case '2': lgs(); break;
case '3': nlgs(); break;
case '4': optimization(); break;
case '5': diff(); break;
default: cout << "Okay, bye" << endl; break;
@ -82,18 +86,70 @@ void nlgs()
}
void optimization()
{
OPTIMIZATION opt;
long double lower_bound = -2, upper_bound = 2; //upper / lower - _bounds instead of theta-vector
long double increments = 0.1;
long double z = opt.g_von_theta(lower_bound, upper_bound);
long double which_theta = 1;
vector<long double> thetanew(2);
cout << "i" << "\t" << "z" << "\t" << "lb" << "\t" << "ub" << endl;
for(int i = 0; i < 41; i++)
{
cout << i << "\t" << z << "\t" << lower_bound << "\t" << upper_bound << endl;
if (which_theta == 1)
{
thetanew.at(0) = lower_bound + increments;
thetanew.at(1) = upper_bound;
}
else
{
thetanew.at(0) = lower_bound;
thetanew.at(1) = upper_bound - increments;
}
long double znew = opt.g_von_theta(thetanew.at(0), thetanew.at(1));
if(znew > z)
{
z = znew;
lower_bound = thetanew.at(0);
upper_bound = thetanew.at(1);
}
else
{
if (which_theta == 1)
which_theta = 2;
}
}
}
void diff() {
//Plotter: https://www.desmos.com/calculator
DIFF diff;
vector<long double> xvalues = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250 };
vector<long double> yvalues = { 0, 2926, 10170, 21486, 33835, 45251, 55634, 65038, 73461, 80905, 87368, 92852, 97355, 100878, 103422, 104986, 106193, 110246, 119626, 136106, 162095, 199506, 238775, 277065, 314375, 350704 };
vector<long double> AbfullX = {0, 5, 10, 15, 20 };
vector<long double> AbfullY = {1, 0.8811, 0.7366, 0.5430, 0.1698};
cout << "Erste Ableitung mit forward und backward" << endl;
print_array(diff.xvalues.data(), diff.forward_diff(diff.yvalues).data(), diff.xvalues.size());
print_array(diff.xvalues.data(), diff.central_diff(diff.yvalues, diff.xvalues).data(), diff.xvalues.size());
print_array(xvalues.data(), diff.forward_diff(yvalues, xvalues).data(), xvalues.size());
print_array(xvalues.data(), diff.central_diff(yvalues, xvalues).data(), xvalues.size());
cout << "Zweite Ableitung mit backward" << endl;
print_array(diff.xvalues.data(), diff.backward_diff(diff.forward_diff(diff.yvalues)).data(), diff.xvalues.size());
print_array(xvalues.data(), diff.backward_diff(diff.forward_diff(yvalues, xvalues), xvalues).data(), xvalues.size());
cout << "Erste Ableitung 6.1.2" << endl;
print_array(diff.xvalues.data(), diff.mid_diff(AbfullY, AbfullX).data(), AbfullX.size());
print_array(xvalues.data(), diff.mid_diff(AbfullY, AbfullX).data(), AbfullX.size());
}