I want to write a piece of general code that can simultaneously solve and plot two types of the system of two-variable nonlinear homogeneous equations. The first type of system has numerical solutions, i.e., coordinate points (which may be more than one), while the second type has analytical solutions, i.e., curves. It is also possible that a system of equations has no solution. I want the code to be flexible enough to handle these different types of solutions. I have written Code 1 and Code 2, but each one only works for one type of equation system. Please help me modify and combine these two pieces of code to create a more general-purpose solution. Thank you in advance!
Code 1 (Type 1):
ClearAll["Global`*"]; (*Define the system of equations*) system = {x + y == 0, x^2 - y == 0}; (*Use Reduce to solve the system*) solutions = Reduce[system, {x, y}, Reals]; (*Output the solutions*) Print["Solutions: ", solutions]; (*Initialize lists to store functions and points*) funcs = {}; points = {}; (*Use LogicalExpand to split the solutions*) cases = List @@ LogicalExpand[solutions]; (*Iterate over each solution*) Do[(*Solve each case*)sol = Solve[case, {x, y}, Reals]; If[Length[sol] > 0,(*Extract and store solutions*)Do[xVal = x /. s; yVal = y /. s; AppendTo[points, {xVal, yVal}], {s, sol}]], {case, cases}]; (*Output the extracted solutions*) If[funcs =!= {}, Print["Curve solutions: ", funcs];]; If[points =!= {}, Print["Discrete point solutions: ", points];]; (*Plot the solutions*) Module[{plotFuncs, plotPoints, combinedPlot},(*Plot all function solutions*) If[funcs =!= {}, plotFuncs = Plot[Evaluate[funcs], {x, -5, 5}, PlotStyle -> Blue, PlotRange -> All, AxesLabel -> {"x", "y"}, PlotLabel -> "Solutions of the system"], plotFuncs = Nothing]; (*Plot all discrete point solutions*) If[points =!= {}, plotPoints = ListPlot[points, PlotStyle -> {Red, PointSize[Large]}], plotPoints = Nothing]; (*Combine plots*) combinedPlot = Show[Sequence @@ DeleteCases[{plotFuncs, plotPoints}, Nothing], PlotRange -> All, AxesLabel -> {"x", "y"}, PlotLabel -> "Solutions of the system", ImageSize -> Large]; (*Display the combined plot*)combinedPlot]
Output:
Solutions: (x==-1||x==0)&&y==-x Discrete point solutions: {{-1,1},{0,0}}
Code 2 (Type 2):
ClearAll["Global`*"]; (*Define the system of equations*) system = {x y - x^3 == 0, y^2 - 2 x^2 y + x^4 == 0}; (*Use Reduce to solve the system*) solutions = Reduce[system, {x, y}, Reals]; (*Output the solutions*) Print["Solutions: ", solutions]; (*Initialize lists to store functions and points*) funcs = {}; points = {}; (*Use logical operators to split different solutions*) If[StringContainsQ[ToString[solutions], "||"],(*Split multiple solutions*) cases = ToExpression[ "{" <> StringReplace[ToString[solutions], "||" -> ","] <> "}"];,(*Single solution*)cases = {solutions};]; (*Iterate over each solution*) Do[(*If the solution is y==f(x)*) If[MatchQ[case, y == _],(*Extract the function f(x) and add to funcs list*) AppendTo[funcs, case[[2]]],(*Otherwise, assume it's a discrete point solution*) If[MatchQ[case, And[_, _]],(*Extract x and y values*) xVal = x /. Solve[case, x, Reals]; yVal = y /. Solve[case, y, Reals]; (*If both can be successfully extracted and are unique, add to points list*) If[Length[xVal] == 1 && Length[yVal] == 1, AppendTo[points, {xVal[[1]], yVal[[1]]}]]]], {case, cases}]; (*Output the extracted solutions*) If[funcs =!= {}, Print["Curve solutions: ", funcs];]; If[points =!= {}, Print["Discrete point solutions: ", points];]; (*Plot the solutions*) Module[{plotFuncs, plotPoints, combinedPlot},(*Plot all function solutions*) If[funcs =!= {}, plotFuncs = Plot[Evaluate[funcs], {x, -5, 5}, PlotStyle -> Blue, PlotRange -> All, AxesLabel -> {"x", "y"}, PlotLabel -> "Solutions of the system"];, plotFuncs = Nothing;]; (*Plot all discrete point solutions*) If[points =!= {}, plotPoints = ListPlot[points, PlotStyle -> {Red, PointSize[Large]}];, plotPoints = Nothing;]; (*Combine plots:Only call Show when there are valid graphical \ objects*)combinedPlot = Show[Sequence @@ DeleteCases[{plotFuncs, plotPoints}, Nothing], PlotRange -> All, AxesLabel -> {"x", "y"}, PlotLabel -> "Solutions of the system", ImageSize -> Large]; (*Display the combined plot*)combinedPlot]
Output:
Solutions: y==x^2 Curve solutions: {x^2}
Type 3: No solutions