Spaces:
Runtime error
Runtime error
I think the differential equations work
Browse files
maths/differential_equations/solve_first_order_ode.py
CHANGED
@@ -6,6 +6,8 @@ import numpy as np
|
|
6 |
from scipy.integrate import solve_ivp
|
7 |
from typing import Callable, List, Tuple, Dict, Any, Union
|
8 |
import matplotlib.pyplot as plt
|
|
|
|
|
9 |
|
10 |
ODEFunc = Callable[[float, Union[np.ndarray, List[float]]], Union[np.ndarray, List[float]]]
|
11 |
|
@@ -60,3 +62,34 @@ def solve_first_order_ode(
|
|
60 |
'success': False,
|
61 |
'plot_path': None
|
62 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from scipy.integrate import solve_ivp
|
7 |
from typing import Callable, List, Tuple, Dict, Any, Union
|
8 |
import matplotlib.pyplot as plt
|
9 |
+
from maths.differential_equations.ode_interface_utils import parse_float_list, parse_time_span, string_to_ode_func
|
10 |
+
import gradio as gr
|
11 |
|
12 |
ODEFunc = Callable[[float, Union[np.ndarray, List[float]]], Union[np.ndarray, List[float]]]
|
13 |
|
|
|
62 |
'success': False,
|
63 |
'plot_path': None
|
64 |
}
|
65 |
+
# --- Gradio Interface for First-Order ODEs ---
|
66 |
+
first_order_ode_interface = gr.Interface(
|
67 |
+
fn=lambda ode_str, t_span_str, y0_str, t_eval_count, method: solve_first_order_ode(
|
68 |
+
string_to_ode_func(ode_str, ('t', 'y')),
|
69 |
+
parse_time_span(t_span_str),
|
70 |
+
parse_float_list(y0_str), # y0 can be a list for systems
|
71 |
+
int(t_eval_count),
|
72 |
+
method
|
73 |
+
),
|
74 |
+
inputs=[
|
75 |
+
gr.Textbox(label="ODE Function (lambda t, y: ...)",
|
76 |
+
placeholder="e.g., lambda t, y: -y*t OR for system lambda t, y: [y[1], -0.1*y[1] - y[0]]",
|
77 |
+
info="Define dy/dt or a system [dy1/dt, dy2/dt,...]. `y` is a list/array for systems."),
|
78 |
+
gr.Textbox(label="Time Span (t_start, t_end)", placeholder="e.g., 0,10"),
|
79 |
+
gr.Textbox(label="Initial Condition(s) y(t_start)", placeholder="e.g., 1 OR for system 1,0"),
|
80 |
+
gr.Slider(minimum=10, maximum=1000, value=100, step=10, label="Evaluation Points Count"),
|
81 |
+
gr.Radio(choices=['RK45', 'LSODA', 'BDF', 'RK23', 'DOP853'], value='RK45', label="Solver Method")
|
82 |
+
],
|
83 |
+
outputs=[
|
84 |
+
gr.Image(label="Solution Plot", type="filepath", show_label=True, visible=lambda res: res['success'] and res['plot_path'] is not None),
|
85 |
+
gr.Textbox(label="Solver Message"),
|
86 |
+
gr.Textbox(label="Success Status"),
|
87 |
+
gr.JSON(label="Raw Data (t, y values)", visible=lambda res: res['success']) # For users to copy if needed
|
88 |
+
],
|
89 |
+
title="First-Order ODE Solver",
|
90 |
+
description="Solves dy/dt = f(t,y) or a system of first-order ODEs. " \
|
91 |
+
"WARNING: Uses eval() for the ODE function string - potential security risk. " \
|
92 |
+
"For systems, `y` in lambda is `[y1, y2, ...]`, return `[dy1/dt, dy2/dt, ...]`. " \
|
93 |
+
"Example (Damped Oscillator): ODE: lambda t, y: [y[1], -0.5*y[1] - y[0]], y0: 1,0, Timespan: 0,20",
|
94 |
+
flagging_mode="manual"
|
95 |
+
)
|