How to plot two functions on one graph?

馋奶兔 提交于 2019-12-13 09:09:17

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/13507700/how-to-plot-two-functions-on-one-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!