# 介面lab01
## 工作日誌
* 7/4學習matlab基本語法
* 7/28完成lab01
## 程式碼
### 1. DataAgent輔助開發狀態空間計算系統
#### ASA
```c
#include "c4mlib.h"
#define F_CPU 11059200UL
int main()
{
C4M_DEVICE_set();
int k=100;
double A[2][2];
double B[2][1];
double C[1][2];
double D[1][1];
double x[100][2][1];
double x_0[2][1];
double u[1][100];
double y[1][100];
double temp1;
double temp2;
double tempx[2][1];
//設定狀態空間方程式的A,B,C,D
printf("Please give A\n");
HMI_snget_matrix(8, 2, 2, &A);
printf("Please give B\n");
HMI_snget_matrix(8, 2, 1, &B);
printf("Please give C\n");
HMI_snget_matrix(8, 1, 2, &C);
printf("Please give D\n");
HMI_snget_matrix(8, 1, 1, &D);
printf("Please give x(0)\n");
HMI_snget_matrix(8, 2, 1, &x_0);
//x(0)置入x陣列
for(int i=0;i<2;i++)
{
x[0][i][0] = x_0[i][0];
}
//取得數列u(k)
printf("Please give u\n");
HMI_snget_matrix(8, 1, 100, &u);
//狀態更新方程式x(k+1)=Ax(k)+Bu(k)
for(int i=0;i<k-1;i++)
{
for(int j=0;j<2;j++)//Ax(k)
{
temp1 += A[0][j]*x[i][j][0];
temp2 += A[1][j]*x[i][j][0];
}
tempx[0][0] = temp1 + B[0][0]*u[0][i];//Ax(k)+Bu(k)
tempx[1][0] = temp2 + B[1][0]*u[0][i];
for(int j=0;j<2;j++)
{
x[i+1][j][0] = tempx[j][0];
tempx[j][0] = 0;
}
temp1 = 0;
temp2 = 0;
}
//狀態更新方程式y(k)=Cx(k)+Du(k)
for(int i=0;i<k;i++)
{
for(int j=0;j<2;j++)
{
temp1 += C[0][j]*x[i][j][0];
}
temp2 = D[0][0]*u[0][i];
y[0][i] = temp1 + temp2;
temp1 = 0;
temp2 = 0;
printf("y[%d] = %.2f\n", i+1, y[0][i]);
}
HMI_snput_matrix(8, 1, 100, &y);
}
```
#### MATLAB:generate_u
```matlab
u = zeros(1,100);
for i = 1:100
u(i) = sin(i);
end
u = cast(u,'single');
save datau u;
```
#### MATLAB:plot
```matlab
clear %清除記憶體內儲存變數
clc %清除command window訊息
close all %清除已開啟的作圖視窗
load datau;
load agent_y.mat;
%繪圖
x = 1:100;
plot(x, u, 'B-x', x, data0, 'R-x');
xlabel('k');
ylabel('values of u(k) and y(k)')
title('u(k) and y(k)');
legend('u(k)=sin(k)','y(k)');
```
### 2. Matlab Remo put get 巨集輔助開發狀態空間計算系統
#### ASA
```c
#include "c4mlib.h"
#define F_CPU 11059200UL
int main()
{
C4M_DEVICE_set();
int k=100;
double A[2][2] = {{0.0, 0.0}, {0.0, 0.0}};
double B[2][1] = {{0.0}, {0.0}};
double C[1][2] = {0.0, 0.0};
double D[1][1] = {0.0};
double x[100][2][1];
double x_0[2][1] = {{0.0}, {0.0}};
double u[1][100];
double y[1][100];
double temp1;
double temp2;
double tempx[2][1];
HMI_snput_matrix(8, 2, 2, &A);
HMI_snput_matrix(8, 2, 1, &B);
HMI_snput_matrix(8, 1, 2, &C);
HMI_snput_matrix(8, 1, 1, &D);
HMI_snput_matrix(8, 2, 1, &x_0);
HMI_snget_matrix(8, 2, 2, &A);
HMI_snget_matrix(8, 2, 1, &B);
HMI_snget_matrix(8, 1, 2, &C);
HMI_snget_matrix(8, 1, 1, &D);
HMI_snget_matrix(8, 2, 1, &x_0);
HMI_snget_matrix(8, 1, 100, &u);
//狀態更新方程式x(k+1)=Ax(k)+Bu(k)
for(int i=0;i<k-1;i++)
{
for(int j=0;j<2;j++)
{
temp1 += A[0][j]*x[i][j][0];
temp2 += A[1][j]*x[i][j][0];
}
tempx[0][0] = temp1 + B[0][0]*u[0][i];
tempx[1][0] = temp2 + B[1][0]*u[0][i];
for(int j=0;j<2;j++)
{
x[i+1][j][0] = tempx[j][0];
tempx[j][0] = 0;
}
temp1 = 0;
temp2 = 0;
}
//狀態更新方程式y(k)=Cx(k)+Du(k)
for(int i=0;i<k;i++)
{
for(int j=0;j<2;j++)
{
temp1 += C[0][j]*x[i][j][0];
}
temp2 = D[0][0]*u[0][i];
y[0][i] = temp1 + temp2;
temp1 = 0;
temp2 = 0;
}
HMI_snput_matrix(8, 1, 100, &y);
}
```
#### MATLAB:
```matlab
clear %清除記憶體內儲存變數
clc %清除command window訊息
close all %清除已開啟的作圖視窗
load datau;
port = remo_open(6);
A = remo_snget_matrix(port);
B = remo_snget_matrix(port);
C = remo_snget_matrix(port);
D = remo_snget_matrix(port);
x_0 = remo_snget_matrix(port);
err = remo_set_timeout(port, 1000);
A = [1.35 0.55;
-0.45 0.35];
B = [0.5;
0.5];
C = [3 1];
D = [1];
x_0 = [0;
0];
A = cast(A,'single');
B = cast(B,'single');
C = cast(C,'single');
D = cast(D,'single');
x_0 = cast(x_0,'single');
remo_snput_matrix(port, A);
remo_snput_matrix(port, B);
remo_snput_matrix(port, C);
remo_snput_matrix(port, D);
remo_snput_matrix(port, x_0);
remo_snput_matrix(port, u);
y = remo_snget_matrix(port);
fprintf('done\n');
%繪圖
x = 1:100;
plot(x, u, 'B-x', x, y, 'R-x');
xlabel('k');
ylabel('values of u(k) and y(k)')
title('u(k) and y(k)');
legend('u(k)=sin(k)','y(k)');
%end
1. remo_close(port);
```
## 流程圖
### 1. DataAgent輔助開發狀態空間計算系統

### 2. Matlab Remo put get 巨集輔助開發狀態空間計算系統

## 實驗數據
### 1. DataAgent輔助開發狀態空間計算系統
1. u=sin(k)
疊代次數k=100
頻率=1/2π



2. u=cos(k)
疊代次數k=100
頻率=1/2π



### 2. Matlab Remo put get 巨集輔助開發狀態空間計算系統
1. u=sin(k)
疊代次數k=100
頻率=1/2π


2. u=cos(k)
疊代次數k=100
頻率=1/2π


## 驗收
### 驗收題目
:::info
實驗一驗收:
調整 A B C D 。
並將 u 改為弦波輸入。(同一組系統,須包含 1Hz, 100Hz, 1000Hz 弦波輸入)
討論不同參數下的響應結果 (需包含至少兩組收斂 兩組發散系統)。
:::
### 驗收成果
#### 討論A、B、C、D影響
0. u=sin(2πwt)
疊代次數k=100

1. 頻率w=1Hz

3. 頻率w=10Hz

2. 頻率w=100Hz

1. u=sin(2πwt)
疊代次數k=100

更改A=
| 1 | 0 |
| -------- | -------- |
| 0 | 1 |
1. 頻率w=1Hz

3. 頻率w=10Hz

2. 頻率w=100Hz

**---------------實驗得證A為對疊代影響矩陣---------------**
2. u=sin(2πwt)
疊代次數k=100

更改B=
| 0 |
| -------- |
| 0 |
1. 頻率w=1Hz
1. 頻率w=10Hz
1. 頻率w=100Hz
**---------------實驗與原數據比對大約差20倍---------------**
3. u=sin(2πwt)
疊代次數k=100

更改C=
| -3 | -1 |
| --- | --- |
1. 頻率w=1Hz
1. 頻率w=10Hz
1. 頻率w=100Hz
**---------------實驗將C改為-1倍,與輸出數據原數據比對也為-1倍---------------**
4. u=sin(2πwt)
疊代次數k=100

更改D=
| 10 |
| -------- |
1. 頻率w=1Hz
1. 頻率w=10Hz
1. 頻率w=100Hz
**---------實驗將D改為10倍,輸出數據與原數據比對振幅分別為原數據加上u的10倍---------**
#### 根據以上結論實作發散系統
5. u=sin(2πwt)
疊代次數k=100
A=
| -1 | -1.5 |
| -------- | -------- |
| 1.1 | 0 |
B=
| 1.5 |
| -------- |
| 0.5 |
C=
| 0 | 1 |
| --- | --- |
D=
| 0 |
| -------- |
X0=
| 0 |
| -------- |
| 0 |
1. 頻率w=1Hz
1. 頻率w=10Hz
1. 頻率w=100Hz
**---------因為此發散系統數據太大,將B乘以0.000000001倍以縮小尺度方便觀察---------**
6. u=sin(2πwt)
疊代次數k=100
A=
| -1 | -1.5 |
| -------- | -------- |
| 1.1 | 0 |
B=
| 0.0000000015 |
| -------- |
| 0.0000000005 |
C=
| 0 | 1 |
| --- | --- |
D=
| 0 |
| -------- |
X0=
| 0 |
| -------- |
| 0 |
1. 頻率w=1Hz
1. 頻率w=10Hz
1. 頻率w=100Hz