台湾国立大学郭彦甫Matlab教程笔记(5)
today:
1.script writing
2.structured programming
3.user-defined funtcion
呼叫程式
1.a file containing a series of matlab commands
2. pretty much like a C/C++ program
3. scripts need to be saved to a.m file before they can be run
copy these to new script
for i=1:10
x=linspace(0,10,101);
plot(x,sin(x+i));
print(gcf,'-deps',strcat('plot',num2str(i),'.ps'));
end
上图是matlab 视窗,exist new script button
select file for save asfile name :[1] not beginnig with numbers [2] 大小写有差别 跟变数很像
运行结果:在不停贴图,跟动画一样
下面是一些常用的东西
【1】演示一下 function 怎么找
find a button like equation, fx
for example to find a function named cos ,不记得怎么拼
then double click it
【2】 想要某一行不执行 注解:符号是 % 一个区块mark起来,选择注解 右键 comment,国语是注解
如果出现两个%%符号,就会变成一个section
运行的时候有两种,一是 run section,second is run.运行节:只会运行一个有阴影的section运行,会运行整个脚本
section%%的作用:程式部分比较多的时候,%%可以帮助我们把程式分块,对debug非常非常有用
下面说一下debug
如何添加break point ,在左边数字边上有横线,点击横线就会加上break point
debug mode可以在 command window 中显示 为k<<
in dubug mode ,鼠标放到 变量上就可以查看变量的值。下图是把鼠标放到了x上面
关于缩进,快捷键
全选, 鼠标右键,smart indent 自动缩排
或者用快捷键,先选中,按ctrl +I即可实现自动缩排的功能
现在开始,写程式的时候用 script editor
script flow 执行顺序
1, typically scripts run from the first line to the last
2 structured programming techniques(subroutine,loop,condition,etc) are applied to make the program look neat
subroutine子程式
loop 循环,回圈
condition,条件
rational operator
operator
<:less than
<= less than or equal
>greater than
>= greater than or equal to
== equal to
~= not equal to
&& and
|| or
= is assignment
使用格式
if condition 1
statement1
elseif condition2
statement2
else
statement3
end
举例子
(余数 remainder)
a=3;
if rem(a,2)==0
disp( 'a is even')
else
disp('a ls odd')
end
1.elseif and else are optional
2.condition 是条件, statement是动作
更精简:
if rem(a,2)==0
disp('a is even')
end
使用格式:
switch expressiom
case value1
statement1
case value2
stamement2
otherweise
statement
end
举例子:
input_num =1;
switch input_num
case -1
disp('negative 1');
case 0
disp('zero');
case 1
disp('positive 1');
otherwise
disp(' other value');
end
使用格式:
while expression
statement
end
举例子:
n=1;
while prod(1:n) < 1e100
n=n+1;
end
这里prod这个function,表示连乘,阶乘factorial
1e100表示 1*10的一百次方
上面程式的功能:求出n!<10^100的n是多少
作业题
我的练习
a=1;
summation=0;
while a<=999
summation=summation+a;
a=a+1;
end
最后在command window 中 键入 summation即可查看所求为多少
使用格式:
for varible =start:increment :end
commands
end
举例子:
for n=1:10
a(n)=2^n;
end
disp(a)
结果:
现在:想要求2的奇数次方
可以这样改,给increment是2
for n=1:2:9
a(n)=2^n;
end
disp(a)
但是注意,之前运行的a的结果没有clear,可能显示一样的结果
所以在运行之前先clear 一下:在command window 输入:clear;clc
下面是正常的运行结果,但是还是发现 偶数位置的数也显示出来了,全是0,这不是我们想要的。
我们想要的是这样的结果:中间没有零
我还能有什么方法呢?上面刚学了这个取余数rem(a,2)==1。不就是奇数吗?这样行不行?
看运行结果,发现还是不行,中间还是有零存在,这不是我们想要的。
那么该如何解决呢?
想使用一个新的数组b过渡一下
现在的写法是这样:用另外一个变量i从1逐步递增,每当n是奇数,就把2^n给a(i),随后i+1
i=1;
for n=1:10
if rem(n,2)==1
b(n)=2^n;
a(i)=b(n);
i=i+1;
end
end
disp(a)
运行结果:得到想要的结果
pre_allocating space to varibles提前分配空间
1. in the previous example ,we do not pre-allocate space to vector a rather than letting matlab resize it on every iteration
一次宣告变数的空间,pre-allocating,这样会比较快
后面提前宣告一个零矩阵 2000*2000,提前分配好空间。这样计算时间很少
左边的程式码每次都需要计算一个新位置,也就是说随着ii和jj的变化,A矩阵是不断变大的,这样计算时间会很多。
tic 和 toc 计时,过了多少时间。
我们在matlab中运行一下,左边的程式码花费的时间
右边pre-allocating的时间
时间之差大概是50倍。
练习作业
我的练习
第一题:
代码:
A=[0 1 4;9 -14 25;-34 49 64];
B=zeros(3,3);
for i=1:9
B(i)=A(i);
end
disp(B)
A=[0 1 4;9 -14 25;-34 49 64];
B=zeros(3,3);
for i=1:9
B(i)=A(i);
if A(i)<0
B(i)=abs(A(i));
end
end
disp(A);
disp(B);
1.terminates the execution of for or while loops
2.used in iteration反复,迭代 where convergence is not guaranteed
一般break和while连接使用
1.at the beginning of your script ,use cammand
clear all to remove previous varibles
close all to close all figures
2.use semicolon ; at the end of commands to inhibit unwanted output不显示在command window
3.use ellipsis … to make scripts more readable换行号
中间加…即可
其实不加ellipsis 也没关系
加了之后效果一样
记一下自己pdf文件的位置
【总结一下】
1.script writing
2.structured programming两块内容学习完成。