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* diff( double (*fct)(double), double x, double h0, double tau, int * n) {
- assert (tau > 0);
- double phi = 0;
- double phip= 0;
- double hp= 0;
- double help = 1;
- double h = 0;
- double * z= malloc (1*sizeof(double));
- while(1) {
- phip = phi;
- hp = h;
- if (*n == 1) {
- h = h0;
- }
- else {
- help = help *2;
- h = 1/(2*help*h0);
- }
- phi = (fct(x+h)-fct(x))/h;
- z = realloc(z,*n *sizeof(double));
- z[*n-1]= phi;
- if (fabs(phi) <= tau && fabs(phip-phi)<= tau ) {
- return z;
- }
- if ( fabs(phi) > tau && fabs (phip - phi) <= tau * fabs(phip)) {
- return z;
- }
- *n=*n+1;
- }
- }
- double fct (double x) {
- return x*x;
- }
- void printVector(double * vector, int dim) {
- int j = 0;
- for (j= 0; j < dim; j++) {
- printf("%f\n",vector[j]);
- }
- }
- int main () {
- double x = 0;
- double tau = 0;
- int n = 1;
- double *folge = NULL;
- double h0 = 0;
- printf("Geben Sie einen Startwert x ein:" );
- scanf("%lf",&x);
- printf("Geben Sie ein h0 ein: ");
- scanf("%lf",&h0);
- printf("Geben Sie ein tau ein:");
- scanf("%lf",&tau);
- folge= diff(fct,x,h0,tau,&n);
- printVector(folge,n);
- free (folge);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement