[Cheatsheet] Introduction to MATLAB

本文全面介绍了MATLAB的基础指令、变量类型、文件操作、函数定义、绘图、微积分等核心功能,涵盖数学计算、数据处理、算法实现等多个方面,是初学者和进阶用户的实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 基础指令

项目代码
运算符号+ - * / \ ^ .* ./ .\ .^ .’ sqrt() exp() log() log10()
帮助指令help xxx
变量赋值符号=
清除指令clearclc
设置格式format short, format long, format longE, format shortE, format rat, format hex, format loose, format compact

2. 变量

2.1 类型

logical, character, numeric, table, cell, structure, function handle

2.2 数字类(numeric)变量

2.2.1 类型

intuintsingledouble(默认类型)

2.2.2 array(double类)

  1. vector, matrix: 使用[ ]构造与索引
  2. 常用指令:
    sort(), sortrows(), max(), min(), mean(), sum(), length(), size(), find(Array,element), eye(n), zeroes(m,n), ones(m,n), diag([x1,x2,x3,...,xn]), rand(n), linspace()

2.3 字符类(character)变量

  1. 常用指令:char()

2.4 逻辑类(logical)变量

  1. 类型:
    0(ture), 1(flase)
  2. 常用运算符号:
    ==>, <, >=, <=, ~=, and, &, &&, or, |, ||, not
  3. 常用条件语句:
    if...elseif...else...end, for...end, parfor, switch...case...otherwise, try...catch, while...endmbreak, continue, end, pause, return

2.5 结构类(structure)变量

  1. 展示:
  2. 基础语句:
    student(1).name=’John Smith’
    student(1).ID=’115010415’
    student(2).name=’Amy Chang’
  3. 常用条件语句: struct(), fieldnames(), getfield(), isfield(), isstruct(), rmfield(), setfield(), table2struct(), struct2table(), cell2struct(), struct2cell()

2.6 单元格(cell)变量

  1. 展示:
    在这里插入图片描述
  2. 基础语句:
    A(x1,y1)= { } 或者 A{x1,y1}=xxx
  3. 常用语句:
    cell(), cell2mat(), cell2struct(), cell2table(), iscell(), mat2cell(), num2cell(), struct2cell(), table2cell()

2.7 function handler

基础语句:f=@(x,y,z) (x.*sin(y)+z.*cos(x))

3. 读写文件

3.1 .m类Matlab文件

基础语句:save(filename,variables,format)

3.2 .csv.xlsx类Excel文件

  1. 读取语句:
    xlsread(‘xxx.csv’), xlsread(‘xxx.xlsx’), readtable(‘xxx.csv’)
  2. 写入语句:
    xlswrite(‘name.xlsx’,variablename,sheet number,’position’)

3.3 .txt类TXT文件

  1. 读取语句:fopen(‘filename’,’permission’), 读取方式可以为’r’, ’w’, ’a’
  2. 写入语句: fwrite(), fprintf()
  3. 其他语句: fclose(), fgets(), fileread(), fread(), fseek(), fscanf()

4. 函数:

  1. 基础语句
function [y1,y2,...,yn] = function_name(x1,x2,...,xm) 
...... 
end
% x为输入参数,y为输出参数
  1. 常用变量:nargin, nargout, varargin, varargout, inputname

5. 绘图

  1. 图像绘制语句
    plot(x,y,’style’), subplot(m rows,n columns, sequence of the plot), hist(y,bin width), bar(), bar3(), polar(theta,rho), plot3(), surf(), contour(), meshgrid()
  2. 图像参数语句
    hold on, hold off , legend(), title(), xlabel(), ylabel(), text(x,y,’text’,’Interpreter’,’latex’), annotation(‘arrow’,’X’,[x1,x2],’Y’,[y1,y2]), gca, gcf, xlim(), ylim(), axis on/off, grid on/off, box on/off, axis square/equal/equal tight/image/ij/xy

6. 微积分

6.1 多项式

  1. 构造:
    polyval(polynomial coefficient,independent variable vector space)
  2. 微分:
    polyder(coefficient array), polyder(coefficient array, x)
  3. 积分:
    polyint(coefficient array, value of C)

6.2 数值解

  1. 微分:diff(y)./diff(x)
  2. 积分:Trapezoid Rule: trapez(), integral(y,start x, end x)

7. 求根与微分方程

  1. 构造:
    syms x y z, 构造出的x,y,z无实值,但可以进行基础运算
  2. solve(equation, independent variable, requirements, value), solve(equation1, euqation2, x, y), solve(equations, ‘MaxDegree’,1/2/3)
  3. fsolve(), fzero(), roots()
  4. 微分方程:
    ode45(),
ode=function==number
cond=y(x_0 )==number 
dsolve(ode,cond)

8. 线性方程与线性系统

  1. Reduced row echelon form: rref([A b])
  2. 解矩阵: inv(A)*b, A\b
  3. determinant: det(A)
  4. Cramer’s rule: x i = d e t ( A i ) d e t ( A ) x_i=\frac{det(A_i )}{det(A)} xi=det(A)det(Ai)
  5. 矩阵性质:cond(A), rank(A)
  6. Matrix exponential: expm()
  7. LU decomposition: [L U P]=lu(A)
  8. Orthogonal-triangular decomposition decomposition: gr()
  9. LDL factorization fro Hermitian matrices: ldl()
  10. Singular value decomposition:svd(), gsvd()
  11. Eigen decomposition: [v, d] = eig(A)

9. 统计,回归,内插

  1. 基础指令:mean(), median(), mode(), var(), sd(), range(), iqr(), quantile(data,[p1,p2,p3,...]), boxplot(), tabulate()
  2. 拟合:polyfit(x,y, power)
  3. 线性回归:regress(y,[x1,x2])
  4. 非线性回归:fitnlm(tabulate,modelfunction,initial value)
  5. 著名工具集:cftool()
  6. 内插: interpl(), spline(), pchip(), interp2()

10. 参考资料

https://www.mathworks.com
https://mp.weixin.qq.com/s/WJ-hpkN8aNd_zYOP4i_efA

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值