# 数学建模-最小二乘问题

### 目录

• 1.线性最小二乘
• 2.非负线性最小二乘
• 3.有约束线性最小二乘
• 4.非线性最小二乘

## 1.线性最小二乘

V = B X ^ − l X ^ = ( B T P B ) − 1 ( B T P l ) V=B\hat{X}-l\\ \hat{X}=(B^TPB)^{-1}(B^TPl)

## 2.非负线性最小二乘

{ 0.03 x 1 + 0.28 x 2 = 0.85 0.68 x 1 + 0.70 x 2 = 0.17 0.62 x 1 + 0.62 x 2 = 0.07 0.63 x 1 + 0.61 x 2 = 0.84 \begin{cases} 0.03 x_1 + 0.28 x_2 = 0.85 \\ 0.68 x_1 + 0.70 x_2 = 0.17 \\ 0.62 x_1 + 0.62 x_2 = 0.07 \\ 0.63 x_1 + 0.61 x_2 = 0.84 \\ \end{cases}

C = [0.03    0.28
0.68    0.70
0.62    0.62
0.63    0.61];
d = [0.85
0.17
0.07
0.84];
[x,~,~,exitflag,~,~] = lsqnonneg(C,d)


## 3.有约束线性最小二乘

{ 0.03 x 1 + 0.28 x 2 = 0.85 0.68 x 1 + 0.70 x 2 = 0.17 0.62 x 1 + 0.62 x 2 = 0.07 0.63 x 1 + 0.61 x 2 = 0.84 \begin{cases} 0.03 x_1 + 0.28 x_2 = 0.85 \\ 0.68 x_1 + 0.70 x_2 = 0.17 \\ 0.62 x_1 + 0.62 x_2 = 0.07 \\ 0.63 x_1 + 0.61 x_2 = 0.84 \\ \end{cases}

s . t . { 3 x 1 + 2 x 2 ≤ 1 2 x 1 + 3 x 2 ≤ 2 x 1 , x 2 ≤ 2 s.t. \begin{cases} 3x_1+2x_2\le1\\ 2x_1+3x_2\le2\\ x_1,x_2\le2 \end{cases}

C = [0.03    0.28
0.68    0.70
0.62    0.62
0.63    0.61];
d = [0.85
0.17
0.07
0.84];
A = [3,2
2,3];
b = [1;2];
lb = [-Inf,-Inf];
rb = [2,2];
[x,~,~,exitflag,~,~] = lsqlin(C,d,A,b,[],[],lb,rb);


## 4.非线性最小二乘

Y = ∑ i = 0 4 a i s i n ( 2 π X 24 ) + b i c o s ( 2 π X 24 ) Y=\sum_{i=0}^4a_isin(\frac{2\pi X}{24})+b_icos(\frac{2\pi X}{24})

X=rand(100,1);
Y=rand(100,1);

fun=@(a,X)(a(1)*cos(2*pi*X/24)+a(2)*sin(2*pi*X/24)+...
a(3)*cos(4*pi*X/24)+a(4)*sin(4*pi*X/24)+...
a(5)*cos(6*pi*X/24)+a(6)*sin(6*pi*X/24)+...
a(7)*cos(8*pi*X/24)+a(8)*sin(8*pi*X/24))+a(9);
a0=ones(1,9);% 初值
[a,~,~,exitflag,~,~]=lsqcurvefit(fun,a0,X,Y);

t=0:0.2:24;
TEC=fun(a,t);