简体   繁体   中英

Why my code produce an incorrect graph in MATLAB?

Does anyone know why my MATLAB code is give an incorrect graph?

I want to plot f(x)=exp(-x)-2*x.

The codes as follows.

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');

This code give me a figure like this.

在此处输入图像描述

We know f(0)=1. But in the graph f(0) not equal to 1. Does anyone know why my code produce an incorrect graph?

The axis on your graph are inverted. The line that says

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

should be:

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

The plot function expects first the abscissa (x) and then the ordinate (f).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM