MATLAB: Why won’t this plot anything

plot

my code is
f = @(x) x*(exp(x))
for x = -1:.1:1
f(x)
plot(x,f(x), 'k')
hold on
end
I also tried simply typing x=6 y=7 and then plot(x,y) and still nothing comes up. I've rebooted, tried figure(), and I just don't know why my plot comes up as a white graph with no line.

Best Answer

By default, plot() only creates lines when at least two different points are passed in one call (and the points are not infinite and are not nan). plot() by default never adds markers.
If you want to plot one point at a time by putting a marker where the point should be, change the 'k' to 'k*'
If you want to plot a line, then you need to record all of the f(x) values and then plot after the loop.
Hint: if you use x.*exp(x) then you can process an entire vector of values at the same time.
Related Question