1. Script writing
1.1 MATLAB 脚本
- .m 文件;
- 包含了一系列MATLAB命令的文件;
- 应用结构化编程技术(子程序,循环,条件等)使程序看起来很整洁;
2. Structured programming
2.1 Flow Control
if, elseif, else
for
switch, case
otherwise
try, catch
while
break
continue
end
pause
return
2.2 关系(逻辑)运算符
Operator | Meaning |
---|---|
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
== | 等于 |
~= | 不等于 |
&& | 与 |
| | | 或 |
2.2.1 if elseif else
if condition1
statement1
elseif condition2
statement2
else
statement3
end
EX:
>> a=3
a =
3
>> if rem(a, 2) == 0
disp('a is even')
else
disp('a is odd')
end
a is odd
2.2.2 switch
switch expression
case value1
statement1
case value2
statement2
.
.
otherwise
statement
end
EX:
>> input_num = 1
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
positive 1
2.2.3 while
while expression
statement
end
EX:
>> n = 1;
while prod(1:n) < 1e100
n = n + 1;
end
>> n
n =
70
练习:
- 计算 1+2+3+…+999
>> a = 1;
sum = 0;
while a < 1000
sum = sum + a;
a = a + 1;
end
>> sum
sum =
499500
2.2.4 for
for variable=start : increment : end
commands
end
EX:
>> for n=1:10
a(n)=2^n;
end
disp(a)
1 至 7 列
2 4 8 16 32 64 128
8 至 10 列
256 512 1024
2.2.5 预分配空间 Pre-allocating Space to Variables
- A 和 B 那种方法运行时间快?
A:
tic
for ii = 1:2000
for jj = 1:2000
A(ii,jj) = ii + jj;
end
end
toc
B:
tic
A = zeros(2000, 2000);
for ii = 1:size(A,1)
for jj = 1:size(A,2)
A(ii,jj) = ii + jj;
end
end
toc
EX:
- Use structured programming to:
- Find the entries in matrix A that are negative
- Store these entries’ position in a matrix B
- Change the values of these entries to zero
A = [ 0 − 1 4 9 − 14 25 − 34 49 64 ] A= \left[ \begin{matrix} 0 & -1 & 4 \\ 9 & -14 & 25 \\ -34 & 49 & 64 \end{matrix} \right] A=⎣⎡09−34−1−144942564⎦⎤
>> A = [0, -1, 4; 9, -14, 25; -34, 49, 64]
for n = 1:3
for m = 1:3
if A(n,m) < 0
A(n,m) = 0;
end
end
end
B = A
A =
0 -1 4
9 -14 25
-34 49 64
B =
0 0 4
9 0 25
0 49 64
2.2.6 break
- 终止for或while循环的执行
x = 2; k = 0; error = inf;
error_threshold = 1e-32;
while error > error_threshold
if k > 100
break
end
x = x - sin(x)/cos(x);
error = abs(x - pi);
k = k + 1;
end
2.2.7 Tips for Script Writing
clear all
%清除所有的变量close all
%关闭所有的图形clc
;
···
% 省略号,换行- ctrl+c %终止正在运行的程序
3. User-defined function
3.1 MATLAB内置函数的内容
>> edit(which('mean.m'))
3.2 User-Define Functions
- 写一个函数,计算给定初始位移
x
0
x_0
x0,初始速度
v
0
v_0
v0和降落时间
t
t
t的自由落体运动的位移
x
x
x:
x = x 0 + v 0 t + 1 2 g t 2 x=x_0+v_0t+\frac{1}{2}gt^2 x=x0+v0t+21gt2
% 新建脚本 freebody.m
function x = freebody(x0,v0,t)
% calculation of free falling
% x0: initial displacement in m
% v0: initial velocity in m/sec
% t: the elapsed time in sec
% x: the depth of falling in m
x = x0 + v0.*t + 1/2*9.8*t.*t;
运行函数:
>> freebody(1,1,2)
ans =
22.6000
3.2.1 具有多个输入和输出的函数
- 求粒子的加速度和作用于它的力
a = v 2 − v 1 t 2 − t 1 F = m a a=\frac{v_2-v_1}{t_2-t_1} F=ma a=t2−t1v2−v1 F=ma
function [a F] = acc(v2,v1,t2,t1,m)
a = (v2-v1)./(t2-t1);
F = m.*a;
运行函数:
>> [Acc Force] = acc(20,10,5,4,1)
Acc =
10
Force =
10
3.2.2 练习
- 将华氏摄氏度转为摄氏度;
- 脚本应该继续运行,直到没有提供转换的数字;
function F2C
while(1)
F = input('Temperature in F:');
a = isempty(F);
if a == 0
C=(F-32)*5/9;
disp(['==> Temperature in C = ',num2str(C)])
else
break
end
end
运行函数:
>> F2C
Temperature in F:43
==> Temperature in C = 6.1111
Temperature in F:32
==> Temperature in C = 0
3.2.3 函数默认变量
inputname
mfilename
nargin
nargout
varargin
varargout
EX:
function [volume]=pillar(Do,Di,height)
if nargin==2,
height=1;
end
volume=abs(Do.^2-Di.^2).*height*pi/4;
运行函数:
>> pillar(1,2)
ans =
2.3562
>> pillar(1,2,3)
ans =
7.0686
3.2.4 Function Handles
- 一种创建匿名函数的方法,即不必在.m文件中定义一行表达式函数;
f = @(x) exp(-2*x);
x = 0:0.1:2;
plot(x, f(x));