我在繪製向量時遇到語法錯誤 (I'm encountering a syntax error while plotting vectors)


問題描述

我在繪製向量時遇到語法錯誤 (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 SchwarzeneggerJoe Ferndz)

參考文件

  1. I'm encountering a syntax error while plotting vectors (CC BY‑SA 2.5/3.0/4.0)

#syntax-error #Python #Numpy






相關問題

SQL Server 2008 - 嘗試編輯日期 - 語法錯誤? (SQL Server 2008 - Trying to edit dates - Syntax Error?)

將 Jquery.min 升級到 Jquery.1.9 時出錯 (Error Upgrade Jquery.min into Jquery.1.9)

SyntaxError:無效語法(打印功能) (SyntaxError: invalid syntax (print function))

錯誤:“if”之前的預期表達式 (error: expected expression before ‘if’)

導致 C2061 的用戶創建的標頭:語法錯誤:標識符“類名” (User-Created Header Causing C2061: Syntax Error : Identifier 'classname')

If 語句行收到語法錯誤 (Syntax Error received on If statement line)

語法錯誤:到達了意外的文件結尾。你有一個未關閉的#if (Syntax error: Unexpected end of file reached. You have an unclosed #if)

已經放 } 但錯誤仍然說 } 是預期的? (Already put } but the error still says that } is expected?)

為什麼 Java 不允許在這裡使用三元運算符? (Why doesn't Java allow the use of a ternary operator here?)

我在繪製向量時遇到語法錯誤 (I'm encountering a syntax error while plotting vectors)

我想在 Kotlin 中自定義新按鈕時遇到問題 (I have a problem when I want to customize my new button in Kotlin)

為什麼帶註釋的變量不能是全局的? (Why can't an annotated variable be global?)







留言討論