File size: 2,901 Bytes
761e949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
import plotly.graph_objects as go
from sympy import symbols, lambdify
from sympy.parsing.sympy_parser import parse_expr, standard_transformations, implicit_multiplication_application
from solver import preprocess_equation

def plot_function(input_str):
    """Create an interactive plot of the function."""
    try:
        # Preprocess input
        processed_input = preprocess_equation(input_str)

        # Handle different types of input
        if '=' in processed_input:
            # Equation: move everything to left side
            left_side, right_side = [side.strip() for side in processed_input.split('=')]
            transformations = standard_transformations + (implicit_multiplication_application,)
            expr = parse_expr(left_side, transformations=transformations) - parse_expr(right_side, transformations=transformations)
        elif input_str.startswith('∫'):
            # Integration: plot the original function
            expr = parse_expr(processed_input[1:].strip(), transformations=(standard_transformations + (implicit_multiplication_application,)))
        elif input_str.startswith('d/dx'):
            # Derivative: plot the original function
            expr = parse_expr(processed_input[4:].strip(), transformations=(standard_transformations + (implicit_multiplication_application,)))
        else:
            # Regular expression
            expr = parse_expr(processed_input, transformations=(standard_transformations + (implicit_multiplication_application,)))

        # Create lambda function for numpy evaluation
        x = symbols('x')
        f = lambdify(x, expr, 'numpy')

        # Generate x values
        x_vals = np.linspace(-10, 10, 1000)

        # Calculate y values
        y_vals = f(x_vals)

        # Create plot
        fig = go.Figure()

        # Add function curve
        fig.add_trace(go.Scatter(
            x=x_vals,
            y=y_vals,
            mode='lines',
            name='f(x)',
            line=dict(color='#FF4B4B', width=2)
        ))

        # Add x-axis line
        fig.add_trace(go.Scatter(
            x=x_vals,
            y=[0]*len(x_vals),
            mode='lines',
            name='x-axis',
            line=dict(color='black', width=1)
        ))

        # Update layout
        fig.update_layout(
            title='Function Visualization',
            xaxis_title='x',
            yaxis_title='y',
            showlegend=True,
            hovermode='x',
            plot_bgcolor='white',
            width=800,
            height=500
        )

        # Update axes
        fig.update_xaxes(zeroline=True, zerolinewidth=1, zerolinecolor='black', gridcolor='lightgray')
        fig.update_yaxes(zeroline=True, zerolinewidth=1, zerolinecolor='black', gridcolor='lightgray')

        return fig

    except Exception as e:
        raise Exception(f"Error creating plot: {str(e)}")