commit
81a1daebfc
1 changed files with 95 additions and 0 deletions
Split View
Diff Options
@ -0,0 +1,95 @@ |
|||
//============================================================================
|
|||
// Name : CPUebung2.cpp
|
|||
// Author : Pauli
|
|||
// Version :
|
|||
// Copyright : Your copyright notice
|
|||
// Description : Hello World in C++, Ansi-style
|
|||
//============================================================================
|
|||
|
|||
#include <iostream>
|
|||
#include <math.h>
|
|||
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<pow(10,-2)){ |
|||
printf("Extremum liegt bei x= %f", x2); |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
return 0; |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save