MATLAB: Help About Lines in Plots

plotplotting

Hi everyone; I'm trying to write the correct codes of this plot. I wrote this codes but I could not draw the red and green lines.
f=@(x) x.*exp(-x.^2);
x=[-4:0.1:4];
plot(x,f(x));
xmin = fminbnd(@(x) f(x),-4,4);
xmax = fminbnd(@(x) -f(x),-4,4);
How can I draw the red and green lines? Thank you very much

Best Answer

f = @(x) x.*exp(-x.^2);
x = [-4:0.1:4];
AxesH = axes('NextPlot', 'add'); % Same as "hold on"
plot(x,f(x), 'Parent', AxesH);
drawnow;
xmin = fminbnd(@(x) f(x), -4, 4);
xmax = fminbnd(@(x) -f(x), -4, 4);
YRange = get(AxesH, 'YLim');
line([xmin, xmin], [YRange(1), f(xmin)], 'Color', [1,0,0]);
line([xmax, xmax], [YRange(1), f(xmax)], 'Color', [0,1,0]);