【问题标题】:Why my code produce an incorrect graph in MATLAB?为什么我的代码在 MATLAB 中生成不正确的图形?
【发布时间】:2021-02-20 13:30:31
【问题描述】:

有人知道为什么我的 MATLAB 代码给出的图形不正确吗?

我想绘制 f(x)=exp(-x)-2*x。

代码如下。

clear all;
clc;
h=0.01;
x=-1:h:1;
f=exp(-x)-2*x;
plot(f,x,'color','r');
grid on;
xlabel('x');
ylabel('y');

这段代码给了我一个这样的图。

我们知道 f(0)=1。但是在图中 f(0) 不等于 1。有谁知道为什么我的代码会生成不正确的图?

【问题讨论】:

    标签: matlab graph figure


    【解决方案1】:

    图表上的轴是倒置的。那行说

    plot(f,x,'color','r');
    

    应该是:

    plot(x,f,'color','r');
    

    绘图函数首先需要横坐标 (x),然后是纵坐标 (f)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多