Finding solutions to equations in Python and visualising them graphically.

Math
Sponsored Link

By graphing equations, we are trying to remember what on earth we learnt in high school maths.

Graphing data is not that difficult, as the programme will eventually do it for you.

We’ll use the book High School Math Relearning with Python for Humanities Programmers as a textbook instead.

This article is recommended for

Want to know how to graph linear and quadratic equations.
Learn how to graph linear equations.

Describing graphs with Python

A common requirement for plotting graphs using Python is a graph plotting module, in this case, ‘matplotlib’.

First, I used matplotlib to create a graph of the average height of high school students in Yamagata Prefecture by age group.

Run the following code,

import matplotlib.pyplot as plt 
x = [1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,
 1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001, 
2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015, 
2016,2017,2018,2019] 

y=[168.60,168.90,168.80,169.10,169.00,169.50,169.80,169.60,169.80, 
169.70,170.10,169.60,170.40,169.70,170.60,170.20,170.40,170.60, 
170.40,170.90,171.10,171.00,171.00,170.90,170.70,171.00,171.00,171.10, 
170.00,170.50,170.60,170.90,170.40,170.60,170.40,170.30,170.20,170.40, 
170.10,170.60,170.40,170.30,170.30,170.10,170.10] 

plt.plot(x, y) 
plt.grid(color = '0.8') plt.show()

Graphing an equation

Graphing an equation may not ring a bell, but you can probably think of it as visualising a function.

Equations are functions too.  So by graphing them, you can visually see what the equation represents.

So let’s get on with plotting equations.

Drawing a graph of a linear equation

Let’s start by drawing the graph of a linear equation.

The following programme graphs    for the case where x is assigned a value between 1 and 10.

import matplotlib.pyplot as plt # 描画モジュールのインポート 
x = list(range(1, 11)) # xの値 1 ~ 10 

y = [] 
for i in range(10): 
   y.append(3 * x[i] - 24) # y = 3x - 24 

plt.plot(x, y) 
plt.grid(color = '0.8') 
plt.show()

It was a positive linear equation, so the result was as expected.

Now let’s try to write a negative linear equation, but the basic procedure is the same.

import matplotlib.pyplot as plt # 描画モジュールのインポート 
x = list(range(1, 11)) # xの値 1 ~ 10 
y = [] 
for i in range(10): 
   y.append(-5*x[i] + 3) # y = -5x + 3 

plt.plot(x, y) 
plt.grid(color = '0.8') 
plt.show()

The graph of the linear equation is shown below, with the graph falling steadily to the right.

Drawing a graph of a quadratic equation

The next step is to draw the graph of a quadratic equation.

A quadratic equation is ‘an equation that goes up to the square of x’.

Let us draw the quadratic equation for values of x from -10 to 10 in steps of 0.025.

import matplotlib.pyplot as plt # 描画モジュールのインポート 
import numpy as np # 数値計算モジュールのインポート 

x = np.arange(-10,10,0.025) # xの値 0.025刻みで -10 ~ 10 までに設定 

y =x**2 - 3 # y=x**2−3 

plt.plot(x, y) 
plt.grid(color = '0.8') 
plt.show()

For a quadratic equation , the graph looks like this.

Let’s try another one.

Let’s draw a graph of  , with values of x ranging from -10 to 10, in increments of 0.01.

import matplotlib.pyplot as plt # 描画モジュールのインポート 
import numpy as np # 数値計算モジュールのインポート 
x = np.arange(-10,10,0.01) # xの値 -10 ~ 10 0.01刻み。 
y = 2*x**2 + 12*x +13 # y = 2x^2+12x+13 
plt.plot(x, y) 
plt.show()

After running the programme, the following graph was drawn.

Solve the simultaneous equations

The next step is to solve the simultaneous equations to complete the equations.

The first equation we need to find is .

To find a and b that fit this equation, we will substitute the coordinates (1, 1) and (5, 3) into x and y to form a simultaneous equation called

and find its solution.

To find the solution to the simultaneous equation using Python, we require the ‘Symbol’ module of the ‘Sympy’ library.

Use the Symbol module to find the solution by substituting in x and y.

from sympy import Symbol, solve # 記号計算を行うためのライブラリからSymbol をインポート 
# 式を定義する 
a = Symbol('a') 
b = Symbol('b') 

ex1 = a + b -1 
ex2 = 5*a + b -3 

print(solve((ex1, ex2))) 
{a: 1/2, b: 1/2}

Solving the simultaneous equations yields a : 1/2, b: 1/2, i.e. the equation is  .

Now let’s visualise this equation graphically.

import matplotlib.pyplot as plt # 描画モジュールのインポート 
import numpy as np # 数値計算モジュールのインポート 

# データ 
x = np.arange(1, 10, 1) 
y = 0.5 * x + 0.5 # グラフを描画 

plt.plot(x, y) 
plt.grid(color = '0.8') 
plt.show()

The graph below shows the graph when x is substituted from 1 to 10 in increments of 1.

Find x and y in a simultaneous equation

Earlier we used simultaneous equations to complete the equations, but this time we are actually going to solve the simultaneous equations themselves.

The system of simultaneous equations we are to work on is shown below.

Let’s start solving it.

from sympy import Symbol, solve # 記号計算を行うためのライブラリからSymbol をインポート 
# x と y を定義 
x = Symbol('x') 
y = Symbol('y') 

ex1 = 3*x +5*y -14 # 3x + 5y = 14 
ex2 = -3*x + 2*y + 7 # -3x + 2y = -7 

print(solve((ex1, ex2))) 
{x: 3, y: 1} 連立方程式の解

Solving the simultaneous equations was easy, but let’s try to visualise what this means.

import matplotlib.pyplot as plt # 描画モジュールのインポート 
import numpy as np # 数値計算モジュールのインポート 

x = np.arange(-10, 20) # xの値 -10 ~ 20 
y = -3/5*x + 14/5 # y の値を求めるために、3*x +5*y -14 を5で割り両辺をそろえる。 
y2 = 3/2*x - 7/2 # y の値を求めるために、-3*x + 2*y + 7 を2で割り両辺をそろえる。 

# グラフの描画 
plt.plot(x, y) 
plt.plot(x, y2) 
plt.axis('equal') 
plt.grid(color = '0.8') 
plt.show()

Below is a graph, and you can see that the two lines intersect.

The blue line is the negative linear equation and the orange line is the positive linear equation.

The point of intersection of the two lines, i.e. the coordinates of the point of intersection, is the solution part of the simultaneous equation (x: 3, y: 1).

Sponsored Link

コメント

タイトルとURLをコピーしました