2017-05-11 18 views
0

코드가 작동하지 않는 이유는 무엇입니까? 본질적으로 필자가 작성한 함수는 파이를 테일러 시리즈를 사용하여 계산합니다. 시도 할 때마다 충돌이 발생합니다. 프로그램을 실행하십시오.C++의 테일러 계열을 사용하여 파이를 계산하는 함수

  1. 모든 변수를 정의 파이의
  2. 세트 추정치가 0
  3. 가에서 기간을 계산할 수 : 여기

    코드 뒤에 논리는 다음과 같은 내 코드

    #include <iostream> 
    #include <math.h> 
    #include <stdlib.h> 
    using namespace std; 
    
    double get_pi(double accuracy) 
    { 
    double estimate_of_pi, latest_term, estimated_error; 
    int sign = -1; 
    int n; 
    
    estimate_of_pi = 0; 
    n = 0; 
    
    do 
    { 
        sign = -sign; 
        estimated_error = 4 * abs(1.0/(2*n + 1.0)); //equation for error 
        latest_term = 4 * (1.0 *(2.0 * n + 1.0));  //calculation for latest term in series 
        estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi 
        n = n + 1;          //changing value of n for next run of the loop 
    } 
    while(abs(latest_term)< estimated_error); 
    
    return get_pi(accuracy); 
    
    } 
    
    int main() 
    { 
        cout << get_pi(100); 
    } 
    

    입니다 테일러 시리즈 및 오류를 계산 이 용어

  4. 다음 추가 파이의 추정에 대한 최신 용어는
  5. 프로그램은 다음에 시리즈의 다음 용어 및 오류를 해결하고 while 문에서 조건이 만족 될 때까지, 파이의 추정에 추가해야

도움을 주셔서 감사합니다

답변

1

함수에 몇 가지 오류가 있습니다. "// 참고 :"로 시작하는 행에 대한 내 주석을보십시오.

double get_pi(double accuracy) 
{ 
    double estimate_of_pi, latest_term, estimated_error; 
    int sign = -1; 
    int n; 

    estimate_of_pi = 0; 
    n = 0; 

    do 
    { 
     sign = -sign; 

     //NOTE: This is an unnecessary line. 
     estimated_error = 4 * abs(1.0/(2*n + 1.0)); //equation for error 

     //NOTE: You have encoded the formula incorrectly. 
     // The RHS needs to be "sign*4 * (1.0 /(2.0 * n + 1.0))" 
     //      ^^^^  ^
     latest_term = 4 * (1.0 *(2.0 * n + 1.0));  //calculation for latest term in series 
     estimate_of_pi = estimate_of_pi + latest_term; //adding latest term to estimate of pi 
     n = n + 1;          //changing value of n for next run of the loop 
    } 

    //NOTE: The comparison is wrong. 
    // The conditional needs to be "fabs(latest_term) > estimated_error" 
    //        ^^^^    ^^^ 
    while(abs(latest_term)< estimated_error); 

    //NOTE: You are calling the function again. 
    // This leads to infinite recursion. 
    // It needs to be "return estimate_of_pi;" 
    return get_pi(accuracy);  
} 

또한, main에서 함수 호출이 잘못되었습니다. 다음과 같아야합니다.

get_pi(0.001) 

용어의 절대 값이 0.001보다 작 으면 함수가 반환 할 수 있음을 나타냅니다.

다음은 나를 위해 작동하는 업데이트 된 버전의 기능입니다.

double get_pi(double accuracy) 
{ 
    double estimate_of_pi, latest_term; 
    int sign = -1; 
    int n; 

    estimate_of_pi = 0; 
    n = 0; 

    do 
    { 
     sign = -sign; 
     latest_term = sign * 4 * (1.0 /(2.0 * n + 1.0)); //calculation for latest term in series 
     estimate_of_pi += latest_term;     //adding latest term to estimate of pi 
     ++n;            //changing value of n for next run of the loop 
    } 
    while(fabs(latest_term) > accuracy); 

    return estimate_of_pi; 
} 
+0

내가 당신을 내가 게시 한 후, 지금은 광산을 수정하고 의도 한대로, 덕분에 다시 –

+0

@AmanSood, 당신은 환영합니다 작동 한 언급 한 몇 가지를 발견, 당신은 짝짓기 감사합니다. 다행스럽게 도울 수있었습니다. –

0

귀하의 신고서가 원인 일 수 있습니다.

get_pi (정확도) 대신 "estimate_of_pi"를 반환하십시오.

0

귀하의 휴식 상태가

2*n + 1 < 1/(2*n + 1)  => (2*n + 1)^2 < 1 

로 재 작성 될 수 있으며,이 긍정적 n에 대한 true 수 없을 것입니다. 따라서 루프는 끝나지 않을 것입니다. 이 문제를 해결 한 후에는 현재 (당신이 정지 조건을 고정 가정) 끝없이 반복적으로 함수를 호출

return estimated_error; 

귀하에게 반환 문을 변경해야합니다.

기타 계산에는 전혀 사용하지 않는 signaccuracy 매개 변수가 있습니다.

그런 반복에 대한 조언은 항상 최대 반복 횟수를 위반하는 것입니다. 이 경우 수학이 수렴되었다고 가정하면 수렴한다는 것을 알지만, 일반적으로 반복이 수렴하는지 결코 확신 할 수 없습니다.