|
> |
odegenerator := module()
option package;
export odegeniter, odegenman;# odegenexpl;
######################
odegeniter := proc(V, t, y, differential_eq, solve_all, range)
local a0, a1, a2, b0, b1, eq, rows, row_eqs, numrows, i, single_row, selected_rows, dataframe;
uses PDEtools;
# Check if the input is a valid differential equation
if not has(differential_eq, diff(y(t), t)) then
error "The 'differential_eq' parameter must be a valid differential equation containing diff(y(t), t).";
end if;
# Validate solve_all and range parameters
if not (solve_all = true or solve_all = false) then
error "The solve_all parameter must be true or false.";
end if;
if not (range = 0 or type(range, posint) or type(range, range)) then
error "The range parameter must be 0, a positive integer, or a range (e.g., 1..5).";
end if;
# Initialize storage for equations and solutions
rows := [];
row_eqs := [];
# Iterate through all combinations of parameters in V
for a0 in V do
for a1 in V do
for a2 in V do
for b0 in V do
for b1 in V do
# Substitute parameters into the differential equation
eq := differential_eq;
eq := subs('a__0' = ifelse(a0 = 0, infinity, a0), eq);
eq := subs('a__1' = ifelse(a1 = 0, infinity, a1), eq);
eq := subs('a__2' = ifelse(a2 = 0, infinity, a2), eq);
eq := subs('b__0' = ifelse(b0 = 0, infinity, b0), eq);
eq := subs('b__1' = ifelse(b1 = 0, infinity, b1), eq);
# Add the equation to the rows
rows := [op(rows), [nops(rows) + 1, a0, a1, a2, b0, b1, eq, ""]];
row_eqs := [op(row_eqs), eq];
end do;
end do;
end do;
end do;
end do;
# Get the number of rows generated
numrows := nops(rows);
# Generate the full DataFrame when solve_all is true and range is 0
if solve_all = true and range = 0 then
printf("Generating full DataFrame...\n");
for i to numrows do
try
rows[i][8] := rhs(dsolve(row_eqs[i], y(t)));
catch:
rows[i][8] := "No explicit solution";
end try;
end do;
# Create and return the DataFrame
dataframe := DataFrame(Matrix(rows), columns = ['Row', 'a__0', 'a__1', 'a__2', 'b__0', 'b__1', 'Equation', 'Solution']);
return dataframe;
elif solve_all = true and type(range, posint) then
# Retrieve a specific row from the DataFrame
printf("Retrieving row %d from DataFrame...\n", range);
if range > numrows then
error "Row index out of bounds.";
end if;
single_row := rows[range];
try
single_row[8] := rhs(dsolve(row_eqs[range], y(t)));
catch:
single_row[8] := "No explicit solution";
end try;
return DataFrame(Matrix([single_row]), columns = ['Row', 'a__0', 'a__1', 'a__2', 'b__0', 'b__1', 'Equation', 'Solution']);
elif solve_all = true and type(range, range) then
# Retrieve rows within a specified range
printf("Retrieving rows %a from DataFrame...\n", range);
selected_rows := [];
for i from op(1, range) to op(2, range) do
if i > numrows then
error "Row index out of bounds.";
end if;
selected_rows := [op(selected_rows), rows[i]];
try
selected_rows[-1][8] := rhs(dsolve(row_eqs[i], y(t)));
catch:
selected_rows[-1][8] := "No explicit solution";
end try;
end do;
# Return the selected rows as a DataFrame
return DataFrame(Matrix(selected_rows), columns = ['Row', 'a__0', 'a__1', 'a__2', 'b__0', 'b__1', 'Equation', 'Solution']);
else
error "Invalid combination of solve_all and range.";
end if;
end proc:
################
odegenman := proc(V, x, y, differential_eq, const_values, xrange, yrange, initial_conditions)
local a0, a1, a2, b0, b1, sol, Fsol, row_number, count,
a0_iter, a1_iter, a2_iter, b0_iter, b1_iter, modified_eq, eq_type, odeplot_cmd;
uses PDEtools, plots;
# Check if const_values is valid
if not type(const_values, list) or nops(const_values) <> 5 then
error "The constant values (const_values) must be a list with exactly 5 elements.";
end if;
# Check if xrange and yrange are valid
if not type(xrange, range) or not type(yrange, range) then
error "Both xrange and yrange must be ranges, e.g., 0..2 or -10..10.";
end if;
# Check if initial_conditions is a valid list
if not type(initial_conditions, list) or nops(initial_conditions) = 0 then
error "Initial conditions must be provided as a non-empty list.";
end if;
# Assign the constant values
a0 := const_values[1];
a1 := const_values[2];
a2 := const_values[3];
b0 := const_values[4];
b1 := const_values[5];
# Find the row number by unique identification
count := 1;
row_number := "Unknown"; # Default value if no row is found
for a0_iter in V do
for a1_iter in V do
for a2_iter in V do
for b0_iter in V do
for b1_iter in V do
if [a0_iter, a1_iter, a2_iter, b0_iter, b1_iter] = const_values then
row_number := sprintf("%d", count); # Convert to string
end if;
count := count + 1;
end do;
end do;
end do;
end do;
end do;
# Create a modified version of the differential equation
modified_eq := differential_eq;
if a0 = 0 then
modified_eq := subs('a__0' = infinity, modified_eq); # Disable term
else
modified_eq := subs('a__0' = a0, modified_eq);
end if;
if a1 = 0 then
modified_eq := subs('a__1' = infinity, modified_eq);
else
modified_eq := subs('a__1' = a1, modified_eq);
end if;
if a2 = 0 then
modified_eq := subs('a__2' = infinity, modified_eq);
else
modified_eq := subs('a__2' = a2, modified_eq);
end if;
if b0 = 0 then
modified_eq := subs('b__0' = infinity, modified_eq);
else
modified_eq := subs('b__0' = b0, modified_eq);
end if;
if b1 = 0 then
modified_eq := subs('b__1' = infinity, modified_eq);
else
modified_eq := subs('b__1' = b1, modified_eq);
end if;
# Evaluate the type of the ODE
eq_type := DEtools:-odeadvisor(modified_eq);
# Solve the equation symbolically
printf("The corresponding row number is: %s\n", row_number);
printf("The simplified ODE with the given coefficients is:\n\n");
print(modified_eq, eq_type);
printf("Attempting to solve symbolically...\n");
try
sol := dsolve(modified_eq, y(x));
if type(sol, `=`) then
Fsol := rhs(sol);
printf("Explicit symbolic solution found:\n");
print(Fsol);
else
Fsol := "No explicit solution (analytically).";
printf("No explicit symbolic solution found for the differential equation. A DEplot will be generated numerically.\n");
end if;
catch:
Fsol := "Symbolic solution failed.";
printf("Symbolic solution failed. A DEplot will be generated numerically.\n");
end try;
# Generate the DE plot numerically
printf("Generating DE plot for the differential equation...\n");
try
odeplot_cmd := DEtools:-DEplot(
modified_eq,
[y(x)],
x = xrange,
initial_conditions,
y = yrange,
stepsize = 0.1,
title = "DE Plot for Differential Equation"
);
print(plots:-display(odeplot_cmd, size = [550, 550]));
catch:
printf("Plotting failed. Ensure the initial conditions and ranges are correct.\n");
end try;
return Fsol;
end proc:
####################
end module:
|
true stands for using DataFrame
false : Error, (in odegeniter) Invalid combination of solve_all and range.
|
(1.1) |
> |
differential_eq:= diff(y(t), t) = sin(t)/'a__0' + y(t)/'a__1' + y(t)^2/'a__2' + exp(-t)/'b__0' + sinh(-t)/'b__1';
V := {0, 1};
|
|
(1.2) |
> |
odegeniter(V, t, y, differential_eq, true, 0);# all DataFrame rows, using 0
|
Generating full DataFrame...
|
|
|
(1.3) |
> |
odegeniter(V, t, y, differential_eq, true, 2);# a row in DataFrame, using 2 as rownumber ( check in full Dataframe)
|
Retrieving row 2 from DataFrame...
|
|
|
(1.4) |
> |
odegeniter(V, t, y, differential_eq, true, 2..5);# a range of rows in DataFrame ( check in full Dataframe)
|
Retrieving rows 2 .. 5 from DataFrame...
|
|
|
(1.5) |
Now using odegenman for plotting a fieldplot from the differential eqation for a rownumber from the DataFrame
> |
V := {0, 1};
differential_eq := diff(y(t), t) = sin(t)/'a__0' + y(t)/'a__1' + y(t)^2/'a__2' + exp(-t)/'b__0' + sinh(-t)/'b__1';
trange := 0..5;
yrange := -10..10;
initial_conditions := [[y(0) = 1], [y(0) = -1], [y(0) = 2]];# using 3 initial conditions
odegenman(V, t, y, differential_eq, [1, 1, 1, 1, 1], trange, yrange, initial_conditions);
|
The corresponding row number is: 32
The simplified ODE with the given coefficients is:
|
|
Attempting to solve symbolically...
No explicit symbolic solution found for the differential equation. A DEplot will be generated numerically.
Generating DE plot for the differential equation...
|
|
|
(1.6) |
lest take row 0 : y' = C , row 1 has 5 zeroes to fill in list in command
> |
#odegenman(V, t, y, differential_eq, [1, 1, 1, 1, 1], trange, yrange, initial_conditions);# example of 5 ones in list is last rownumber of 32 rows for V := {0, 1};
|
> |
odegenman(V, t, y, differential_eq, [0, 0, 0, 0, 0], trange, yrange, initial_conditions);
|
The corresponding row number is: 1
The simplified ODE with the given coefficients is:
|
|
Attempting to solve symbolically...
Explicit symbolic solution found:
|
|
Generating DE plot for the differential equation...
|
|
|
(1.7) |
Is correct
> |
V := {0, 1};
differential_eq := diff(y(t), t) = 'a__0'*(y(t))^2 + 'b__0'* t^k;# special Riccati equation , k is an arbititrary number
|
|
(1.8) |
> |
odegeniter(V, t, y, differential_eq, true, 0);#, a0 = 1 and b0 = 1 , there is a overlap of rows
|
Generating full DataFrame...
|
|
|
(1.9) |
> |
odegeniter(V, t, y, differential_eq, true, 19);# row 19 gives the general solution, but using row 32 is always correct
|
Retrieving row 19 from DataFrame...
|
|
|
(1.10) |
now what is dfieldplot of this?
> |
V := {0, 1};
differential_eq := diff(y(t), t) = 'a__0'*(y(t))^2 + 'b__0'* t^k;
trange := 0..5;
yrange := -10..10;
initial_conditions := [[y(0) = 1], [y(0) = -1], [y(0) = 2]];# using 3 initial conditions differential_eq := diff(y(t), t) = 'a__0'*(y(t))^2 + 'b__0'* t^k;
|
|
(1.11) |
> |
odegenman(V, t, y, differential_eq, [1, 0, 0, 1, 0], trange, yrange, initial_conditions);#rownumber 19
|
The corresponding row number is: 19
The simplified ODE with the given coefficients is:
|
|
Attempting to solve symbolically...
Explicit symbolic solution found:
|
|
Generating DE plot for the differential equation...
Plotting failed. Ensure the initial conditions and ranges are correct.
|
|

|
(1.12) |
How to get a fieldplot for this DE : try to find information about this Bessel functions ?
> |
FunctionAdvisor(Bessel, quiet);
|
![[AiryAi, AiryBi, BesselI, BesselJ, BesselK, BesselY, HankelH1, HankelH2, KelvinBei, KelvinBer, KelvinHei, KelvinHer, KelvinKei, KelvinKer]](/view.aspx?sf=307313_Answer/21dcef9a18b80447fb220cbdf492a975.gif)
|
(1.13) |
> |
FunctionAdvisor(BesselJ, quiet);
|
> |
FunctionAdvisor(BesselY, quiet);
|
|