2010-12-13 8 views
0

Ich habe diese Frage beim Studieren für das Finale bekommen, und ich kann es nicht zur Arbeit bringen. Die Frage selbst ist unten gezeigt. Jede Hilfe, wie dies angegangen werden könnte, wäre sehr willkommen.Zentrierte-Differenzen-Approximationen in C

Unten ist der Code von einer ähnlichen Frage, die ich in Angriff genommen habe. Ich hoffe, es kann auf die Bekämpfung dieser Frage als Grundlage verwendet werden, ist

#include <stdio.h> 
    #include <stdlib.h> 
    #include <cmath> 
    using namespace std; 

     double f(double x) 
     { 
      return (cos(x)); 
     } 


    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    Numerical Differentiation Formulae (n-th derivative) 
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ 
    void dnf_dxn (int n, double x, double h, double& fd, double& cd) 
    { 

    if(n==1) 
     // Approximation to the 1st Derivative of f at x 
     { 
     // 1st order forward differencing 
     fd = (f(x+h) - f(x))/h; 

     // 2nd order centered differencing 
     cd = (f(x+h) - f(x-h))/(2*h); 

     } 

    else if(n==2) 
     // Approximation to the 2nd Derivative of f at x 
     { 
     // 1st order forward differencing 
     fd = (f(x+2*h) - 2*f(x+h) + f(x))/(h*h); 

     // 2nd order centered differencing 
     cd = (f(x+h) - 2*f(x) + f(x-h))/(h*h); 

     } 

    else if(n==3) 
     // Approximation to the 3rd Derivative of f at x 
     { 
     // 1st order forward differencing 
     fd = (f(x+3*h) - 3*f(x+2*h) + 3*f(x+h) - f(x))/(h*h*h); 

     // 2nd order centered differencing 
     cd = (f(x+2*h) - 2*f(x+h) + 2*f(x-h) - f(x-2*h))/(2*h*h*h); 

     } 

    else 
     { 
     printf("Only derivatives of orders 1, 2 and 3 are implemented. \n"); 
     getchar(); 
     exit(1); 
     } 

    } 



    /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
     NUM_DIFF  M A I N  P R O G R A M 
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ 
    int main() 
    { 

    printf("\n Numerical Differentiation of f(x)=cos(x) at x=1 \n \n"); 
    printf(" Derivative of order 1, 2 and 3 using forward  \n"); 
    printf(" and centered difference approximations (h=0.01): \n \n"); 


    double x = 0.5; 
    double h = 0.01; 
    int n; 
    double fd, cd, exact, cd_error, fd_error; 
    double true_fx = - sin(x); 
    double true_fxx = - cos(x); 
    double true_fxxx = sin(x); 


    printf("Derivative Stepsize Differencing  Result Abs Error \n"); 

    for(n=1; n<4; n++) 
     { 

     dnf_dxn (n, x, h, fd, cd); 

     if(n==1) 
      { exact = true_fx; } 
     else if(n==2) 
      { exact = true_fxx; } 
     else 
      { exact = true_fxxx; } 

     fd_error = abs(exact - fd); 
     cd_error = abs(exact - cd); 
     printf("  %i  %4.2f  Forward  %10.7f %10.3e \n", 
        n, h, fd, fd_error); 
     printf("      Centered %10.7f %10.3e \n", 
         cd, cd_error); 

     } 

    printf("\n \n <Press the RETURN key to exit num_diff.cpp> \n \n"); 
    getchar(); 

    } 

Hier ist die eigentliche Frage: The assignment

Antwort

1

Nun, zum einen, sagen Sie, dass Sie diese Unterschiede bei x = 1 kalkulieren, aber sie sind tatsächlich berechnen sie bei x = 0.5.

Es ist nicht genug zu sagen "es funktioniert nicht." Was erwartest du und was bekommst du stattdessen?

+0

Tut mir leid, dass der obige Code von einer anderen Frage stammt, die aber mit der Frage zusammenhängt, nach der ich frage, daher der Unterschied in Variablen und Gleichungen. Ich hätte das deutlicher machen sollen. Mein Problem ist, dass nach dem Ersetzen der Variablen und Gleichungen mit den richtigen, ich nicht weiß, was als nächstes zu tun ist. Das Bit bezüglich h hat mich komplett verloren. – David