From 00cbbe80f27812457fd81ec85a385686624866c0 Mon Sep 17 00:00:00 2001 From: Patrice Matz Date: Sat, 9 Dec 2017 21:20:28 +0100 Subject: [PATCH] - added basic optimization --- SCE/SCE/DIFF.cpp | 8 +++--- SCE/SCE/DIFF.h | 10 +++----- SCE/SCE/SCE.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/SCE/SCE/DIFF.cpp b/SCE/SCE/DIFF.cpp index 497e550..b40e58a 100644 --- a/SCE/SCE/DIFF.cpp +++ b/SCE/SCE/DIFF.cpp @@ -11,7 +11,7 @@ DIFF::~DIFF() } -vector DIFF::diff() +vector DIFF::diff(vector yvalues) { vector diff = yvalues; vector xdiff (diff.size()-1); @@ -23,7 +23,7 @@ vector DIFF::diff() return xdiff; } -vector DIFF::backward_diff(vector y) +vector DIFF::backward_diff(vector y, vector xvalues) { vector temp; temp.resize(y.size()); @@ -35,7 +35,7 @@ vector DIFF::backward_diff(vector y) temp.at(y.size()-1) = 0; return temp; } -vector DIFF::forward_diff(vector y) +vector DIFF::forward_diff(vector y, vector xvalues) { vector temp; temp.resize(y.size()); @@ -72,10 +72,12 @@ vector DIFF::mid_diff(vectory, vectorx) 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++; diff --git a/SCE/SCE/DIFF.h b/SCE/SCE/DIFF.h index 682b522..eb647f1 100644 --- a/SCE/SCE/DIFF.h +++ b/SCE/SCE/DIFF.h @@ -5,13 +5,9 @@ using namespace std; class DIFF { public: - vector 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 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 diff(); - - - vector forward_diff(vector); - vector backward_diff(vector); + vector diff(vector); + vector forward_diff(vector, vector); + vector backward_diff(vector, vector); static vector central_diff(vector, vector); static vector mid_diff(vector, vector); diff --git a/SCE/SCE/SCE.cpp b/SCE/SCE/SCE.cpp index 4a3ebe4..04737d9 100644 --- a/SCE/SCE/SCE.cpp +++ b/SCE/SCE/SCE.cpp @@ -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; @@ -77,23 +81,75 @@ void nlgs() NLGS nlgs; cout << endl << "Bisektion: " << nlgs.bisektion(1,4,100) << endl; cout << "Fixpunkt: " << nlgs.fixedpoint(1,100) << endl; - cout << "Newton: " < 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 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 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 AbfullX = {0, 5, 10, 15, 20 }; vector 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()); } \ No newline at end of file