利用 CoCalc 網站的 ChatGPT 對話窗來算數學

圖片
CoCalc 網站 提供了線上計算的平台環境,作者致力於發展用電腦做數學運算,開發了 SageMath 這種數學計算程式。 在 CoCalc 網站中,除了提供 SageMath 的計算,也提供了 Python, R 等等計算環境。 登入 CoCalc 網站後,在首頁會有 Extensive ChatGPT Integration 的對話視窗,整合了 ChatGPT 與網站數學運算的功能。 我們也可以利用這個 ChatGPT 視窗,輸入文字提示指令來得到如何做數學計算。 例如輸入: “用 Python 畫出 y = x^2 + 3x + 1 = 0 的函數圖形。” 網站就會生成 Python 程式碼: import numpy as np import matplotlib . pyplot as plt x = np . linspace ( - 10 , 10 , 100 ) # Generate 100 points between -10 and 10 y = x ** 2 + 3 * x + 1 # Calculate y values plt . plot ( x , y ) plt . xlabel ( 'x' ) plt . ylabel ( 'y' ) plt . title ( 'Graph of y = x^2 + 3x + 1' ) plt . grid ( True ) plt . show ( ) SageMath 本身也是一個功能強大的數學計算軟體。我們來使用看看。在 CoCalc 的 ChatGPT 對話窗輸入: “用 SageMath 畫出 y = x^2 + 3x + 1 的函數圖形,並且求 x^2 + 3x + 1 = 0 的解。” 得到 SageMath 的程式碼,並且執行它: # Plot the graph of y = x^2 + 3x + 1 f ( x ) = x ^ 2 + 3 * x + 1 plot ( f , - 5 , 5 , ymin = - 5 , ymax = 20 , color = 'blue' ) # Solve the equati

用 Colab AI 生成程式碼解數學方程式

AI 時代的電腦變得更人性化了。我們只要用適當的文字提示來問 AI 問題,就可以快速地得到程式碼,節省時間,也讓初學者可以更專注在要解決的計算問題上,而不用花太多時間一行一行寫程式。

Google Colab 的筆記本,搭配了 Colab AI 的對話窗。以下我們舉一個例子,說明如何用 Colab AI 自動生成 Python 程式碼。

問題:
求這個一元二次方程式的解: \(x^2 - 5x + 6 = 0\)。

我們可以在 Colab AI 的對話窗裡,輸入以下文字提示:
“用 SymPy 求這個方程式的解:x**2 - 5*x + 6 = 0”

Colab AI 就自動生成了程式碼,用滑鼠點程式碼右上角的小圖示「新增程式碼儲存格」,就會自動把程式碼複製到筆記本中。產生的程式碼如下:

import sympy

# Define the variable
x = sympy.Symbol("x")

# Create the equation
equation = sympy.Eq(x**2 - 5*x + 6, 0)

# Solve the equation
result = sympy.solve(equation, x)

# Print the solutions
print("The solutions are:", result)
The solutions are: [2, 3]

上面的程式用了 sympy 裡的 solve() 函數來解方程式。透過 import sympy 匯入模組,在程式中要使用 sympy 中的函數時,都要在函數前面加上 sympy.,這樣的好處是在程式中清楚指明了某個函數是用哪一個模組。避免同名函數的混淆。例如,在 sympy 模組裡有 sin() 函數,在 math 模組裡也有 sin() 函數。在 sympy 中有圓周率 pi,在 math 中也有圓周率 pi。分別計算如下:

import sympy
import math

print(sympy.sin(pi / 6))
print(math.sin(math.pi / 6))
print(sympy.pi)
print(sympy.pi.n())
print(math.pi)
0.500000000000000
0.49999999999999994
pi
3.14159265358979
3.141592653589793

如果我們要換另一種匯入 sympy 模組的方法,這種方法將 sympy 中所有公開的函數和內建的變數直接引入。可以在 Colab AI 的對話窗輸入以下文字提示:
“程式的開頭請改成以下格式: from sympy import *”

會看到 Colab AI 產生了另一組程式碼:

from sympy import *

# Define the variable
x = Symbol("x")

# Create the equation
equation = Eq(x**2 - 5*x + 6, 0)

# Solve the equation
result = solve(equation, x)

# Print the solutions
print("The solutions are:", result)
The solutions are: [2, 3]

用第二種方式匯入 sympy 模組裡的所有公開函數,在程式中,就可以更簡潔地直接使用函數名稱。然而要注意,在較大的程式中,可能會有和其它模組內的函數同名衝突的問題,程式的維護時,也較容易混淆。

我們把程式稍做修改,可看到用 solve() 函數執行後,直接會輸出方程式的解。

from sympy import *

# Define the variable
x = Symbol("x")

# Create the equation
equation = Eq(x**2 - 5*x + 6, 0)

# Solve the equation
solve(equation, x)
[2, 3]

我們再來試試另外一個例子,求這個一元二次方程式的解: \(3x^2 - 2x + 6 = 0\)。

from sympy import *

# Define the variable
x = Symbol("x")

# Create the equation
equation = Eq(3*x**2 - 2*x + 6, 0)

# Solve the equation
solve(equation, x)
[1/3 - sqrt(17)*I/3, 1/3 + sqrt(17)*I/3]

這裡我們看到用 solve() 得到的答案是用文字輸出,其中 sqrt(17) 代表17的平方根,而 I 是虛數 i

sympy 中另一個函數 solveset() 也可以用來解方程式,得到的答案排版較為美觀。

from sympy import *

# Define the variable
x = Symbol("x")

# Create the equation
equation = Eq(3*x**2 - 2*x + 6, 0)

# Solve the equation
solveset(equation, x)

\(\displaystyle \left\{\frac{1}{3} - \frac{\sqrt{17} i}{3}, \frac{1}{3} + \frac{\sqrt{17} i}{3}\right\}\)

電腦比人腦強的地方在於,它可以處理更複雜的計算,我們來出一題一元三次方程式吧。

求這個一元三次方程式的解:\(x^3 - 2x^2 + 6x + 1 = 0\)

from sympy import *

# Define the variable
x = Symbol("x")

# Create the equation
equation = Eq(x**3 - 2*x**2 + 6*x + 1, 0)

# Solve the equation
solveset(equation, x)

\(\displaystyle \left\{- \frac{\sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}}{3} + \frac{2}{3} + \frac{14}{3 \sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}}, - \frac{7}{3 \sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}} + \frac{2}{3} + \frac{\sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}}{6} + i \left(\frac{7 \sqrt{3}}{3 \sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}} + \frac{\sqrt{3} \sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}}{6}\right), - \frac{7}{3 \sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}} + \frac{2}{3} + \frac{\sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}}{6} + i \left(- \frac{\sqrt{3} \sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}}{6} - \frac{7 \sqrt{3}}{3 \sqrt[3]{\frac{119}{2} + \frac{21 \sqrt{57}}{2}}}\right)\right\}\)

答案很複雜卻也很美麗!這個方程式的解有 3 個,其中 1 個是實數解,另外 2 個是含有虛數的複數解。

如果要求上式數值解,可以輸入:

solveset(equation, x).n()

\(\displaystyle \left\{-0.157720808860374, 1.07886040443019 - 2.27516542234209 i, 1.07886040443019 + 2.27516542234209 i\right\}\)

這裡我們可以看到 Python 解數學方程式的強大能力。

搭配 Colab AI 來協助生成程式碼,是不是非常方便呢。您也可以試試看對 Colab AI 下不同的提示指令。

這個網誌中的熱門文章

在 Colab 筆記本裡呈現更美觀的數學式

利用 CoCalc 網站的 ChatGPT 對話窗來算數學