0

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.

enter image description here

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?

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.