Home>

### i want to see the change of one element of a recurrence formula including a python matrix

When I assembled a recurrence formula including the following matrix with jupyter notebook, the definition of the recurrence formula itself seems to be working, but I took out one element of the matrix and made a graph to see the change of that element. When I try, I get the following error:

Specifically, the initial state is a matrix called add, and it is a recurrence formula that multiplies it by a matrix called UE and continues to add add.
It appears in a 1x2 matrix, but I want to draw a graph of the element (F (t) [0]) in the first row of that element at the tth.

``````add = np.array ([[1,2]]). T
UE = np.array ([[1, -1], [-1, 1]])
def F (t):
if t == 0:
else: else:
return np.dot (UE, F (t-1)) + add
print (F (10))
# [[-1022]
# [1025]]
print (F (10) [0])
# [-1022]
t = np.arange (0, 1000)
plt.plot (t, F (t) [0])``````
``````-------------------------------------------------- -------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-c7b4a9e0b37b>in<module>
---->1 plt.plot (t, F (t) [0])
<ipython-input-40-43c0249f5b44>in F (t)
1 def F (t):
---->2 if t == 0:
Four
5 else:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any () or a.all ()``````

The reason for the error is that the vector value was input to the function F that expects the scalar value to be input, as TakaiY answered.

To achieve what the questioner wants to do, apply numpy.vectorize, which transforms a scalar->scalar function into a vector->vector function. Change the last part as follows.

In addition, since the original function F is a scalar → a vector of two elements, vectorize is applied after making it a scalar → scalar function by shaping it so that only the first element is extracted using the lambda function. I will.

``````t = np.arange (0, 100)
vF0 = np.vectorize (lambda x: F (x) [0])
plt.plot (t, vF0 (t))``````

Also, if t is extended to 1000, repeated nesting will exceed the limit of Python, so I set it to 100. I think this improvement goes beyond the premise of the question of using recurrence formulas, so it is not supported.

``````t = np.arange (0, 1000)