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).