Skip to content

Commit 61524bb

Browse files
committed
New Calculator
1 parent e8fd1d6 commit 61524bb

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

requirements/Equation_Plotter.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
matplotlib
2+
numpy

src/Equation_Plotter.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
Version: 0.1.0
3+
Author: Minemetero
4+
"""
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
8+
def plot_equation(equation, x_range, y_range, title="Equation Plot"):
9+
"""
10+
Plots a given mathematical equation.
11+
12+
Parameters:
13+
equation (str): The equation to plot, with 'x' as the variable.
14+
x_range (tuple): The range of x values (min, max).
15+
y_range (tuple): The range of y values (min, max).
16+
title (str): The title of the plot.
17+
"""
18+
# Define the x values
19+
x = np.linspace(x_range[0], x_range[1], 400)
20+
21+
try:
22+
# Define the y values by evaluating the equation
23+
y = eval(equation)
24+
except Exception as e:
25+
print(f"Error in equation: {e}")
26+
return
27+
28+
# Create the plot
29+
plt.figure(figsize=(8, 6))
30+
plt.plot(x, y, label=f'y = {equation}')
31+
plt.xlim(x_range)
32+
plt.ylim(y_range)
33+
plt.axhline(0, color='black', linewidth=0.5)
34+
plt.axvline(0, color='black', linewidth=0.5)
35+
plt.grid(color='gray', linestyle='--', linewidth=0.5)
36+
plt.title(title)
37+
plt.xlabel('x')
38+
plt.ylabel('y')
39+
plt.legend()
40+
plt.show()
41+
42+
def get_range(prompt):
43+
"""
44+
Prompts the user for a range and returns it as a tuple.
45+
"""
46+
while True:
47+
try:
48+
range_input = input(prompt)
49+
range_values = tuple(map(float, range_input.split(',')))
50+
if len(range_values) == 2 and range_values[0] < range_values[1]:
51+
return range_values
52+
else:
53+
print("Please enter two numbers separated by a comma, where the first number is less than the second.")
54+
except ValueError:
55+
print("Invalid input. Please enter two numbers separated by a comma.")
56+
57+
def main():
58+
print("Welcome to the Equation Plotter!")
59+
60+
# Input equation from user
61+
while True:
62+
user_equation = input("Enter the equation in terms of 'x' (e.g., x**2 + 2*x + 1): ")
63+
try:
64+
# Test if the equation can be evaluated
65+
x = np.linspace(-10, 10, 10)
66+
y = eval(user_equation)
67+
if isinstance(y, np.ndarray):
68+
break
69+
except Exception as e:
70+
print(f"Invalid equation: {e}")
71+
72+
# Get x range from user
73+
x_range = get_range("Enter the x range as two numbers separated by a comma (e.g., -10, 10): ")
74+
75+
# Get y range from user
76+
y_range = get_range("Enter the y range as two numbers separated by a comma (e.g., -10, 10): ")
77+
78+
# Plot the equation
79+
plot_equation(user_equation, x_range, y_range)
80+
81+
if __name__ == "__main__":
82+
main()

0 commit comments

Comments
 (0)