简体   繁体   中英

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?

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');

another option

p=ezplot('x^3',[-3,3]); set(p,'Color','red');
hold on; ezplot('3^x',[-3,3]);  title('x^3 and 3^x');

ps. Two ezplot commands are used with a hold on becuase ezplot does not support setting color directly on it in the same call. One must first make the ezplot then set the color afterwords. Also, there is not way to pass more than one color at the same time. Hence if one to use ezplot , I did not see a way to avoid multiple calls.

Sometimes Matlab functions are not all consistent in how they work.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM