0
我正在爲我正在學習的課程努力工作。我必須在C++中使用割線方法找到函數的根。我已經寫完了,我發現第二個零點在0.146;然而,我的教授正在尋找的是0.064。我已經嘗試了一切,但我無法弄清楚如何使它輸出0.064。他告訴我們要擺在那有它的功能的頭文件,這裏是頭文件:使用C++中的割線函數尋找根
#define FX0 pow(x0, 3) - 0.165*pow(x0, 2) + 3.993E-4
#define FX1 pow(x1, 3) - 0.165*pow(x1, 2) + 3.993E-4
和下面的代碼:
#include <iostream>
#include <cmath>
#include <iomanip>
#include "Secant.h"
#include "Keyboard.h"
using namespace std;
int main()
{
//Define Variables
float x0,x1,x2,tolerance,maxIterations,count,FX;
count = 0;
x0 = 0.02;
x1 = 0.05;
tolerance = .000001;
maxIterations = 100;
FX = pow(x0, 3) - 0.165*pow(x0, 2) + 3.993E-4;
//Loop statement that runs until a Zero is found
while(fabs(FX0-FX1)>tolerance && count < maxIterations && fabs(FX)>tolerance)
{
x2=x1-(FX1*((x0-x1)/(FX0-FX1)));
FX = pow(x2, 3) - 0.165*pow(x2, 2) + 3.993E-4;
x0=x1;
x1=x2;
count++;
}
//Display the zero
if (fabs(FX)<tolerance)
cout << "The zero is at x = " << setprecision(4) << x2;
//Or Report that no zero was found
else
cout << "No zeroes were found within the given function.";
return 0;
}
神聖的廢話的人,它的工作。你不知道我一直在努力爭取這個計劃多久。我不能夠感謝你! – CodingCowboy