最近我一直在研究一种发动机推力,它可以计算给定值下的坡度。
我已经有很多代码可以工作,但我似乎无法让方程函数工作。这个人应该根据图形和牛顿上的特定点列出值,然后给出一个不同的时间,计算机将在给定的时间之间找到一个值并进行斜率计算。
当然这是行不通的,在这一点上我真的很迷茫,我 100% 确定我的循环在函数中是错误的,但我不确定我的方程是错误的。
基本上程序应该这样做
x y
.19 14.5
.24 6.0
.40 4.4
Enter a time: .21
((.21-.19)/.24-.19))*(6-14.5)+14.5=11.1
the thrust at time .21 is 11.1
源
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int grid_rows=50;
const int grid_cols=2;
double slope(double thrust[grid_rows][grid_cols],double time);
// Constant Declarations
const double PI = 3.1415926535; // the radius/diameter of a circle
// Main Program
int main( )
{
system("CLS");
cout << "Take Home #12 by - "
<< "CETUAnn";
double thrust[grid_rows][grid_cols];
double time;
double newton;
char ans;
int i=0;
int j=0;
cout << "Enter thrust curve data (0 for thrust end list): "<<endl;
for(i=0;i < grid_rows; i++)
{
for(j=0; j< grid_cols;j++)
{
cin >> thrust[i][j];
if(thrust[i][j]==0)
{
break;
}
}
if(thrust[i][j]==0)
{
break;
}
}
do
{
cout << "Enter a time: "<<endl;
cin >> time;
newton=slope(thrust,time);
cout << "The thrust at time "<<time << " is " << newton << " newtons." <<endl:
cout << "Would you like another thrust value? (Y or N): " <<endl;
cin >> ans;
}while(ans=='Y'||ans=='y');
}
double slope(double thrust[50][2],double time)
{
double newton;
while(time > thrust[50][2]);
{
for(int i=0;i < grid_rows; i++)
{
for( int j=0; j< grid_cols;j++)
{
newton=((time - thrust[i][j])/(thrust[i][j]-thrust[i][j]))
*(thrust[i][j]-thrust[i][j])+thrust[i][j];
return newton;
}
}
}
}
double slope(double thrust[50][2],double time)
{
double newton;
while(time > thrust[50][2]);
{
for(int i=0;i < grid_rows; i++)
{
for( int j=0; j< grid_cols;j++)
{
newton=((time - thrust[i][j])/(thrust[i][j]-thrust[i][j]))
*(thrust[i][j]-thrust[i][j])+thrust[i][j];
return newton;
}
}
}
}
我看到你的算法存在一些问题。
1)你在这里被零误差除以。
((time - thrust[i][j])/(thrust[i][j]-thrust[i][j]))
2)你的循环永远不会运行(总是在第一次迭代时返回)。
return newton;
3)如果你修复(2)记住你可能永远被困在while循环中,(时间和推力的值[50][2]从未改变)。
while 循环末尾的 ";" 也是故意的吗?
while(time > thrust[50][2]);
您可能希望将坡度方法更改为以下内容。
double slope (double x1, double x2, double y1, double y2, double time){
double result = 0;
if ((x2-x1) != 0 ){ // google 'double comparison" you may want to use EPSILON instead
result = ((time - x1)/(x2-x1)) * (y2-y1) + y1
}
return result;
}
要使用它,您可能需要执行以下操作
。... assuming trust
[row][0] contains all the x
[row][1] contains all the y
double [lastEntry-1] results;
for(int i=0; i< lastEntry-1; i++){
results[i] = slope ( thrust[i][0], //x1
thrust[i+1][0],//x2
thrust[i][1], //y1
thrust[i+1][1],//y2
time);
}
我把如何从 cin 填充推力作为你的练习。