matlab软件(matlab新手入门教程)(1)



Matlab 介绍——Introduction to Matlab

#01本期主题介绍

The topic of this issue

如果您学会使用Matlab,

将有一个科研小助手,做事更轻松!

本期话题将对matlab做一个基础介绍。

Matlab的功能多样,

这里介绍一下Matlab“入门级”的基本功能,

目的是给没有接触过Matlab的朋友学习参考。

所以,请感兴趣的朋友和小编开启学习Matlab的旅程吧!

If you can use Matlab, you will have an additional research assistant.

Do things more easily!

The topic of this issue will do a tweet introduction to matlab.

The functions of Matlab are quite diverse,

Here is an introduction to the basic functions of Matlab "entry level",

The purpose is to learn reference for friends who have not contacted Matlab.

So,

Please open with interested friends and editors

Let's get to know the journey of Matlab!

#02Matlab软件介绍

Matlab software introduction

MATLAB 是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境

MATLAB is a commercial mathematical software produced by MathWorks, a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numerical calculation.

主要用途:

(1)数值和符号计算;
(2)绘图;

(3)工具箱(Toolbox),分为功能性和学科性两种类。

使用技巧:Help

输入help funname (函数名),即可以得到函数的相关帮助信息。

The main purpose:

(1) Numerical value and symbol calculation;

(2) Drawing;

(3) Toolbox (Toolbox) is divided into two categories: functional and disciplinary.

Use skills: Help

Enter help funname (function name) to get help information about the function.

#03Matlab基础界面介绍

Matlab basic interface introduction

1.Matlab的基础界面The basic interface of Matlab

matlab软件(matlab新手入门教程)(2)

1)菜单工具栏

2)文件目录

3)命令窗口:交互式地控制Matlab。比如,简单计算,函数命令测试等。

4)数据区:命令中使用的变量、导入的数据、执行的脚本后的数据都会显示在此处。

5)代码编辑区:编辑代码,按菜单栏“运行”Matlab便可执行。

1) Menu toolbar

2) File directory

3) Command window: Control Matlab interactively. For example, simple calculation, function command test, etc.

4) Data area: The variables used in the command, the imported data, and the data after the executed script will all be displayed here.

5) Code editing area:Edit the code and press "Run" Matlab in the menu bar to execute it.

matlab软件(matlab新手入门教程)(3)

#04Matlab基本使用介绍

Introduction to the basic use of Matlab

1.命令窗口的使用(绘制正弦曲线和余弦曲线)

The use of the command window (drawing sine curve and cosine curve)

操作: 在命令窗口输入

x=[0:0.5:360]*pi/180;plot(x,sin(x),x,cos(x));

Operation: Enter in the command window

x=[0:0.5:360]*pi/180;plot(x,sin(x),x,cos(x));

2.变量命名规则

2. Variable naming rules

1、变量赋值

1. Variable assignment

变量命名:以字母开头,后接字母、数字或下划线的字符序列,最多63个字符;变量名区分字母的大小写。

变量赋值:变量名=表达式

表达式 [自动赋值给变量ANS]

举例:在命令窗口输入

x=1 2i,y=3-sqrt(17)

z=(cos(abs(x y))-sin(78*pi/180))/(x abs(y))

Variable naming: start with a letter, followed by a sequence of letters, numbers or underscores, up to 63 characters; variable names are case sensitive

Variable assignment: variable name = expression

Expression [automatically assigned to variable ANS]

Example: Enter in the command window

x=1 2i,y=3-sqrt(17)

z=(cos(abs(x y))-sin(78*pi/180))/(x abs(y))

matlab软件(matlab新手入门教程)(4)

3.矩阵操作(Matrix operation)

1)矩阵的建立

直接输入法:将矩阵元素用方括号括起来,按矩阵行的顺序输入各元素,同一行各元素之间用空格或逗号分隔不同行的元素之间用分号分隔

2)利用冒号表达式建立一个行向量:e1:e2:e3

其中e1为初始值,e2为步长,e3为终止值;定义时可以不使用中括号、e2,默认步长为1。

1)The establishment of matrix

Direct input method: Enclose the matrix elements in square brackets, and enter the elements in the order of the matrix rows. The elements in the same row are separated by spaces or commas, and the elements in different rows are separated by semicolons.

2) Use the colon expression to create a row vector: e1:e2:e3

Among them, e1 is the initial value, e2 is the step size, and e3 is the end value; the brackets and e2 may not be used in the definition, and the default step size is 1.

matlab软件(matlab新手入门教程)(5)

3)用linspace函数产生行向量

linspace(a,b,n)。其中a和b是生成向量的第一个和最后一个元素,n是元素总数。linspace(a,b,n)与a:(b-a)/(n-1):b等价。具体操作如下图所示:

Use the linspace function to generate a row vector: linspace(a,b,n). Where a and b are the first and last elements of the generated vector, and n is the total number of elements. linspace(a,b,n) is equivalent to a:(b-a)/(n-1):b.The specific operation is shown in the figure below:

matlab软件(matlab新手入门教程)(6)

4)特殊矩阵 Special matrix

空矩阵[ ]:不包含任何元素,阶数为0×0。

产生特殊矩阵的函数:zeros(m,n)[产生零矩阵]。

Empty matrix [ ]: does not contain any elements, and the order is 0×0.

A function to generate a special matrix: zeros(m,n) [generate a zero matrix].

matlab软件(matlab新手入门教程)(7)

ones[产生全1矩阵]、eye[产生单位矩阵]

ones[generate all 1 matrix], eye[generate identity matrix]

matlab软件(matlab新手入门教程)(8)

rand[产生0~1间均匀分布的随机矩阵]、randn[产生均值为0,方差为1的标准正态分布随机矩阵]

rand[Generate a uniformly distributed random matrix between 0 and 1], randn[generate a standard normal distribution random matrix with a mean value of 0 and a variance of 1]

matlab软件(matlab新手入门教程)(9)

魔方矩阵:每行、每列及两条对角线上的元素和都相等;magic(n)生成一个n阶魔方阵。

具体操作如下图所示:

Magic cube matrix: the sum of the elements in each row, column and two diagonals are equal; magic(n) generates an n-order magic cube matrix.

The specific operation is shown in the figure below:

matlab软件(matlab新手入门教程)(10)

5.matlab之线性规划函数

Linprog( )对应的线性规划模型

min z=cx

Ax≤b

x≥0

[x,fval]=linprog(c,A,b,Aeq,beq,lb,ub)

其中,x返回线性规划的最优解(列向量)

fval返回目标函数最优值

c为目标函数系数(行/列向量)

A,b对应不等式约束A·x≤b

Aeq,beq对应等式约束Aeq·X=beq

lb,ub指定x的范围,即lb≤x≤ub,一般地,lb

为零向量,ub=[];b,beq,lb,ub均为列向量

举例:将下列模型转化为linprog格式

Min -z=-3x1-x2-4x3

sub.to 6x1 3x2 5x3≤45

3x1 4x2 5x3≤30

x1,x2,x3≥0

所以

c=[-3;-1;-4]; A=[6,3,5;3,4,5];

b=[45;30]; lb=zeros(3,1);

[x,fval]= linprog(c,A,b,[ ],[ ],lb,[ ])

计算机返回结果:x=(5;0;3),fval=-27

具体操作过程:

The linear programming model corresponding to Linprog( )

min z=cx

Ax≤b

x≥0

[x,fval]=linprog(c,A,b,Aeq,beq,lb,ub)

Among them, x returns the optimal solution of the linear programming (column vector).fval returns the optimal value of the objective function.c is the objective function coefficient (row/column vector).A, b corresponds to the inequality constraint A·x≤b.

Aeq, beq corresponds to the equality constraint Aeq·X=beq.lb,ub specifies the range of x, that is, lb≤x≤ub, generally, lb is a zero vector, ub=[ ]; b, beq, lb, ub are all column vectors

Example: Convert the following model to linprog format

Min -z=-3x1-x2-4x3

sub.to 6x1 3x2 5x3≤45

3x1 4x2 5x3≤30

x1, x2, x3≥0

so

c=[-3;-1;-4]; A=[6,3,5;3,4,5];

b=[45;30]; lb=zeros(3,1);

[x,fval]= linprog(c,A,b,[ ],[ ],lb,[ ])

The computer returns the result: x=(5;0;3), fval=-27

Specific operation process:

matlab软件(matlab新手入门教程)(11)

今天的分享就到这里了。