台大郭彦甫老师Matlab教程
Applications of Matlab in Engineering
https://www.bilibili.com/video/BV1GJ41137UH?p=3&spm_id_from=pageDriver
第1节:Matlab基本操作与矩阵输入
format long,显示小数位数增加。
format short,默认,显示4位小数。
format rat,显示分数,有理数,3/13。
向上的方向键调出前面的指令。
clc:clear command window display
clear:remove all variables in the workspace
who:variables in the workspace
whos:variables information of the workspace
row vector :[1 2 3 4]
column vector :[1;2;3;4]
colon operator:
a=[1:5]
b=[1:2:5]
b=[1:5;2:3:15;-2:0.5:0]
str=‘a’:2:‘z’
A(3,:):第三行全部
A(3,:)=[]:去掉A第三个行的方法
矩阵计算:
特殊矩阵:
eye(2)
zeros(2,4)
ones(2,3)
diag([2 3 4]):对角矩阵,对角元素为2、3、4
矩阵常用函数:
max(A):取每列元素最大值,返回一个行向量。
max(max(A)):取所有元素最大值。
min(A):取每列元素最大值,返回一个行向量。
sum(a):取每列元素的和,返回一个行向量。
sum(sum(a)):取所有元素的和。
mean(A):取每列元素的平均值,返回一个行向量。
mean(mean(A)):取所有元素的平均值。
sort(A):每列元素从小排到大,返回一个与A同维的矩阵。
sortrows(A):按照第一列元素从小到大排每行。
size(A):维数
length(A):元素个数
find(A==5):返回元素5所在的位置。
%%分区块,即分节
运行节
智能缩进:ctrl+I
第2节:结构化程式与自定义函数
f(x)搜索函数,如cos。
代码上一行加2个%(%%),即按照区块显示。鼠标点到一个区块,可以单独执行区块代码,即点击运行节。可以用于debug。
行号上点击,出现灰色圆点,即给一个断点。
逻辑运算符:
== 等于
~= 不等于
&& and
|| or
if condition1
statement1
elseif condition2
statement2
else
statement3
end
a=3;
if rem(a,2)==0 %remainder余数
disp('a is even') %a为偶数
else
disp('a is odd') %a为奇数
end
switch expression
case value1
statement1
case value2
statement
……
otherwise
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;
wihile prod(1:n)<1e100
%prod(1:n)为n的阶乘,1e100为1*(10^100)
n=n+1;
end
Exercise:
n=1;
a=0;
while n<=999
a=a+n;
n=n+1;
end
disp(a)
for variable=start:increment:end
commands
end
for n=1:10
a(n)=2^n;
end
disp(a)
输出:2 4 8 16 32 64 128 256 512 1024
for n=1:2:10
a(n)=2^n;
end
disp(a)
输出: 2 0 8 0 32 0 128 0 512
for n=1:5
a(n)=2^(2*n-1);
end
disp(a)
输出:2 8 32 128 512
变量空间预宣告,则执行速度会更快。
tic(开始位置),toc(结束位置):计时
Exercise:
A=[0 -1 4;9 -14 25;-34 49 64];
B=A;
for i=1:9
if B(i)<0
B(i)=-B(i)
end
end
break: terminates the execution of for or while loops
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+i;
end
Tips
clear all:to remove previous variables
close all:to close all figures
use semicolon; %用分号
use ellipsis… %用换行号
ctrl+C:to terminate the scripts before conclusion
Functions
edit(which('mean.m')) %查看mean函数代码
function y=mean(x) %function:keyword; y:output; x:input; mean:function name
function x = freebody(x0,v0,t)
x = x0+v0.*t+1/2*9.8*t.*t
freebody(0,0,10)
输出:ans = 490
freebody([0 1],[0 1],[10 20])
输出:ans = 490 1981 %一定要用t.*t
Functions with Multiple Inputs and Outputs
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)
华氏度转为摄氏度:
C=(F-32)*5/9
%华氏温度转摄氏温度
%function F2C()
while 1
F=input('Temperature in F:');
if isempty(F)==0
C=(F-32).*5./9;
%X=['Temperature in C = ',num2str(C)];
disp(['Temperature in C = ',num2str(C)])
else
break
end
end
varargin
Function Handles
f = @(x) exp(-2*x);
x = 0:0.1:2;
plot(x,f(x));