Matlab笔记(台大郭彦甫14课)

台大郭彦甫老师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=3if 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的阶乘,1e1001*(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));
第3节:变量与档案存取
第4节:初阶绘图
第5节:进阶绘图
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The book consists chapters illustrating a wide range of areas where MATLAB tools are applied. Many interesting problems have been included throughout the book, and its contents will be beneficial for students and professionals in wide areas of interest. These areas include mathematics, physics, chemistry and chemical engineering, mechanical engineering, biological (molecular biology) and medical sciences, communication and control systems, digital signal, image and video processing, system modeling and simulation. Contents 1 Application of GATES and MATLAB for Resolution of Equilibrium, Metastable and Non-Equilibrium Electrolytic Systems 2 From Discrete to Continuous Gene Regulation Models – A Tutorial Using the Odefy Toolbox 3 Systematic Interpretation of High-Throughput Biological Data 4 Hysteresis Voltage Control of DVR Based on Unipolar PWM 5 Modeling & Simulation of Hysteresis Current Controlled Inverters Using MATLAB 6 84 Pulse Converter, Design and Simulations with Matlab 7 Available Transfer Capability Calculation 8 Multiuser Systems Implementations in Fading Environments 9 System-Level Simulations Investigating the System-on-Chip Implementation of 60-GHz Transceivers for Wireless Uncompressed HD Video Communications 10 Low-Noise, Low-Sensitivity Active-RC Allpole Filters Using MATLAB Optimization 11 On Design of CIC Decimators 12 Fractional Delay Digital Filters 13 On Fractional-Order PID Design 14 Design Methodology with System Generator in Simulink of a FHSS Transceiver on FPGA 15 Modeling and Control of Mechanical Systems in Simulink of Matlab 16 Generalized PI Control of Active Vehicle Suspension Systems with MATLAB 17 Control Laws Design and Validation of Autonomous Mobile Robot Off-Road Trajectory Tracking Based on ADAMS and MATLAB Co-Simulation Platform 18 A Virtual Tool for Computer Aided Analysis of Spur Gears with Asymmetric Teeth 19 The Use of Matlab in Advanced Design of Bonded and Welded Joints 20 ISPN: Modeling Stochastic with Input Uncertainties Using an Interval-Based Approach 21 Classifiers of Digital Modulation Based on the Algorithm of Fast Walsh-Hadamard Transform and Karhunen-Loeve Transform 22 Novel Variance Based Spatial Domain Watermarking and Its Comparison with DIMA and DCT Based Watermarking Counterparts 23 Quantitative Analysis of Iodine Thyroid and Gastrointestinal Tract Biokinetic Models Using MATLAB 24 Modelling and Simulation of pH Neutralization Plant Including the Process Instrumentation

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值