Matlab绘制多个函数

时间:2014-05-22 03:36:42

标签: matlab function plot

如果我有一个功能myFunction(x,y,z)喜欢

function myFunction = (x,y)

for n=1:3
    f = x+y^n+1;
    plot(x,f)
end

如何在单独的脚本文件中调用此函数以在一个图形中绘制所有内容?这不起作用:

hold on

myFunction(3,2)
myFunction(1,2)

1 个答案:

答案 0 :(得分:2)

两个可能的答案,

function f = myfunction(x,y,fig)
if nargin<3;
    fig = gcf;
end

isHold = ishold(gca(fig));
hold on
n = 1:3;
f = x+y^.n+1;
plot(x,f,'o') % Or whatever marker you select
if ~isHold
    hold off;
end

如果你只是想要在for循环中绘制点,那么当然一切都会变得更简单。

function f = myfunction(x,y)
figure;
n = 1:3;
f = x+y^.n+1;
plot(x,f,'o'); % Or whatever marker you select.
% plot(x,f); % line plot

然而,最简单的解决方案是另一个。用匿名函数替换该函数。

f = @(x,y,n) x+y^.n+1;
figure;
plot(x,f(x,y,1:3)); % Plot as a line plot
% plot(x,f(x,y,1:3),'o'); % plot as markers