2011-05-11 6 views
0

私のプログラムでは、方程式の根を見つけるために書いたプログラムはほぼ終了しましたが、小さな問題にぶつかりました。方程式に速度と角度のさまざまな値を割り当てるために使用しているネストされたループは機能しません。ループの途中かfxの割線への呼び出しのどこかに、私が間違っているという間違いがあります。それは、根、反復回数、およびf(x)について同じ数字を与え続けます。誰も私は、私はそれを大幅に感謝:)ループはセカントメソッドでは機能しません

#include<iostream> 
#include<cmath> 
#include<iomanip> 
#include<fstream> 

using namespace std; 

// Declaration of functions used 
void secant(double, double, double, double, double, double, double, double, double&, int&); 
double fx(double, double, double, double, double, double, double); 

const double tol=0.0001; 
// Tolerance for convergence 
const int max_iter=50; // Maximum iterations allowed 

// main program 
int main() 
{ 
    int iteration; // Number of iterations  
    double kr, uc, q, b, radians; 
    double x0, x1; // Starting values for x 
    double root; // Root found by secant method 
    const double PI = 4.0*atan(1.0); 

    ifstream datain ("shuttle.txt"); 
    ofstream dataout ("results.txt"); 
    datain >> kr >> uc >> q >> b; 

    int velocity = 16000; 
    double angle =10; 
    x0= 1000; 
    x1 = 200; 
    for (int velocity = 16000; velocity <= 17500; velocity += 500) 
    { 
     for (int angle = 10; angle <= 70; angle += 15) 
     { 
      radians= angle * PI/180 ; 
      cout << velocity << endl; 
      cout << radians << endl; 
      cout << angle << endl;     
      secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration);  
     } 
    } 
    system("pause"); 
} 

// Definition of function "secant" 
// Receives a, b, c, d and x0 values from main program 
// Returns root and the iterations required 
void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration) 
{  
    double xnminus1, xnplus1, xn; // Local variables 
    iteration=0; // Initialize iterations  
    xnminus1=x0; 
    xn=x1; 
    do 
    { 
     ++iteration; 
     xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/(fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1)); 
     cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl; 
     xnminus1 = xn; 
     xn=xnplus1; 
    } while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol)&& (iteration < max_iter)); 
    root=xnplus1; 
    cout<<"\nThe root is = "<<root<<endl; 
    cout<<"The number of iterations was = "<<iteration<<endl; 
    cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl; 
} 

// Defines "fx" 
double fx(double radians,double velocity, double kr, double uc, double q, double b, double ts) 
{ 
    return kr * pow(ts,4.0) + uc * ts - q - pow(velocity/b, 2.0) * sin(radians); 
} 
+1

は、あなたが適切に提供しましたプログラムに記述されている 'shuttle.txt'ファイルを入力しますか? – Purnima

+0

私は今、愚かな気がする、私は間違ったディレクトリに入れて、ありがとう、笑あなたが朝3で作る愚かな間違いを – Brian

答えて

0

あなたが複数定義された変数を持っている私のミスを見つけることができれば、メインで最も外側の速度や角度を削除してみてください:

int velocity = 16000; // remove this one 
double angle =10; // and this one 
x0= 1000; 
x1 = 200; 
for (int velocity = 16000; velocity <= 17500; velocity += 500) 
{ 
     for (int angle = 10; angle <= 70; angle += 15) 
     { 
+0

それを決して気にしない:) – Brian

関連する問題