我使用Qt框架创建了一个应用程序,并使用QPainter
绘制了一些图表。
我可以绘制exp函数,我想绘制y'=y
微分方程(欧拉方法)的数值解,即exp(x)
,看起来它们是正确绘制的,但是当我尝试调整窗口大小,exp函数保持不变,但近似值因某种原因消失了。
可能是什么问题?
(我知道代码非常难看,因为现在我把所有内容放在标题中)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QPointF>
#include <math.h>
#include <QWidget>
#include <QPainter>
#include <QVector>
#include <cmath>
#include <QImage>
#include <Qt>
#include <QPen>
class Plot : public QWidget
{
public:
Plot() {}
void init()
{
QPainter painter(this);
QPen my_pen;
my_pen.setStyle(Qt::SolidLine);
my_pen.setWidth(5);
my_pen.setBrush(Qt::blue);
my_pen.setColor(Qt::blue);
my_pen.setCapStyle(Qt::RoundCap);
my_pen.setJoinStyle(Qt::RoundJoin);
painter.setPen(my_pen);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width()/2.0f,height()/2.0f);
painter.scale(1/10.0f,-1/10.0f);
//DRAWING THE COORDINATE SYSTEM
painter.drawLine(QPointF(-m_width/2.0f,0.0f),QPointF(m_width/2.0f,0.0f));
painter.drawLine(QPointF(0.0f,-m_height/2.0f),QPointF(0.0f,m_height/2.0f));
//SPLITTING Y AND X ASIS TO UNITS
for(int i = -20/2.0f; i <= 20/2.0f; ++i)
{
painter.drawLine(QPointF(i*m_unit,-10),QPointF(i*m_unit,10));
}
for(int i = -20/2.0f; i <= 20/2.0f; ++i)
{
painter.drawLine(QPointF(-10,i*m_unit),QPointF(10,i*m_unit));
}
//DRAWING EXP(X) FUNCTION
for(int i = -m_number_of_points/2; i < m_number_of_points/2; ++i)
{
m_point_array[i+m_number_of_points/2] = QPointF(i,m_unit*exp(i/m_unit));
}
for(int i = 0; i < m_number_of_points; ++i)
{
painter.drawPoint(m_point_array[i]);
}
//DRAWING THE NUMERICAL APPROXIMATION OF THE SOLUTION OF y'=y DIFF. EQ, WHICH IS exp(x)
for(int i = 0; i < m_number_of_basepoints; ++i)
{
m_approx_point_array[i] = QPointF(m_unit*((i+1)*m_step+m_first_x_coord),m_unit*(m_initial_condition + m_step*derived_func(m_initial_condition)));
m_initial_condition = m_initial_condition + m_step*derived_func(m_initial_condition);
}
my_pen.setColor(Qt::red);
painter.setPen(my_pen);
painter.drawLine(QPointF(m_unit*m_first_x_coord,m_unit*m_xxx),QPointF(m_approx_point_array[0]));
for(int i = 0; i < m_number_of_basepoints-1; ++i)
{
painter.drawLine(m_approx_point_array[i],m_approx_point_array[i+1]);
}
}
void paintEvent(QPaintEvent*)
{
init();
}
float derived_func(float param)
{
return param;
}
//INITIALIZING THE VALUES FOR COORDINATE SYSTEM
float m_ratio_x = 1/10.0f;
float m_ratio_y = -1/10.0f;
float m_width = width() * (1.0f/m_ratio_x);
float m_height = height() * (1.0f/m_ratio_y);
float m_unit = 300.0f;
//INITIALIZING THE VALUES FOR DRAWING exp(x)
int m_number_of_points = m_width;
QPointF* m_point_array = new QPointF[m_number_of_points];
//INITIALIZING THE VALUES FOR APPROXIMATION
int m_number_of_basepoints = 1000;
QPointF* m_approx_point_array = new QPointF[m_number_of_basepoints];
float m_first_x_coord = -5.0f;
float m_initial_condition = 1/exp(5);
float m_last_x_coord = 2.0f;
float m_step = (m_last_x_coord - m_first_x_coord)/m_number_of_basepoints;
float m_xxx = m_initial_condition;
};
#endif
答案 0 :(得分:0)
无法确切知道但我认为很可能是由于m_initial_condition
变量在每次计算时被修改而引起的。作为诊断此类问题的好方法,我建议将const
添加到不应实际更改的变量中。