我对Matlab非常陌生,我试图尝试制作一个简单的迭代脚本.基本上我想做的就是情节:
1*sin(x)
2*sin(x)
3*sin(x)
...
4*sin(x)
Run Code Online (Sandbox Code Playgroud)
这是我写的程序:
function test1
x=1:0.1:10;
for k=1:1:5;
y=k*sin(x);
plot(x,y);
end % /for-loop
end % /test1
Run Code Online (Sandbox Code Playgroud)
但是,它只绘制y = 5*sin(x)或者最后一个数字是......
有任何想法吗?
谢谢!阿米特
您需要使用该命令hold on确保每次绘制新内容时都不会删除绘图.
function test1
figure %# create a figure
hold on %# make sure the plot isn't overwritten
x=1:0.1:10;
%# if you want to use multiple colors
nPlots = 5; %# define n here so that you need to change it only once
color = hsv(nPlots); %# define a colormap
for k=1:nPlots; %# default step size is 1
y=k*sin(x);
plot(x,y,'Color',color(k,:));
end % /for-loop
end % /test1 - not necessary, btw.
Run Code Online (Sandbox Code Playgroud)
编辑
您也可以在没有循环的情况下执行此操作,并根据@Ofri的建议绘制2D数组:
function test1
figure
x = 1:0.1:10;
k = 1:5;
%# create the array to plot using a bit of linear algebra
plotData = sin(x)' * k; %'# every column is one k
plot(x,plotData)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3363 次 |
| 最近记录: |