我是否在 C++ 中正确实施了 Euler 方法?
Am I implementing Euler's Method correctly in C++?
我被要求写一个 C 或 C++ 程序来解决 the given differential equation
这必须使用欧拉方法在数值上实现。用户应该能够输入速度 (v)、x(0) 的初始值和 program.It 开头的最终时间 (T) 还应该绘制时间 0
我感觉我的程序 运行 没问题,但我在正确实现方程方面的欧拉方法时遇到了问题。这是我创建的程序。任何 feedback/advise 将不胜感激。如果您需要更多信息,请告诉我。
#include <iostream>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
using namespace std;
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <array>
#include "gnuplot.cxx"
int main()
{
//establishing values
double v, x0, T, dt, number_steps;
const int max_number_steps = 100000;
int i=0;
//establishing numverical arrays
double value_t [max_number_steps];
double approx_x [max_number_steps];
//Allow users to input variables
cout<<"Enter Initial Condition"<< endl;
cout<<"Velocity(v) = ";
cin>> v;
cout<<"x0 = ";
cin >> x0;
cout<<"Final time(T) = ";
cin >> T;
cout << "number steps = ";
cin >> number_steps;
//Establishing stepside and assigning arrays
dt = T/number_steps;
value_t[0]= 0.0;
approx_x[0] = x0;
//for loop which should implement Euler's Method
for ( i= 0; i < number_steps; i++)
{
value_t [i+1] = dt*(i+1);
approx_x[i+1] = approx_x[i+1] + dt*v;
}
//Graph the plot via gnuplot
gnuplot_one_function("Velocity", "linespoints", "Time(t)", "Position(x)", value_t, approx_x, number_steps);
return 0;
}
除了与干净代码相关的问题外,您还有一个错误:
approx_x[i+1] = approx_x[i+1] + dt*v;
欧拉法从第x_{i}个元素开始计算第x_{i+1}个元素,微分方程右边乘以步长为:
approx_x[i+1] = approx_x[i] + dt*v; // where approx_x[0] = x_0;
您对欧拉方法的概念存在根本性错误。
my_aprox[i + 1] = my_aprox[i] + dt*v
请记住,要计算新的近似值,您必须“先验”初始值,下一个近似值将是下一个初始值。
我被要求写一个 C 或 C++ 程序来解决 the given differential equation
这必须使用欧拉方法在数值上实现。用户应该能够输入速度 (v)、x(0) 的初始值和 program.It 开头的最终时间 (T) 还应该绘制时间 0 我感觉我的程序 运行 没问题,但我在正确实现方程方面的欧拉方法时遇到了问题。这是我创建的程序。任何 feedback/advise 将不胜感激。如果您需要更多信息,请告诉我。#include <iostream>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
using namespace std;
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <array>
#include "gnuplot.cxx"
int main()
{
//establishing values
double v, x0, T, dt, number_steps;
const int max_number_steps = 100000;
int i=0;
//establishing numverical arrays
double value_t [max_number_steps];
double approx_x [max_number_steps];
//Allow users to input variables
cout<<"Enter Initial Condition"<< endl;
cout<<"Velocity(v) = ";
cin>> v;
cout<<"x0 = ";
cin >> x0;
cout<<"Final time(T) = ";
cin >> T;
cout << "number steps = ";
cin >> number_steps;
//Establishing stepside and assigning arrays
dt = T/number_steps;
value_t[0]= 0.0;
approx_x[0] = x0;
//for loop which should implement Euler's Method
for ( i= 0; i < number_steps; i++)
{
value_t [i+1] = dt*(i+1);
approx_x[i+1] = approx_x[i+1] + dt*v;
}
//Graph the plot via gnuplot
gnuplot_one_function("Velocity", "linespoints", "Time(t)", "Position(x)", value_t, approx_x, number_steps);
return 0;
}
除了与干净代码相关的问题外,您还有一个错误:
approx_x[i+1] = approx_x[i+1] + dt*v;
欧拉法从第x_{i}个元素开始计算第x_{i+1}个元素,微分方程右边乘以步长为:
approx_x[i+1] = approx_x[i] + dt*v; // where approx_x[0] = x_0;
您对欧拉方法的概念存在根本性错误。
my_aprox[i + 1] = my_aprox[i] + dt*v
请记住,要计算新的近似值,您必须“先验”初始值,下一个近似值将是下一个初始值。