2010-12-13 4 views
0

결승에서 공부할 때이 질문을 보았습니다. 제대로 작동하지 않을 수 있습니다. 질문 자체가 아래에 나와 있습니다. 이 문제를 해결하는 방법에 대한 도움을 주시면 감사하겠습니다.C의 가운데 차이 근사법

다음은 내가 수행 한 유사한 질문의 코드입니다. 나는이 질문이 여기에

#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(); 

    } 

태클에 기초로 실제 질문은 사용할 수 있기를 바랍니다 : The assignment

답변

1

글쎄, 하나, 당신은 당신이 x = 1에서 이러한 차이를 계산하는 것을 말하지만, 당신이 실제로 그들을 x = 0.5에서 계산합니다.

"작동하지 않는다"고 말하는 것만으로는 충분하지 않습니다. 당신은 무엇을 기대하고 있으며, 대신 무엇을 얻고 있습니까?

+0

죄송합니다. 위의 코드는 다른 질문에서 가져온 것이지만 내가 요구하는 것과 관련되어 있으므로 변수 및 방정식의 차이가 있습니다. 나는 그것을 분명하게해야했다. 내 문제는 변수 및 방정식을 올바른 것으로 바꾸면 다음에 수행 할 작업을 알 수 없다는 것입니다. h에 관한 조금은 나를 완전하게 잃어 버렸다. – David