From f8033ef4acbead25cf342b85ccd9631a7a46b6e5 Mon Sep 17 00:00:00 2001 From: Patrice Matz Date: Thu, 7 Dec 2017 19:05:44 +0100 Subject: [PATCH] -hier haste Basedow ;P - also: differential stuff --- SCE/SCE/DIFF.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ SCE/SCE/DIFF.h | 23 +++++++++++++++ SCE/SCE/SCE.cpp | 37 +++++++++++++++++++++++- SCE/SCE/nlgs.cpp | 14 ++++----- SCE/SCE/nlgs.h | 6 ++-- 5 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 SCE/SCE/DIFF.cpp create mode 100644 SCE/SCE/DIFF.h diff --git a/SCE/SCE/DIFF.cpp b/SCE/SCE/DIFF.cpp new file mode 100644 index 0000000..ec72ba2 --- /dev/null +++ b/SCE/SCE/DIFF.cpp @@ -0,0 +1,74 @@ +#include "DIFF.h" +using namespace std; + +DIFF::DIFF() +{ + y1values.resize(yvalues.size()); +} + + +DIFF::~DIFF() +{ +} + + +vector DIFF::diff() +{ + vector diff = yvalues; + vector xdiff (diff.size()-1); + for(unsigned int i = 0; i < xdiff.size(); i++) + { + xdiff.at(i) = diff.at(i+1) - diff.at(i); + } + + return xdiff; +} + +vector DIFF::forward_diff() +{ + for(unsigned int i=0; i < xvalues.size()-1; i++) + { + y1values.at(i) = (yvalues.at(i + 1) - yvalues.at(i)) / (xvalues.at(i + 1) - xvalues.at(i)); + } + y1values.at(25) = 0; + return y1values; +} + +vector DIFF::backward_diff() +{ + + for (unsigned int i = 1; i < xvalues.size(); i++) + { + y1values.at(i) = (yvalues.at(i) - yvalues.at(i-1)) / (xvalues.at(i) - xvalues.at(i-1)); + } + y1values.at(0) = 0; + return y1values; +} + +vector DIFF::backward_diff(vector y) +{ + vector temp; + temp.resize(y.size()); + + for (unsigned int i = 0; i < y.size() - 1; i++) + { + temp.at(i) = (y.at(i + 1) - y.at(i)) / (xvalues.at(i + 1) - xvalues.at(i)); + } + temp.at(25) = 0; + return temp; +} +vector DIFF::forward_diff(vector y) +{ + vector temp; + temp.resize(y.size()); + + for (unsigned int i = 1; i < y.size(); i++) + { + temp.at(i) = (y.at(i) - y.at(i - 1)) / (xvalues.at(i) - xvalues.at(i - 1)); + } + temp.at(0) = 0; + return temp; +} + + + diff --git a/SCE/SCE/DIFF.h b/SCE/SCE/DIFF.h new file mode 100644 index 0000000..6e39461 --- /dev/null +++ b/SCE/SCE/DIFF.h @@ -0,0 +1,23 @@ +#pragma once +#include +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 y1values; //y-values of 1st derivative + vector y2values; //y-values of 2nd derivative + vector diff(); + vector forward_diff(); + vector backward_diff(); + + vector forward_diff(vector); + vector backward_diff(vector); + + + DIFF(); + ~DIFF(); +}; + diff --git a/SCE/SCE/SCE.cpp b/SCE/SCE/SCE.cpp index 81ada93..36cc688 100644 --- a/SCE/SCE/SCE.cpp +++ b/SCE/SCE/SCE.cpp @@ -2,11 +2,31 @@ #include #include "lgs.h" #include "nlgs.h" +#include "DIFF.h" using namespace std; void cylinder(); void lgs(); void nlgs(); +void diff(); +void print_array(long double* a, int size) +{ + cout << endl; + for(int i = 0; i < size; i++) + { + cout << a[i] << "\t"; + } + cout << endl << endl; +} +void print_array(long double* a, long double* b, int size) +{ + cout << endl; + for (int i = 0; i < size; i++) + { + cout << a[i] << "," << b[i] << endl; + } + cout << endl << endl; +} int main() { @@ -17,6 +37,7 @@ int main() cout << "1 Berechungen an einem Zylinder" << endl; cout << "2 LGS loesen" << endl; cout << "3 n.LGS loesen" << endl; + cout << "5 numerische differentation" << endl; cout << "e Beenden" << endl; @@ -26,6 +47,7 @@ int main() case '1': cylinder(); break; case '2': lgs(); break; case '3': nlgs(); break; + case '5': diff(); break; default: cout << "Okay, bye" << endl; break; } @@ -52,5 +74,18 @@ void lgs() void nlgs() { - + NLGS nlgs; + cout << endl << "Bisektion: " << nlgs.bisektion(1,4,100) << endl; + cout << "Fixpunkt: " << nlgs.fixedpoint(1,100) << endl; + cout << "Newton: " <