如何编写嵌套的 for 循环
How to write a nested for-loop
我是 运行 一个执行常微分方程欧拉近似的程序。选择的步长越小,近似值越准确。我可以使用以下代码让它在设定的步长下工作:
#include <iostream>
using std::cout;
double f (double x, double t)
{
return t*x*x-t;
}
int main()
{
double x=0.0,t=0.0,t1=2.0;
int n=20;
double h = (t1-t) / double(n);
// ----- EULERS METHOD
for (int i=0; i<n; i++)
{
x += h*f(x,t);
t += h;
}
cout << h << " " << x << "\n";
}
所以此代码对 n=20 运行欧拉近似,对应步长 0.1,并输出步长和 x(2) 的近似值。我想让 top 知道如何循环这段代码(对于不同的 n 值),以便它输出这个,然后是越来越小的步长和相应的近似值。
即输出是这样的:
0.1 -0.972125
0.01 -0.964762
0.001 -0.9641
等
所以我在 for 循环中尝试了一个 for 循环,但它给了我一个奇怪的极值输出。
#include <iostream>
using std::cout;
double f (double x, double t)
{
return t*x*x-t;
}
int main()
{
double x=0.0,t=0.0,t1=2.0;
for (int n=20;n<40;n++)
{
double h = (t1-t)/n;
for (int i=0;i<n;i++)
{
x += h*f(x,t);
t += h;
}
cout << h << " " << x << "\n";
}
}
如果我没理解错的话,您想在主函数中针对不同的 n 值执行第一段代码。那么你的问题是变量 x、t 和 t1,它们在循环之前设置一次并且永远不会重置。你想让它们在你的外循环中:
#include <iostream>
using std::cout;
double f( double x, double t )
{
return t * x * x - t;
}
int main()
{
for ( int n = 20; n < 40; n++ )
{
double x = 0.0, t = 0.0, t1 = 2.0;
double h = ( t1 - t ) / n;
for ( int i = 0; i < n; i++ )
{
x += h * f( x, t );
t += h;
}
cout << h << " " << x << "\n";
}
}
为此使用一个函数,使它更清楚:
#include <iostream>
using std::cout;
double f( double x, double t )
{
return t * x * x - t;
}
void eulers( const int n )
{
double x = 0.0, t = 0.0, t1 = 2.0;
double h = ( t1 - t ) / n;
for ( int i = 0; i < n; i++ )
{
x += h * f( x, t );
t += h;
}
cout << h << " " << x << "\n";
}
int main()
{
for ( int n = 20; n < 40; n++ )
{
eulers( n );
}
}
希望对您有所帮助。
我是 运行 一个执行常微分方程欧拉近似的程序。选择的步长越小,近似值越准确。我可以使用以下代码让它在设定的步长下工作:
#include <iostream>
using std::cout;
double f (double x, double t)
{
return t*x*x-t;
}
int main()
{
double x=0.0,t=0.0,t1=2.0;
int n=20;
double h = (t1-t) / double(n);
// ----- EULERS METHOD
for (int i=0; i<n; i++)
{
x += h*f(x,t);
t += h;
}
cout << h << " " << x << "\n";
}
所以此代码对 n=20 运行欧拉近似,对应步长 0.1,并输出步长和 x(2) 的近似值。我想让 top 知道如何循环这段代码(对于不同的 n 值),以便它输出这个,然后是越来越小的步长和相应的近似值。 即输出是这样的:
0.1 -0.972125
0.01 -0.964762
0.001 -0.9641
等
所以我在 for 循环中尝试了一个 for 循环,但它给了我一个奇怪的极值输出。
#include <iostream>
using std::cout;
double f (double x, double t)
{
return t*x*x-t;
}
int main()
{
double x=0.0,t=0.0,t1=2.0;
for (int n=20;n<40;n++)
{
double h = (t1-t)/n;
for (int i=0;i<n;i++)
{
x += h*f(x,t);
t += h;
}
cout << h << " " << x << "\n";
}
}
如果我没理解错的话,您想在主函数中针对不同的 n 值执行第一段代码。那么你的问题是变量 x、t 和 t1,它们在循环之前设置一次并且永远不会重置。你想让它们在你的外循环中:
#include <iostream>
using std::cout;
double f( double x, double t )
{
return t * x * x - t;
}
int main()
{
for ( int n = 20; n < 40; n++ )
{
double x = 0.0, t = 0.0, t1 = 2.0;
double h = ( t1 - t ) / n;
for ( int i = 0; i < n; i++ )
{
x += h * f( x, t );
t += h;
}
cout << h << " " << x << "\n";
}
}
为此使用一个函数,使它更清楚:
#include <iostream>
using std::cout;
double f( double x, double t )
{
return t * x * x - t;
}
void eulers( const int n )
{
double x = 0.0, t = 0.0, t1 = 2.0;
double h = ( t1 - t ) / n;
for ( int i = 0; i < n; i++ )
{
x += h * f( x, t );
t += h;
}
cout << h << " " << x << "\n";
}
int main()
{
for ( int n = 20; n < 40; n++ )
{
eulers( n );
}
}
希望对您有所帮助。