質問編集履歴
2
書式の修正
    
        title	
    CHANGED
    
    | 
         @@ -1,1 +1,1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            Pythonの 
     | 
| 
      
 1 
     | 
    
         
            +
            Python 行列表現 for文内のコードエラーが対処できない
         
     | 
    
        body	
    CHANGED
    
    | 
         
            File without changes
         
     | 
1
書式の改善
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -17,30 +17,45 @@ 
     | 
|
| 
       17 
17 
     | 
    
         | 
| 
       18 
18 
     | 
    
         
             
            ### 該当のソースコード
         
     | 
| 
       19 
19 
     | 
    
         | 
| 
       20 
     | 
    
         
            -
            ``` 
     | 
| 
      
 20 
     | 
    
         
            +
            ```
         
     | 
| 
      
 21 
     | 
    
         
            +
            TypeError                                 Traceback (most recent call last)
         
     | 
| 
      
 22 
     | 
    
         
            +
            <ipython-input-14-c07dcd572c24> in <module>
         
     | 
| 
      
 23 
     | 
    
         
            +
                  4   time_t[:, counter] = TIME_STEP*counter
         
     | 
| 
      
 24 
     | 
    
         
            +
                  5   input_u[:, counter-1] = np.matrix(np.zeros((1, 1)))
         
     | 
| 
      
 25 
     | 
    
         
            +
            ----> 6   state_dx = dynamicalsystem(state_x[:, counter-1], input_u[:, counter-1])
         
     | 
| 
      
 26 
     | 
    
         
            +
                  7   state_x[:, counter] = state_x[:, counter-1] + state_dx*TIME_STEP
         
     | 
| 
       21 
27 
     | 
    
         | 
| 
      
 28 
     | 
    
         
            +
            <ipython-input-10-e09b5a52b810> in dynamicalsystem(state, input)
         
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
      
 29 
     | 
    
         
            +
                  1 def dynamicalsystem(state,input):
         
     | 
| 
      
 30 
     | 
    
         
            +
            ----> 2  return np.vstack([ state[1, 0] , AA1*state[1, 0] + AA2*state[2, 0] + AA3*state[3, 0] + CC1*input[0,0] , state[3,0] , BB1*state[1,0] + BB2*state[2,0] + BB3*[3,0] + CC2*input[0,0] ])
         
     | 
| 
      
 31 
     | 
    
         
            +
                  3 
         
     | 
| 
       23 
32 
     | 
    
         | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
      
 33 
     | 
    
         
            +
            TypeError: can't multiply sequence by non-int of type 'float'
         
     | 
| 
       25 
     | 
    
         
            -
            ```
         
     | 
| 
       26 
34 
     | 
    
         | 
| 
       27 
35 
     | 
    
         
             
            ```
         
     | 
| 
      
 36 
     | 
    
         
            +
            ソースコードは下のようになりました. 
         
     | 
| 
      
 37 
     | 
    
         
            +
            コード1
         
     | 
| 
      
 38 
     | 
    
         
            +
            ```
         
     | 
| 
       28 
39 
     | 
    
         
             
            !pip install control
         
     | 
| 
       29 
40 
     | 
    
         
             
            !pip install --upgrade cvxpy
         
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
      
 41 
     | 
    
         
            +
            ```
         
     | 
| 
      
 42 
     | 
    
         
            +
            コード2
         
     | 
| 
      
 43 
     | 
    
         
            +
            ```
         
     | 
| 
       31 
44 
     | 
    
         
             
            %reset -f
         
     | 
| 
       32 
45 
     | 
    
         
             
            import numpy as np
         
     | 
| 
       33 
46 
     | 
    
         
             
            import scipy as sp
         
     | 
| 
       34 
47 
     | 
    
         
             
            import matplotlib.pyplot as plt
         
     | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
      
 48 
     | 
    
         
            +
            ```
         
     | 
| 
      
 49 
     | 
    
         
            +
            コード3
         
     | 
| 
      
 50 
     | 
    
         
            +
            ```
         
     | 
| 
       36 
51 
     | 
    
         
             
            #パラメータ
         
     | 
| 
       37 
52 
     | 
    
         
             
            g=9.81
         
     | 
| 
       38 
53 
     | 
    
         
             
            m=0.0467
         
     | 
| 
       39 
54 
     | 
    
         
             
            R_W=0.066
         
     | 
| 
       40 
     | 
    
         
            -
            J_W=( 
     | 
| 
      
 55 
     | 
    
         
            +
            J_W=(m*R_W*R_W)/2
         
     | 
| 
       41 
56 
     | 
    
         
             
            M=0.806
         
     | 
| 
       42 
57 
     | 
    
         
             
            L=0.75689
         
     | 
| 
       43 
     | 
    
         
            -
            J_theta=( 
     | 
| 
      
 58 
     | 
    
         
            +
            J_theta=(M*L*L)/3
         
     | 
| 
       44 
59 
     | 
    
         
             
            J_m=0.00001
         
     | 
| 
       45 
60 
     | 
    
         
             
            R_m=6.69
         
     | 
| 
       46 
61 
     | 
    
         
             
            K_t=0.317
         
     | 
| 
         @@ -50,62 +65,60 @@ 
     | 
|
| 
       50 
65 
     | 
    
         
             
            theta = 30;
         
     | 
| 
       51 
66 
     | 
    
         
             
            h = 0.001
         
     | 
| 
       52 
67 
     | 
    
         
             
            #線形係数
         
     | 
| 
       53 
     | 
    
         
            -
            A1=-R_m*( 
     | 
| 
      
 68 
     | 
    
         
            +
            A1=-R_m*(M*L*L+J_theta+J_m)/K_t
         
     | 
| 
       54 
     | 
    
         
            -
            A2=-R_m*(-J_m+ 
     | 
| 
      
 69 
     | 
    
         
            +
            A2=-R_m*(-J_m+M*R_W*L)/K_t
         
     | 
| 
       55 
     | 
    
         
            -
            A3=( 
     | 
| 
      
 70 
     | 
    
         
            +
            A3=(R_m*M*g*L)/K_t
         
     | 
| 
       56 
     | 
    
         
            -
            A4=(- 
     | 
| 
      
 71 
     | 
    
         
            +
            A4=(-K_t*K_b-f_m*R_m)/K_t
         
     | 
| 
       57 
     | 
    
         
            -
            A5=K_b+( 
     | 
| 
      
 72 
     | 
    
         
            +
            A5=K_b+(R_m*f_m)/K_t
         
     | 
| 
       58 
     | 
    
         
            -
            B1=R_m*( 
     | 
| 
      
 73 
     | 
    
         
            +
            B1=R_m*(M*R_W*R_W+m*R_W*R_W+J_W+J_m)/K_t
         
     | 
| 
       59 
     | 
    
         
            -
            B2=R_m*( 
     | 
| 
      
 74 
     | 
    
         
            +
            B2=R_m*(M*R_W*L-J_m)/K_t
         
     | 
| 
       60 
     | 
    
         
            -
            B3=( 
     | 
| 
      
 75 
     | 
    
         
            +
            B3=(K_t*K_b+R_m*f_m)/K_t
         
     | 
| 
       61 
     | 
    
         
            -
            B4=(- 
     | 
| 
      
 76 
     | 
    
         
            +
            B4=(-K_t*K_b-R_m*f_m-R_m*f_W)/K_t
         
     | 
| 
       62 
     | 
    
         
            -
            AA1= (A1B4-B2A4)/(B2A2-B1A1)
         
     | 
| 
       63 
     | 
    
         
            -
            AA2 =(-B2A3)/(B2A2-B1A1)
         
     | 
| 
       64 
     | 
    
         
            -
            AA3 =(A1B3-B2A4)/(B2A2-B1A1)
         
     | 
| 
       65 
     | 
    
         
            -
            BB1= (A2B3-B1A5)/(B1A1-B2A2)
         
     | 
| 
       66 
     | 
    
         
            -
            BB2 =(-B1A3)/(B1A1-B2A2)
         
     | 
| 
       67 
     | 
    
         
            -
            BB3 =(A2B3-B1A4)/(B1A1-B2A2)
         
     | 
| 
       68 
     | 
    
         
            -
            CC1 = (B2-A1)/ (B2A2-B1A1)
         
     | 
| 
       69 
     | 
    
         
            -
            CC2 = (B1-A2)/ (B1A1-B2*A2)
         
     | 
| 
       70 
77 
     | 
    
         | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
       73 
     | 
    
         
            -
             
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
             
     | 
| 
       76 
     | 
    
         
            -
             
     | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
             
         
     | 
| 
      
 78 
     | 
    
         
            +
            AA1= (A1*B4-B2*A4)/(B2*A2-B1*A1)
         
     | 
| 
      
 79 
     | 
    
         
            +
            AA2 =(-B2*A3)/(B2*A2-B1*A1)
         
     | 
| 
      
 80 
     | 
    
         
            +
            AA3 =(A1*B3-B2*A4)/(B2*A2-B1*A1)
         
     | 
| 
      
 81 
     | 
    
         
            +
            BB1= (A2*B3-B1*A5)/(B1*A1-B2*A2)
         
     | 
| 
      
 82 
     | 
    
         
            +
            BB2 =(-B1*A3)/(B1*A1-B2*A2)
         
     | 
| 
      
 83 
     | 
    
         
            +
            BB3 =(A2*B3-B1*A4)/(B1*A1-B2*A2)
         
     | 
| 
      
 84 
     | 
    
         
            +
            CC1 = (B2-A1)/ (B2*A2-B1*A1)
         
     | 
| 
      
 85 
     | 
    
         
            +
            CC2 = (B1-A2)/ (B1*A1-B2*A2)
         
     | 
| 
       79 
86 
     | 
    
         | 
| 
      
 87 
     | 
    
         
            +
            ```
         
     | 
| 
      
 88 
     | 
    
         
            +
            コード4
         
     | 
| 
      
 89 
     | 
    
         
            +
            ```
         
     | 
| 
       80 
     | 
    
         
            -
            def  
     | 
| 
      
 90 
     | 
    
         
            +
            def dynamicalsystem(state,input): 
         
     | 
| 
       81 
     | 
    
         
            -
            return np.vstack(  
     | 
| 
      
 91 
     | 
    
         
            +
             return np.vstack([ state[1, 0] , AA1*state[1, 0] + AA2*state[2, 0] + AA3*state[3, 0] + CC1*input[0,0] , state[3,0] , BB1*state[1,0] + BB2*state[2,0] + BB3*[3,0] + CC2*input[0,0] ])  
         
     | 
| 
       82 
92 
     | 
    
         | 
| 
      
 93 
     | 
    
         
            +
            ```
         
     | 
| 
      
 94 
     | 
    
         
            +
            コード5
         
     | 
| 
      
 95 
     | 
    
         
            +
            ```
         
     | 
| 
       83 
96 
     | 
    
         
             
            NUMBER_STEPS = int(10000) #NUMBER_STEPSに整数の10000配列格納
         
     | 
| 
       84 
97 
     | 
    
         
             
            TIME_START = 0.0
         
     | 
| 
       85 
98 
     | 
    
         
             
            TIME_END = 10.0
         
     | 
| 
       86 
99 
     | 
    
         
             
            TIME_STEP = (TIME_END - TIME_START)/float(NUMBER_STEPS)
         
     | 
| 
       87 
100 
     | 
    
         
             
            SIZE_INPUT = int(1)
         
     | 
| 
       88 
101 
     | 
    
         
             
            SIZE_OUTPUT = int(1)
         
     | 
| 
       89 
     | 
    
         
            -
            SIZE_STATE = int(4)
         
     | 
| 
      
 102 
     | 
    
         
            +
            SIZE_STATE = int(4)    
         
     | 
| 
       90 
103 
     | 
    
         
             
            time_t = np.matrix(np.zeros((1, NUMBER_STEPS)))
         
     | 
| 
       91 
104 
     | 
    
         
             
            input_u = np.matrix(np.zeros((SIZE_INPUT, NUMBER_STEPS)))
         
     | 
| 
       92 
     | 
    
         
            -
            output_y = np.matrix(np.zeros((SIZE_OUTPUT, NUMBER_STEPS)))
         
     | 
| 
       93 
105 
     | 
    
         
             
            state_x = np.matrix(np.zeros((SIZE_STATE, NUMBER_STEPS)))
         
     | 
| 
       94 
     | 
    
         
            -
            reference_r = np.matrix(np.zeros((SIZE_OUTPUT, NUMBER_STEPS)))
         
     | 
| 
       95 
     | 
    
         
            -
             
     | 
| 
       96 
     | 
    
         
            -
            state_dx = np.matrix(np.zeros((SIZE_STATE, 1)))
         
     | 
| 
      
 106 
     | 
    
         
            +
            state_dx = np.matrix(np.zeros((SIZE_STATE, 1)))   
         
     | 
| 
       97 
     | 
    
         
            -
             
     | 
| 
       98 
     | 
    
         
            -
            GAIN_K = np.matrix([[1.1, 0.1]])
         
     | 
| 
       99 
     | 
    
         
            -
            STATE_INTTAL = np.matrix(( [2], [0],[-1],[2]))
         
     | 
| 
      
 107 
     | 
    
         
            +
            STATE_INTTAL = np.matrix(( [2], [0],[-1],[2]))     
         
     | 
| 
       100 
     | 
    
         
            -
             
     | 
| 
      
 108 
     | 
    
         
            +
            ```
         
     | 
| 
      
 109 
     | 
    
         
            +
            コード6
         
     | 
| 
      
 110 
     | 
    
         
            +
            ```
         
     | 
| 
      
 111 
     | 
    
         
            +
            from numpy.matrixlib import matrix
         
     | 
| 
       101 
112 
     | 
    
         
             
            state_x[:, 0] = STATE_INTTAL
         
     | 
| 
       102 
     | 
    
         
            -
            input_u[:, 0] = np.matrix(( [0]))
         
     | 
| 
       103 
     | 
    
         
            -
            state_dx = np.matrix(np.zeros((SIZE_STATE, 1)))
         
     | 
| 
       104 
113 
     | 
    
         
             
            for counter in range(1, NUMBER_STEPS):
         
     | 
| 
       105 
     | 
    
         
            -
            time_t[:, counter] =  
     | 
| 
      
 114 
     | 
    
         
            +
              time_t[:, counter] = TIME_STEP*counter
         
     | 
| 
       106 
     | 
    
         
            -
            input_u[:, counter-1] = np.matrix((  
     | 
| 
      
 115 
     | 
    
         
            +
              input_u[:, counter-1] = np.matrix(np.zeros((1, 1)))
         
     | 
| 
       107 
     | 
    
         
            -
            state_dx = dynamicalsystem(state_x[:, counter-1], input_u[:, counter-1])
         
     | 
| 
      
 116 
     | 
    
         
            +
              state_dx = dynamicalsystem(state_x[:, counter-1], input_u[:, counter-1])
         
     | 
| 
       108 
     | 
    
         
            -
            state_x[:, counter] = state_x[:, counter-1] +  
     | 
| 
      
 117 
     | 
    
         
            +
              state_x[:, counter] = state_x[:, counter-1] + state_dx*TIME_STEP
         
     | 
| 
      
 118 
     | 
    
         
            +
            ```
         
     | 
| 
      
 119 
     | 
    
         
            +
            コード7
         
     | 
| 
      
 120 
     | 
    
         
            +
            ```
         
     | 
| 
      
 121 
     | 
    
         
            +
            #plt.plot(np.ravel(time_t), np.ravel(input_u))
         
     | 
| 
       109 
122 
     | 
    
         
             
            plt.plot(np.ravel(time_t), np.ravel(state_x[0, :]))
         
     | 
| 
       110 
123 
     | 
    
         
             
            plt.show()
         
     | 
| 
       111 
124 
     | 
    
         
             
            plt.plot(np.ravel(time_t), np.ravel(state_x[1, :]))
         
     | 
| 
         @@ -116,9 +129,8 @@ 
     | 
|
| 
       116 
129 
     | 
    
         
             
            plt.show()
         
     | 
| 
       117 
130 
     | 
    
         
             
            ```
         
     | 
| 
       118 
131 
     | 
    
         
             
            ### 試したこと
         
     | 
| 
       119 
     | 
    
         
            -
             
     | 
| 
      
 132 
     | 
    
         
            +
            コード6以外はコンパイル通りました.
         
     | 
| 
       120 
133 
     | 
    
         
             
            関数部分の行列がreturn文などで出力できているか確認しました.
         
     | 
| 
       121 
     | 
    
         
            -
            関数部分のコンパイルは通りました.
         
     | 
| 
       122 
134 
     | 
    
         | 
| 
       123 
135 
     | 
    
         | 
| 
       124 
136 
     | 
    
         |