# Python to Solve Linear Equation and return 3D Graph with Full Source Code For Beginners

Enter values through spaces for each of the three equations, return the solution and a 3D graph.

• Numpy
• Matplotlib

## Install Prerequisites:

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```pip install numpy
pip install matplotlib``````

## Run the Script:

``python3 linearEquations.py`Code language: CSS (css)`

## Source Code:

linearEquations.py

``````import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

print('The 3 equations are entered individually, each value of the equation is entered separated by a space, for example: \ninput = 6 5 -3 4 \nThis will be equal to 6x + 5y- 3z = 4')

print('Enter values for equation 1: ')
a, b, c, d = map(float, input().split())

print('Enter values for equation 2: ')
e, f, g, h = map(float, input().split())

print('Enter values for equation 3: ')
i, j, k, l = map(float, input().split())
#solve the linear equation
A = np.array([[a,b,c],[e,f,g],[i,j,k]])
b_a = np.array([d,h,l])
sol = np.linalg.solve(A,b_a)
print(sol)

x,y = np.linspace(0,10,10), np.linspace(0,10,10)
X,Y = np.meshgrid(x,y)

Z1 = (d-a*X-b*Y)/c
Z2 =(h-e*X-f*Y)/g
Z3 =(l+X-Y)/k

#create 3d graphics

fig = plt.figure()
ax.plot_surface(X,Y,Z1,alpha=0.5,cmap=cm.Accent,rstride=100,cstride=100)
ax.plot_surface(X,Y,Z2,alpha=0.5,cmap=cm.Paired,rstride=100,cstride=100)
ax.plot_surface(X,Y,Z3,alpha=0.5,cmap=cm.Pastel1,rstride=100,cstride=100)
ax.plot((sol[0],),(sol[1],),(sol[2],),lw=2,c='k', marker='o', markersize=7, markeredgecolor='g', markerfacecolor='white')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()```Code language: PHP (php)```