数学研发论坛

 找回密码
 欢迎注册
查看: 1521|回复: 8

[求助] Mathematica如何展现画函数曲线的过程?

[复制链接]
发表于 2017-3-16 11:55:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?欢迎注册

x
最近在学Mathematica,特别喜欢它的画函数曲线的功能。但是,Mathematica都是一下就直接把函数图形画出来了,可我喜欢看函数图形动态生成的过程,特别是一些漂亮的参数函数曲线。请教各位高手,有什么办法让Mathematica绘制函数图形时,慢慢地随着自变量的增加绘制出整个函数图形。以前是用qt编程实现,但太麻烦了,图形也不精致。
多谢!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2017-3-16 13:34:55 | 显示全部楼层
用Animate函数就行
  1. Animate[ListLinePlot[
  2.   Table[{(k - 1)*Pi/45, Sin[(k - 1)*Pi/45]}, {k, 1, u}],
  3.   PlotRange -> {{0, 20}, {-1, 1}}], {u, 1, 150},
  4. AnimationRunning -> False]
复制代码
将上面的PlotRange选项替换为PlotRange -> {-1, 1},横坐标轴会自动缩放。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2017-3-17 09:55:42 | 显示全部楼层
Qt的QtCharts  (C++/ QML )画图效果还算比较精致的吧,毕竟是Graphics/View的框架 画出来的,比QWidget强. 但是 相比Mathematica来说, 还是麻烦些, 倒是事实, 谁叫他是 通用级别的C++库呢.  

除了kastin所提及的 Animate,还可以用Manipulate.
  1. Manipulate[Plot[Sin[x + a], {x, -Pi, Pi}], {a, 0, 10, .1}]
复制代码

毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2017-3-17 10:31:19 | 显示全部楼层
稍微改了下 Qt自带的例子Multiaxis chart example, 代码 路径是 /opt/Qt5.8.0/Examples/Qt-5.8/charts/multiaxis/

为了动态的画样条曲线,  我加了一个定时器,每次随机产生一个坐标点,添加到曲线里,并刷新.

截图如下, 应该还算很细腻清新的吧, ,不比Mathematica丑:

123.png


  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Charts module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 or (at your option) any later version
  20. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/

  29. #include <QtWidgets/QApplication>
  30. #include <QtWidgets/QMainWindow>
  31. #include <QtCharts/QChartView>
  32. #include <QtCharts/QLineSeries>
  33. #include <QtCharts/QSplineSeries>
  34. #include <QtCharts/QValueAxis>
  35. #include <QtCharts/QCategoryAxis>
  36. #include <QTimer>
  37. #include <QTime>
  38. QT_CHARTS_USE_NAMESPACE

  39. int main(int argc, char *argv[])
  40. {
  41.     QApplication a(argc, argv);

  42.     //![1]
  43.     QChart *chart = new QChart();
  44.     chart->legend()->hide();
  45.     chart->setTitle("Multiaxis chart example");
  46.     //![1]

  47.     //![2]
  48.     QValueAxis *axisX = new QValueAxis;
  49.     axisX->setTickCount(10);
  50.     chart->addAxis(axisX, Qt::AlignBottom);
  51.     //![2]

  52.     //![3]
  53.     QSplineSeries *series = new QSplineSeries;
  54.     *series << QPointF(1, 5) << QPointF(3.5, 18) << QPointF(4.8, 7.5) << QPointF(10, 2.5);
  55.     chart->addSeries(series);

  56.     QValueAxis *axisY = new QValueAxis;
  57.     axisY->setLinePenColor(series->pen().color());

  58.     chart->addAxis(axisY, Qt::AlignLeft);
  59.     series->attachAxis(axisX);
  60.     series->attachAxis(axisY);
  61.     //![3]

  62.     //![4]
  63.     series = new QSplineSeries;
  64.     *series << QPointF(1, 0.5) << QPointF(1.5, 4.5) << QPointF(2.4, 2.5) << QPointF(4.3, 12.5)
  65.             << QPointF(5.2, 3.5) << QPointF(7.4, 16.5) << QPointF(8.3, 7.5) << QPointF(10, 17);
  66.     chart->addSeries(series);

  67.     QCategoryAxis *axisY3 = new QCategoryAxis;
  68.     axisY3->append("Low", 5);
  69.     axisY3->append("Medium", 12);
  70.     axisY3->append("High", 17);
  71.     axisY3->setLinePenColor(series->pen().color());
  72.     axisY3->setGridLinePen((series->pen()));

  73.     chart->addAxis(axisY3, Qt::AlignRight);
  74.     series->attachAxis(axisX);
  75.     series->attachAxis(axisY3);
  76.     //![4]

  77.     //![5]
  78.     QChartView *chartView = new QChartView(chart);
  79.     chartView->setRenderHint(QPainter::Antialiasing);
  80.     //![5]


  81.     QTimer *timer = new QTimer(chart);
  82.     qsrand(QTime::currentTime().second());

  83.     QObject::connect(timer,&QTimer::timeout,[=]() {
  84.         *series<<QPointF(qrand()%10,qrand()%10);
  85.         chartView->update();
  86.       });

  87.     timer->start(100);

  88.     //![6]
  89.     QMainWindow window;
  90.     window.setCentralWidget(chartView);
  91.     window.resize(800, 600);
  92.     window.show();
  93.     //![6]

  94.     return a.exec();
  95. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2017-3-17 21:19:11 | 显示全部楼层
本帖最后由 happysxyf 于 2017-3-17 21:27 编辑

Mathematica很专业,我只能模仿它,慢慢画,还是基本能满足。只有15KB的绘图器 慢速绘图.rar (15.59 KB, 下载次数: 5)
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2017-3-17 21:50:18 | 显示全部楼层
谢谢各位大神!
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2018-2-12 08:26:24 | 显示全部楼层
需要好好揣摩
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|Archiver|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2018-5-27 21:38 , Processed in 0.155690 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表