問題描述
我在繪製向量時遇到語法錯誤 (I'm encountering a syntax error while plotting vectors)
這是我繪製一些向量的代碼:
import numpy as np
import matplotlib.pyplot as plt
epsilon_new = 10e‑16;
a1 = np.array([1,1])
a2 = np.array([‑1,1])
b1 = np.array([1,1])
b2 = np.array([1‑epsilon_new,1])
c1 =4;
c2 = 2; #coefficients solved using basis A
c3 = 6‑(4/epsilon_new);
c4 = 4/epsilon_new ; #coefficients solved using basis B
f = np.array([2,6]) #true f
plt.figure()
plt.plot(np.array([0,f[0]]),np.array([0,f[1]]),label='true f vector');
plt.plot(np.array([0,c1*a1[0]+c2*a2[0]),np.array([0,c1*a1[1]+c2*a2[1]),linestyle='‑‑',label='f vector basis A');
我在最後一行中不斷收到語法錯誤:
plt.plot(np.array([0,c1*a1[0]+c2*a2[0]),np.array([0,c1*a1[1]+c2*a2[1]),linestyle='‑‑',label='f vector basis A');
^
SyntaxError: invalid syntax
所以我想做的是繪製向量 [2 6],然後使用另一組基向量 A 繪製相同的向量,其中 a1 是該 2x2 矩陣的第一列,a2 是該 2x2 矩陣的第二列。我不知道為什麼會出現語法錯誤
參考解法
方法 1:
This line has two missing ]
plt.plot(np.array([0,c1*a1[0]+c2*a2[0]),np.array([0,c1*a1[1]+c2*a2[1]),linestyle='‑‑',label='f vector basis A');
The errors are here:
np.array([0,c1*a1[0]+c2*a2[0]) #missing ]. It should be a2[0]])
and here:
np.array([0,c1*a1[1]+c2*a2[1]) #missing ]. It should be a2[1]])
(by Arnold Schwarzenegger、Joe Ferndz)