本课程重点:模型的建立和结果的分析、广浅新
1.matlab入门知识
1.1matlab环境
%:注释
pi%内部是64位双精度
Row = [1,2.2,3,4,5]%行向量
Col = [1;2;3;4;5]%列向量
size(Row)%大小:1行5列
size(Col)
length(Row)%只显示元素个数
M = [1 2;3 4]%矩阵
save variable%保存变量(load+文件名加载)
clear Row %清除变量
clear%全部清楚
1.2数组与函数
%计算抛物线y = 2x^2上30个点的计算
a = linspace(-1,1,30);
b = 2*a.^2
%如何创建组变量
x = 1:2:9
y = 0:-1:-5
m=3;
n=4;
l = linspace(0,pi,11)
z = zeros(m,n)
o = ones(m,n)
e = eye(n)%n阶单位矩阵
r = rand(m,n)%m行n列的均匀分布随机数矩阵
rn = randn(m,n)%m行n列的正态分布随机数矩阵
d = diag(x)%以一维数组x位对角线元素的对角阵
z =
0 0 0 0
0 0 0 0
0 0 0 0
o =
1 1 1 1
1 1 1 1
1 1 1 1
e =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
r =
0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706
rn =
0.7254 -0.2050 1.4090 -1.2075
-0.0631 -0.1241 1.4172 0.7172
0.7147 1.4897 0.6715 1.6302
d =
1 0 0 0 0
0 3 0 0 0
0 0 5 0 0
0 0 0 7 0
0 0 0 0 9
%数组的下标操作
u = [1:3;4:6;7:9]
v = u(:,[1,3])
w = u(2:3,1:2)
u(4,:)=[10:12]
u(:,4)=[1;1;1;1]
u(2:3,:) = []
>> grammer
u =
1 2 3
4 5 6
7 8 9
v =
1 3
4 6
7 9
w =
4 5
7 8
u =
1 2 3
4 5 6
7 8 9
10 11 12
u =
1 2 3 1
4 5 6 1
7 8 9 1
10 11 12 1
u =
1 2 3 1
10 11 12 1
>>
%矩阵的拼接
D = [2*eye(2),zeros(2,1)]
E = [D;eye(2),zeors(2,1)]
>> grammer
D =
2 0 0
0 2 0
E =
2 0 0
0 2 0
1 0 0
0 1 0
>>
%数组的运算
a = [1:3;4:6];
b = [11:13;14:16];
c=2;
A = a.*b
B = a*c
C = a./b
D= a.^c
E = c.^a%[c^a11 c^a12...]
F = a.^b%[a1^b11 a2^b12...]
>> grammer
A =
11 24 39
56 75 96
B =
2 4 6
8 10 12
C =
0.0909 0.1667 0.2308
0.2857 0.3333 0.3750
D =
1 4 9
16 25 36
E =
2 4 8
16 32 64
F =
1.0e+12 *
0.0000 0.0000 0.0000
0.0003 0.0305 2.8211
>>
1.3二维绘图
%二维绘图
x = linspace(0,2*pi,30);
y = [sin(x);cos(x)];
plot(x,y)
plot(x,y,'o')
- hold on(hold off)
%二维绘图
x = linspace(0,2*pi,30);
y = [sin(x);cos(x)];plot(x,y,'k');
hold on %在同一坐标下画多条曲线或离散点
r = (rand(size(x))-0.5)*0.2;
y2 = sin(x)+r;
plot(x,y2,'bo')
hold off
子图:subplot(m,n,p)
%子图subplot(m,n,p)
x=0:0.1:4*pi;
subplot(2,2,1);
plot(x,sin(x));title('y=sin(x)')
subplot(2,2,4);
plot(x,sin(x).*exp(-x/5));title('y=sin(x)exp(x)')
1.4三维绘图
%三维绘图
t=0:pi/50:pi*10;
plot3(sin(t),cos(t),t)
axis([-1.7 1.7 -1.5 1.5 10 60])
grid on%grid off
%三维绘图
%绘制曲面
x=-4:4;
y=x;
[X,Y] = meshgrid(x,y);%生成x-y坐标的”格点“矩阵
Z = X.^2 + Y.^2;
surf(X,Y,Z)
%三维绘图
%绘制曲面
x=-4:4;
y=x;
[X,Y] = meshgrid(x,y);%生成x-y坐标的”格点“矩阵
Z = X.^2 + Y.^2;
surf(X,Y,Z);
hold on
colormap(hot)
stem3(X,Y,Z,'bo')%用来表现在格点上计算函数值
hold off
rotate3d -->3d旋转
%三维绘图
%绘制曲面
x=-4:4;
y=x;
[X,Y] = meshgrid(x,y);%生成x-y坐标的”格点“矩阵
Z = X.^2 + Y.^2;
surf(X,Y,Z);
rotate3d
figure(2)
meshz(X,Y,Z);
rotate3d
hold on
colormap(hot)
stem3(X,Y,Z,'bo')%用来表现在格点上计算函数值
hold off
模型类型
- 优化模型 (最优化模型)
x~决策变量
f(x)~目标变量
gi(x)~约束条件
- 线性规划(LP)
目标和约束均为线性函数
- 0-1规划模型
M a x ∑ n = 1 N a n Max \sum_{n=1}^N a_n Maxn=1∑Nan
- 网络优化模型-最小费用最大流
- 拟合问题
R(t)经验公式
①机理分析
②测试分析(数据建模)
①最小二乘准则
- 线性插值
2.样条插值⭐
线性拟合(线性最小二乘法)
1. f ( x ) = a 1 r 1 ( x ) + . . . + a n r n ( x ) ( r i ( x ) 已 知 , 系 数 待 定 ) f(x) = a_1r_1(x)+...+a_nr_n(x)(r_i(x)已知,系数待定) f(x)=a1r1(x)+...+anrn(x)(ri(x)已知,系数待定)
2.以最小二乘准则确定a1,a2…an —>多元函数求极值(即使n个点(xi,yi)处的yi与f(xi)的差的平方和最小)—>非齐次线性方程组
∂ J ∂ a 1 = 0 ∂ J ∂ a 2 = 0 . . . ∂ J ∂ a n = 0 \frac{\partial J}{\partial a_1} = 0\\ \frac{\partial J}{\partial a_2} = 0\\ .\\ .\\ .\\ \frac{\partial J}{\partial a_n} = 0 ∂a1∂J=0∂a2∂J=0...∂an∂J=0
6
- 非线性规划(NLP)
目标或约束中存在非线性函数
- 二次规划
- 整数规划
Max = 2*x1 + 3*x2 + 4*x3;
1.5*x1 + 3* x2 + 5 * x3 < 600 ;
280*x1 + 250 * x2 +400 *x3 < 60200;
@gin(x1);@gin(x2);@gin(x3);#表示整数
决策变量(全部或部分)为整数
-
多目标规划
-
分式规划、动态规划、双层规划、模糊规划、随机规划