私は、その3次関数の根を見つけるために3次関数の2分法を使うプログラムをC++で作成しようとしています。今私はこれを持っている:C++での二分法
#include <iostream>
#include <cmath>
using namespace std;
int functie(double a,double b,double c,double d,double x){
double y;
y = (a*x*x*x + b*x*x + c*x + d);
return y;
}
int main(){
int a,b,c,d;//no comment
cout << "enter of form: ax^3 + bx^2 + cx + d (integers)" << endl << "a: ";
cin >> a;
cout << endl << "b: ";
cin >> b;
cout << endl << "c: ";
cin >> c;
cout << endl << "d: ";
cin >> d;
double min, max, temp;
min = -100;
max = 108.54267542;
while(functie(a,b,c,d,max) == 0 ||functie(a,b,c,d,min) == 0){
temp = (max + min)/2;
if(functie(a,b,c,d,min) < 0 && functie(a,b,c,d,temp) < 0 || functie(a,b,c,d,min) > 0 && functie(a,b,c,d,temp) > 0){
min = temp;
} else {max = temp;}
}
cout << min << endl;
cout << max << endl;
cout << temp << endl;
system("pause");
return 0;
}
しかし、それは動作しません。プログラムの最後にあるcoutは入力値のみを出力します。 (私の最大値がどうしてこんなに奇妙なのか疑問がある場合、最大値が+100でルートが0の場合、プログラムがクラッシュするのを防ぐためです...)
いくつかの時間とこれをチェックしたい、ありがとう。
チェックを外す:検査でエラーを見つけ出すことは生産的ではありません。 –
ところで、あなたはクローズの投票をどうやって行うのですか(私はこの特定の質問に投票すると言っているわけではありません)。 –
@BorisStrandjevあなたはあなたのコードをデバッガでステップ実行したり、 –