Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <math.h>
- double diffaitken( double (*fct)(double), double x, double h0, double tau) {
- assert (tau > 0);
- double phi = 0;
- double phip = 0;
- double hp = 0;
- double help = 1;
- double h = 0;
- int n= 0;
- int j = 0;
- double erg = 0;
- double *diff = malloc(1*sizeof(double));
- double *aitk = malloc (1*sizeof(double));
- for (n=0;1 ; n++) {
- for (j = 1;j < n+4 ;j++) { // ACHTUNG MIT 1 ANGEFANGEN
- phip = phi;
- hp = h;
- if (j == 1) {
- h = h0;
- }
- else {
- help = help *2;
- h = 1/(2*help*h0);
- }
- phi = (fct(x+h)-fct(x))/h;
- diff = realloc (diff,j*sizeof(double));
- diff[j-1]=phi;
- }
- aitk = realloc (aitk,(n+1)*sizeof(double));
- aitk[n] = diff[n] - ((diff[n+1]-diff[n])*(diff[n+1]-diff[n]))/(diff[n+2]-2*diff[n+1]+diff[n]);
- if (fabs(aitk[n]) <= tau) {
- if (fabs(aitk[n-1]-aitk[n]) <= tau) {
- //printf("%f\n",aitk[n]);
- erg= aitk[n];
- return erg;
- }
- }
- else {
- if (fabs(aitk[n-1]-aitk[n]) <= tau * aitk[n]) {
- // printf("%f\n",aitk[n]);
- erg = aitk[n];
- return erg;
- }
- }
- printf("h_n= %f\n",h);
- printf("|y_n-y_n-1| = %f\n",fabs(aitk[n+1]-aitk[n]));
- printf("y_n = %f\n",aitk[n+1]);
- printf("\n");
- }
- }
- double fct( double x) {
- return x*x*x;
- }
- int main () {
- double x = 0;
- double h= 0;
- double tau= 0;
- double z = 0;
- printf("Geben Sie einen Auswertungspunkt x ein:");
- scanf("%lf",&x);
- printf("Geben Sie eine Schrittweite h ein:");
- scanf("%lf",&h);
- printf("Geben Sie ein tau ein:");
- scanf("%lf",&tau);
- fct(x) ;
- z= diffaitken(fct,x,h,tau);
- printf("Das Ergebnis ist: %f",z);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement