控制语句&绘制数值曲线
1.控制语句
>> for i=1:10
> v(i)=i*i;
> end;
%循环语句 i 从1到10
>> f= 1:10
>> for i= f;
> disp(i);
> end;
>> i = 1
i = 1
>> while i <=5,
> v(i)= 100;
> i=i+1;
> end
>> v %while循环示例
>> i=1
i = 1
>> while true
> v(i) = 999;
> i = i+1;
> if i == 6,
> break;
> end;
> end;
%break 用法示例
>> if(v(1))== 1
> disp('num is one');
> elseif(v(1)==2);
> disp('num is two');
> else
> disp('num isnot one or two');
> end
num is two
% if else 用法
2.绘制数值曲线
>> plot(t,y1); %绘制曲线
>> hold on %维持前一条曲线的输出;可以在同一图像上绘制多条曲线;% keep previous plot visible
>>hold off % don't overlay any more plots on this figure
>> plot (t,y2,'r'); %r用于修改曲线颜色
>> xlabel ('time');
>> ylabel ('value'); %为x y 轴设置标签
>> legend ('sin','cos') %标记曲线
>> title ('my plot') % 添加标题
>> cd ‘C:\Users\Dell\Desktop ’; print -dpng 'myPlot.png' %修改路径保存,也可以直接保存。
>> help plot
>> figure(1);plot(t,y1);
>> figure(2);plot(t,y2); %在不同的窗口绘制函数 figure(1)在窗口1,
>> subplot(1,2,1); %将绘制区域分成1*2个格子,并且现在使用第一个格子
>> axis([0.5 1 -1 1]) %改变轴刻度,X1-X2 Y1-Y2
>> clf %清除Figuer图像
>> imagesc(A) %可视化矩阵
>> imagesc(A) ,colorbar , colormap gray; %顺序执行 colormap gray 灰度图
>>plot(x, y, 'rx', 'MarkerSize', 10) %绘制点数据10为标记的大小,rx为标记类型
>>plot(X(:,2), X*theta, '-') %绘制直线,点斜式