.m file before they can be run.m 文件不需要编译 是解释型语言


![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ptsN9kYt-1660124233889)(image/image-20211008163659762.png)]](https://1000bd.com/contentImg/2022/08/13/230637041.png)
MATLAB中的注释符号是 %
如果使用 %% 会将代码标记为 section(区块) 可以将代码分块运行
选中多行后 单击右键 选择 Comment 可注释多行 快捷键为 Ctrl + R
Ctrl + T单击行号 使用断点 breakpoint

关于断点测试
MATLAB 在执行到断点处时, 会暂停执行, 可以对前面已执行部分的变量进行检查

选中需要缩进的代码 点击右键 选择 smart indent 可以自动添加缩进

快捷键 : ctrl + I
使用循环结构, 条件结构 等 使程序更加简洁
if,elseif,else 条件为真时执行代码块for 多次执行代码块switch,case,otherwise 分支语句, otherwise 相当于 defaulttry,catch 异常处理while 条件为真时重复执行代码块break 终止当前(最近的)循环continue 跳过当前循环end 终止代码块 或 用于标明数组的最后一个下标pause 暂停执行return 返回到调用函数if elseif elseif condition1
statement1
elseif condition2
statement2
else
statement3
end
elseif和else是可选的
% 判断奇偶数
a = 3;
if rem(a,2) == 0
disp('a is even')
else
disp('a is odd')
end
rem(para1, para2)函数,用于求余数 余数(remainder)
第一个参数是除数, 第二个参数是被除数rem(para1, para2)相当于para1 % para2, 即para1对para2求模
switchswitch expression
case value1
statement1
case value2
statement2
.
.
.
otherwise
statement
end
将
expression与value进行逻辑比较, 如果结果为真, 则执行该分支下的语句当
otherwise分支存在时
会在所有value都与expression不匹配时, 执行otherwise分支下的语句相当于编程语言中的
default❗️注意 与编程语言不同的是
MATLAB中的switch语句不会出现case穿透现象, 不需要加break
input_num = 1;
switch input_num
case -1
disp('negative 1');
case 0
disp('zere');
case 1
disp('positive 1');
whilewhile expression
statement
end
当
expression成立的时候, 执行statement, 直到expression不成立
n = 1;
while prod(1:n) < 1e100
n = n + 1;
end
prod()表示乘积 (product)
1:n表示从1到n的向量
prod(1:n)表示n!阶乘
e100表示 1 0 100 10^{100} 10100
1e100表示 1 × 1 0 100 1\times 10^{100} 1×10100
forfor variable=start:increment:ending
commands
end
start是起始值,ending是结束值
increment是步长
1:10= [1,2,3,4,…,10]
1:2:10= [1,3,5,7,9]不指定步长时, 默认步长为 1
for n=1:10
a(n)=2^n;
end
disp(a)
上式的
a是一个数组
Pre-allocating Space to Variables
看下面两个示例程序
tic
for ii = 1:2000
for jj = 1:2000
A(ii,jj) = ii + jj;
end
end
toc
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
第一个程序没有预分配空间, 在对A矩阵进行赋值时, 会不断对A进行扩容
第二个程序对A预分配空间, 不需要扩容, 效率更高
tic开始计时
toc结束计时可以得到运行代码块的时间
| Operator | Meaning |
|---|---|
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
== | Equal to |
~= | Not equal to |
&& | And |
| ` | ` |
以上运算符的运算结果均是
boolean类型
在脚本的开始处
clear all 先前所有的变量close all 关闭所有的图形在语句的结尾添加分号 ; 避免不需要的输出
使用换行号 ... 增加程序的可读性
A = [1 2 3 4 5 6; ...
6 5 4 3 2 1];
遇到错误时(如死循环), 使用ctrl + c终止程序的运行
MATLAB命令的 .m 文件edit(which('mean.m'))
mean是计算平均值的函数使用:
a[10 16 27 64];
mean(a)可计算数组a的平均值

User Define Function
function x = freebody(x0,vO,t)
% calculation of free falling
% 0: initial displacement in m
% vO: initial velocity in m/sec
% t: the elapsed time in sec
% x: the depth of falling in m
X = ×0 + v0.*t + 1/2*9.8*t.*t;
*是数字之间直接相乘
如果输入向量, 会将两个向量直接相乘
.*是元素之间相乘
如果输入向量, 是将向量的对应元素相乘
质点的加速度和受力如下:
a
=
v
2
−
v
1
t
2
−
t
1
F
=
m
a
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)
input,isempty,break,disp,num2str在没有给出对应参数时, 函数应该使用默认参数进行运算
实现这样的功能, 会用到以下默认变量
inputname Variable name of function input 参数名mfilename File name of currently running function 当前函数的文件名nargin Number of function input arguments 参数数量nargout Number of function output arguments 返回值数量varargin Variable length input argument list 参数长度(向量参数)varargout Variable length output argument list 返回值长度(向量返回值)function [volume] = pillar(Do, Di, height)
if nargin==2, % 如果输入的参数只有两个(没有输入 height)
height=1; % 默认 height 设为 1
end
volume=abs(Do.*2-Di.^2).*height*pi/4;
创建匿名函数的方法
一个曲线的表达式没必要创建一个 .m 文件来储存
f = @(x) exp(-2*x); %此行为 Function Handle
x = 0:0.1:2;
plot(x,f(x));
可以认为 f 是指向后面表达式 exp(-2*x) 的一个指针