Я пытаюсь создать программу для школы, в которой будет использоваться метод поиска секущих корней.Уравнение таково:
(v / b) ^ 2sin (alpha) = kr * Ts ^ 4 + Uc * Ts -q
и мне нужно найти Ts.
К сожалению, пример, который я использовал в качестве своей базы, не работает для меня:.Корень, который он дает - -1.QNAN или что-то в этом роде, поэтому у меня где-то есть ошибка в методе secant, я новичок в C ++, поэтому у меня много проблем с этим.Буду признателен за любую помощь, также, если кто-то может сказать мне коды, чтобы упростить размещение кода, я был бы очень признателен.Спасибо, вот мой код:
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
void secant (double, double, 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
int main ()
{
double kr, uc, q, b, radians, Ts, x0, x1, root;
int iteration;
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 (angle, radians, velocity, kr, uc, q, b, Ts, x0, x1, root, iteration);
}
}
system("pause");
return 0;
}
void secant(double angle, double radians, double velocity, double kr, double uc, double q, double b, double Ts, double x0, double x1, double root, int iteration)
{
double xnminus1, xnplus1, xn;
iteration=0;
xnminus1=x0;
xn=x1;
do
{
++iteration;
xnplus1 = xn - fx(kr, uc, Ts, q, velocity, radians, xn)*(xn-xnminus1)/
(fx(kr, uc, Ts, q, velocity, radians,xn)-fx(kr, uc, Ts, q, velocity, radians,xnminus1));
cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
xnminus1 = xn;
xn=xnplus1;
}
while ((fabs(fx(kr, uc, Ts, q, velocity, radians, 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(kr, uc, Ts, q, velocity, radians, root) <<endl<<endl;
}
double fx(double kr, double uc, double Ts, double q, double velocity, double b, double radians)
{
return kr * pow(Ts, 4.0) + uc * Ts - q - pow(velocity/b, 2.0) * sin(radians);
}