Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <math.h>
- #include <stdlib.h> //für Absolutbetrag für long: labs(long k)
- #include <string>
- #include <fstream>
- using namespace std;
- //Deklarationen
- double ffun(double x);
- double gfun(double x, double* a);
- double avabsError(double f[1001][2], double g[1001][2]);
- double* aufgabe3(double f[1001][2]);
- void funToData(double f[1001][2], double g[1001][2], int i);
- int main()
- {
- //Zufallsgenerator
- //std::random_device rand;
- //std::mt19937 gen(rand());
- std::default_random_engine gen;
- std::uniform_real_distribution<double> distr10(-10, 10);
- std::uniform_real_distribution<double> distr3(-3, 3);
- std::uniform_real_distribution<double> distr01(-0.1, 0.1);
- double arr_f[1001][2];
- double arr_g[1001][2];
- //Aufgabe2
- double a[7]= {-0.5, 0.25, -0.125, 0.0625, -0.03125, 0.015625, -0.0078125};
- for (int i = 0; i < 1001; ++i)
- {
- double x=distr10(gen);
- arr_f[i][0]=x;
- arr_f[i][1]=ffun(x);
- arr_g[i][0]=x;
- arr_g[i][1]=gfun(x, a);
- }
- cerr << "Aufgabe2:" << endl;
- cerr << "Durchschnittlicher absoluter Fehler: " << avabsError(arr_f,arr_g) <<endl;
- funToData(arr_f, arr_g, 2);
- cerr << "Zum Plotten bitte die Dateien funFA2.txt und funGA2.txt benutzen" << endl <<endl;
- //Aufgabe3
- double var_a[7];
- double a_best[7];
- double err=INFINITY;
- for(int j=0; j<1000; ++j)
- {
- for (int i = 0; i < 7; ++i)
- var_a[i]=distr3(gen); //zufällige Kombination der a_i
- //Berechnung des Fehlerwertes
- for (int i = 0; i < 1001; ++i)
- {
- arr_g[i][0]=arr_f[i][0]; //auch wenn es die Aufgabenstellung nicht präzisiert: ist irwie schöner die alten x Werte festzulassen
- arr_g[i][1]=gfun(arr_f[i][0], var_a);
- }
- double fg_err=avabsError(arr_f,arr_g);
- if(fg_err<err)
- {
- err=fg_err;
- for(int i=0; i<7; ++i)
- {
- a_best[i]=var_a[i];
- funToData(arr_f, arr_g, 3); //schon hier erstellen, damit das beste g nicht zwischengespeichert werden muss
- }
- }
- }
- cerr << "Aufgabe3:" << endl;
- cerr << "Minimaler durchschnittlicher absoluter Fehler: " << err << endl;
- cerr << "Bei Koeffizienten:" << endl;
- for(int i=0; i<7; ++i)
- {
- cerr << "a_" << i << "= " << a_best[i] << endl;
- }
- cerr << "Zum Plotten bitte die Dateien funFA3.txt und funGA3.txt benutzen" << endl;
- //Aufgabe4
- err=INFINITY;
- for(int i=0; i<7; ++i)
- {
- a_best[i]=var_a[i];
- funToData(arr_f, arr_g, 4); //schon hier erstellen, damit das beste g nicht zwischengespeichert werden muss
- }
- for(int j=0; j<100; ++j)
- {
- for (int i = 0; i < 7; ++i)
- var_a[i]=distr3(gen); //zufällige Kombination der a_i
- //Inkrement addieren
- for(int k=0; k<1000; ++k)
- {
- double inkm=distr01(gen);
- for(int i=0; i<7; i++)
- {
- var_a[i]+=inkm;
- }
- //g neu berechnen
- for (int i = 0; i < 1001; ++i)
- {
- arr_g[i][0]=arr_f[i][0];
- arr_g[i][1]=gfun(arr_f[i][0], var_a);
- }
- //falls besserer Fehler...
- double fg_err=avabsError(arr_f,arr_g);
- if(fg_err<err)
- {
- err=fg_err;
- //...dann als beste Kombination speichern
- for(int i=0; i<7; ++i)
- {
- a_best[i]=var_a[i];
- funToData(arr_f, arr_g, 4); //schon hier erstellen, damit das beste g nicht zwischengespeichert werden muss
- }
- }
- }
- }
- cerr << "Aufgabe4:" << endl;
- cerr << "Minimaler durchschnittlicher absoluter Fehler: " << err << endl;
- cerr << "Bei Koeffizienten:" << endl;
- for(int i=0; i<7; ++i)
- {
- cerr << "a_" << i << "= " << a_best[i] << endl;
- }
- cerr << "Zum Plotten bitte die Dateien funFA4.txt und funGA4.txt benutzen" << endl;
- }
- double ffun(double x)
- {
- return (-4*cos(x/3)+sin(15/(labs(0.5*x+2)+1)+0.2*x));
- }
- double gfun(double x, double* a)
- {
- long long sum=0;
- for(int i=0; i<7; i++)
- {
- sum+=a[i]*pow(x,i);
- }
- return sum;
- }
- double avabsError(double f[1001][2], double g[1001][2])
- {
- long long sum=0;
- for(int i=0; i<1001; i++)
- {
- sum+=labs(f[i][1]-g[i][1]);
- }
- return sum/1001;
- }
- void funToData(double f[1001][2], double g[1001][2], int aufgabe)
- {
- std::ofstream fschreiben;
- std::ofstream gschreiben;
- if(aufgabe==2)
- {
- fschreiben.open("funFA2.txt");
- gschreiben.open("funGA2.txt");
- }
- else if (aufgabe==3)
- {
- fschreiben.open("funFA3.txt");
- fschreiben.open("funGA3.txt");
- }
- else if (aufgabe==4)
- {
- fschreiben.open("funFA4.txt");
- fschreiben.open("funGA4.txt");
- }
- for(int i=0; i<1001; i++)
- {
- fschreiben << f[i][0] << " " << f[i][1] << endl;
- gschreiben << g[i][0] << " " << g[i][1] << endl;
- }
- fschreiben.close();
- gschreiben.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement