Matlab

Matlab与Linear Algebra

  • det(A)就是求A的行列式,这里要求A为方阵

  • inv(A)求A的逆矩阵

  • A的伴随矩阵就是det(A)*inv(A)

  • B=A’ ,” ‘ “是求矩阵转置的意思

  • rank(A)求A的秩

  • rref(A)把A化到行最简形

  • rrefmovie(A)显示rref每一步操作

    但要加入下面的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function rrefmovie(A,tol)
%RREFMOVIE Movie of the computation of the reduced row echelon form.
% RREFMOVIE(A) produces the reduced row echelon form of A.
% RREFMOVIE, by itself, supplies its own 8-by-6 matrix with rank 4.
% RREFMOVIE(A,tol) uses the given tolerance in the rank tests.

% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 5.12.4.3 $ $Date: 2004/04/10 23:25:44 $

% Sample matrix if none was provided.

commandwindow

Old_Format=get(0,'Format');
if nargin < 1

A = [ 9 4 1 6 12 7
4 0 4 15 1 14
7 0 7 8 10 9
16 0 16 3 13 2
0 2 -4 0 0 0
0 6 -12 0 0 0
9 0 9 6 12 7
5 0 5 10 8 11];
end
format rat
more off
clc
home
disp(' Original matrix')
A
disp('Press any key to continue. . .'), pause
[m,n] = size(A);

% Compute the default tolerance if none was provided.
if (nargin < 2), tol = max([m,n])*eps*norm(A,'inf'); end

% Loop over the entire matrix.
i = 1;
j = 1;
k = 0;
while (i <= m) && (j <= n)
% Find value and index of largest element in the remainder of column j.
[p,k] = max(abs(A(i:m,j))); k = k+i-1;
if (p <= tol)
% The column is negligible, zero it out.
home
disp([' column ' int2str(j) ' is negligible'])
A(i:m,j) = zeros(m-i+1,1)
disp('Press any key to continue. . .'), pause
j = j + 1;
else
if i ~= k
% Swap i-th and k-th rows.
home
disp([' swap rows ' int2str(i) ' and ' int2str(k) blanks(10)])
A([i k],:) = A([k i],:)
disp('Press any key to continue. . .'), pause
end
% Divide the pivot row by the pivot element.
home
disp([' pivot = A(' int2str(i) ',' int2str(j) ')' blanks(10)])
A(i,j:n) = A(i,j:n)/A(i,j)
disp('Press any key to continue. . .'), pause
home
% Subtract multiples of the pivot row from all the other rows.
disp([' eliminate in column ' int2str(j) blanks(10)])
A
disp('Press any key to continue. . .'), pause
for k = 1:m
if k ~= i
home
disp(' ')
A(k,j:n) = A(k,j:n) - A(k,j)*A(i,j:n)
end
end
disp('Press any key to continue. . .'), pause
i = i + 1;
j = j + 1;
end
end
% Restore Format
set(0,'Format',Old_Format)

SEIR模型

save(‘data.mat’,’a’,’b’,’c’) %假如有三个变量’a’, ‘b’, ‘c’,要将它们保存在’data’数据文件中

v = load(‘data.mat’,’a’) %将’data’数据文件中’a’变量读出存到’v’中
画图

subplot(m,n,p)
m,n说明分成几行几列,p说明是第几个(左→右,上↓下)

hold on

Yalmip

  • 介绍

yalmip是由Lofberg开发的一种免费的优化求解工具,其最大特色在于集成许多外部的最优化求解器,形成一种统一的建模求解语言,提供了Matlab的调用API,减少学习者学习成本。

  • 下载 Yalmip 工具箱:

https://yalmip.github.io/
放到toolbox文件夹下
设置路径。选择“添加并包含子文件夹”,将 yalmip 的路径添加进去

  • 测试是否成功:

doc yalmip

yalmiptest

which sdpva

教程

  • 命令

sdpvar:实数变量

intvar:整数变量

binvar:0-1变量

check:可以检查约束条件是否被满足(检查约束条件的余值)

value:可以查看变量或表达式的值

assign: 可以给变量赋值,这个命令调试时很重要

options=sdpsettings(‘solver’,’cplex’); 设置求解方法为调用 CPLEX

optimize(constraints,f,options); Yalmip求解的命令。

不要打赏,只求关注呀QAQ