From 81a1daebfca241b8cecf44ebc5e7846f4c5ff1dc Mon Sep 17 00:00:00 2001 From: maasp Date: Mon, 20 Apr 2020 19:24:09 +0200 Subject: [PATCH] Hallo --- src/CPUebung2.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/CPUebung2.cpp diff --git a/src/CPUebung2.cpp b/src/CPUebung2.cpp new file mode 100644 index 0000000..2d9c0f2 --- /dev/null +++ b/src/CPUebung2.cpp @@ -0,0 +1,95 @@ +//============================================================================ +// Name : CPUebung2.cpp +// Author : Pauli +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +#include +#include +using namespace std; +void funktion(double *fp, double h, double x){ + //Funktionswerte in Array zusammenfassen, mit *fp Zeiger auf double Array + for (int i=0; i<5; i++){ + if (i>=3){ + fp[i]=log(x-(i-2)*h); /*Funktionswerte für f[3]=f(x-h) und f[4]=f(x-2h) + berechnen*/ + } + else { + fp[i]=log(x+i*h); /*Funktionswerte für f[0]=f(x), f[1]=f(x+h) und + f[2]=f(x+2h) berechnen*/ + } + } +} +// analog zu funktion nur für Aufgabe 2 +void funktion2(double *fp2, double h, double x){ + for (int i=0; i<5; i++){ + if (i>=3){ + fp2[i]=pow(log(x-(i-2)*h),1/x); + } + else { + fp2[i]=pow(log(x+i*h),1/x); + } + } +} +void ersteableitung(double x, double h, double *dfp1) { + double *f; //Zeiger um auf Array aus funktion + f=(double *)malloc(5*sizeof(double)); //Platz für array freimachen + funktion(f,h,x); //Array f mit Werten füllen + + *dfp1 = (f[1] - f[3]) / (2 * h); //Differenzenquotient + free(f); //Speicherbereich freigeben +} +void zweiteableitung(double x, double h, double *dfp2){ + double *f; + f=(double *)malloc(5*sizeof(double)); + funktion(f,h,x); + + *dfp2 = (f[1]-2*f[0]+f[3])/(pow(h,2)); //doppelter Differenzenquotient + free(f); +} +void dritteableitung(double x, double h, double *dfp3){ + double *f; + f=(double *)malloc(5*sizeof(double)); + funktion(f,h,x); + + *dfp3 = (f[2]-2*f[1]+2*f[3]-f[4])/(2*pow(h,3)); //dreifacher Differenzenquotient + free(f); +} +//analog zu ersteableitung aber mit funktion2 +void ableitung2(double x, double h, double *dfp) { + double *f; + f=(double *)malloc(5*sizeof(double)); + funktion2(f,h,x); + + *dfp = (f[1] - f[3]) / (2 * h); + free(f); + +} +int main() { + double x = 3; //Entwicklungspunkt + double h = 0.0001; //Epsilon + double df; //Variable um Ergebnis zu speichern + + ersteableitung(x,h,&df); + printf("%f \n", df); /*Aufrufen der Funktion ersteableitung und + ausgabe der Ergebniss*/ + + zweiteableitung(x,h,&df); + printf("%f \n", df); + + dritteableitung(x,h,&df); + printf("%f \n", df); + + for( double x2=1.0;x2<=6.0; x2+=h){ + ableitung2(x2,h,&df); + if(df