Spaces:
Sleeping
Sleeping
File size: 1,959 Bytes
be1aee8 c8bb10d be1aee8 35f1071 94391f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
st.title('Решение системы линейных уравнений')
st.write('Введите уравнения в формате ax + by = c, то есть ставя пробел после каждого числа и знака, но если число отрицательное пишите минус показывающий отрицательность числа и число слитно')
equation1 = st.text_input('Уравнение 1:')
equation2 = st.text_input('Уравнение 2:')
solve_button = st.button('Решить')
if solve_button:
def parse_equation(equation):
parts = equation.split('=')
coef = parts[0].split('x')
a = float(coef[0]) if coef[0] else 1
b_str = coef[1].replace('y', '').replace('+', '').replace('-', '').strip()
b = float(b_str) if b_str else 0
c = float(parts[1])
return a, b, c
a1, b1, c1 = parse_equation(equation1)
a2, b2, c2 = parse_equation(equation2)
intersection = None
if np.linalg.det([[a1, b1], [a2, b2]]) != 0:
intersection = np.linalg.solve([[a1, b1], [a2, b2]], [c1, c2])
else:
st.write('Система уравнений вырожденная, решений бесконечно много или их нет')
if intersection is not None:
st.write(f'Координаты точки пересечения прямых: ({intersection[0]}, {intersection[1]})')
x = np.linspace(-10, 10, 400)
y1 = (c1 - a1 * x) / b1
y2 = (c2 - a2 * x) / b2
fig, ax = plt.subplots()
ax.plot(x, y1, label='Уравнение 1')
ax.plot(x, y2, label='Уравнение 2')
if intersection is not None:
ax.scatter(intersection[0], intersection[1], color='red', label='Пересечение')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
st.pyplot(fig)
|