---
# System prepended metadata

title: Untitled

---

Matlab 作業 E94071071 徐梓修

**作業一**
clear all, close all, clc

m = 1;
M = 5;
L = 2;
g = -10;
d = 1;

s = 1; % pendulum up (s=1)

A = [0 1 0 0;
    0 -d/M -m*g/M 0;
    0 0 0 1;
    0 -s*d/(M*L) -s*(m+M)*g/(M*L) 0];

B = [0; 1/M; 0; s*1/(M*L)];

 
Q = [65 0 0 0;
    0 500 0 0;
    0 0 56 0;
    0 0 0 90];   % 自行設計

R = .0000001;

%%
K = lqr(A,B,Q,R);

[eig_vector,eig_value]=eig(A-B*K)% T=eig_vector e=eig_value
diag_eigval=diag(real(eig_value))
first_eigvector=eig_vector(:,1)


**作業二**
![](https://i.imgur.com/nxHwTdg.png)


**作業三**
**初始角度**

y0 = [-3; 0; pi+.52; 0]

**x距離的最大值**
 x_dis_1=-1;
for a=1:1:length(t)
    x_dis = y(a,1);
    if x_dis_1 > x_dis
        x_dis_1 = x_dis;
    end
end

x_dis_1 =-5.3119

**dx速度最大值**
dx_dis_1=-1;
for b=1:1:length(t)
    dx_dis = y(b,2);
    if dx_dis_1 > dx_dis
        dx_dis_1 = dx_dis;
    end
end

dx_dis_1 =-9.1217

**theta角度總偏離角度與平衡時間**
angle=0;
balance_time=0;
for c=1:1:length(t)
    angle_1=(abs(y(c,3)-(pi)))*(180/pi);
    if angle_1 >1
        balance_time=t(c);
    end
    if angle<angle_1
        angle=angle_1;
    end
end

balance_time =5.0610
angle =29.7938

**dtheta角速度的最大值**
dtheta_dis_1=-1;
for d=1:1:length(t)
    dtheta_dis=y(d,4);
    if dtheta_dis_1>dtheta_dis
       dtheta_dis_1=dtheta_dis;
    end
end

dtheta_dis_1 =-3.9560


