LoginSignup
51
47

More than 5 years have passed since last update.

Pythonを使って一瞬で連立方程式を解く

Last updated at Posted at 2014-02-20

連立方程式をSympyを使ってIPython上で解く

・xyz = -234
・x + y + z = 20
・5x – y + 2z = 85

(環境: Ubuntu12.04LTS)

IPythonのインストール

$ sudo apt-get install ipython-notebook

その他の関連する科学技術計算ライブラリもインストール

$ sudo apt-get install python-matplotlib python-scipy python-pandas python-sympy python-nose

IPythonの起動

$ ipython

連立方程式の解答を出力する

IPythonシェルで以下のように入力します。

from sympy import *
x, y, z = symbols('x y z')
init_printing()
solve([x * y * z + 234, x + y + z - 20, 5 * x - y + 2 * z - 85], [x, y, z])

xyz = -234 という数式の場合、 x * y * x + 234 と記述します。

解答出力結果

init_printing()を記述した場合(レイアウトの都合上画像を3分割しています):
sympy2_1.jpg
sympy2_2.jpg
sympy2_3.jpg

init_printing()を記述しなかった場合:

[(-9*17**(1/2)/4 + 39/4, -9*17**(1/2)/4 - 21/4, 31/2 + 9*17**(1/2)/2),
 (13, -2, 9), (9*17**(1/2)/4 + 39/4, -21/4 + 9*17**(1/2)/4, -9*17**(1/2)/2 + 31/2)]

環境構築をしないでオンライン上で計算を行う方法

以下のWebサイトにアクセスします。
・SymPy Live
http://live.sympy.org/

iPythonと同じようにコンソールから、Sympyを使って計算を行うことができます。

51
47
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
47