Search code examples
matlabplotgraphmatlab-figure

How to plot two functions on one graph?


I've been given the homework to graph the function x^3 and 3^x in one graph.

Does anyone could help me with this exercise please?


Solution

  • every time you call plot matlab will clean the canvas before drawing the new function, unless you are focused on a window where you called hold on, which will substantially tells Matlab to keep the old stuff and superimpose the new drawing.

    x = 0:0.001:10
    
    y1 = x.^3;
    y2 = 3.^x;
    
    plot(x, y1);
    hold on; % without this one will delete y1 before drawing y2
    plot(x, y2, 'r');