0

I have a system of 6 nonlinear equations which I want to solve for 6 unknown constants; $A_i, i=0,...,5$. I wrote the following script which successfully does that:

clear["Global'*"];
e1 = A1 == 10*(1 + A0); 
e2 = A1 + 2 A2 + 3 A3 + 4 A4 + 5 A5 == 0;
e3 = A1 - 3 A0^2*A1 + 6*(1/10)*A3 == 0;
e4 = A1 + 2 A2 + 3 A3 + 4 A4 + 5 A5 - 
    3*(A0 + A1 + A2 + A3 + A4 + A5)^2*(A1 + 2 A2 + 3 A3 + 4 A4 + 
    5 A5) + 6*(1/10)*A3 + 24*(1/10)*A4 + 60*(1/10)*A5 == 0;
e5 = A0 + A1/2 + A2/3 + A3/4 + A4/5 + A5/6 == (-8)/10;
e6 = 2 A2 - 6 A0*A1^2 - 6*A0^2*A2 + 24*(1/10)*A4 == 0;
NSolve[{e1, e2, e3, e4, e5, e6}, {A0, A1, A2, A3, A4, A5}, Reals, 
    WorkingPrecision -> 7]

This runs exactly how I want it to, however, it gives me three possible solutions for the $A_i$:

{{A0 -> 2.866825, A1 -> 38.66825, A2 -> -610.304, A3 -> 1524.567, 
     A4 -> -1314.739, A5 -> 373.439}, {A0 -> 0.071490, A1 -> 10.71490, 
     A2 -> -21.71161, A3 -> -17.5844, A4 -> 38.3346, 
     A5 -> -13.57541}, {A0 -> -0.9577089, A1 -> 0.4229107, 
     A2 -> -0.539306, A3 -> 1.23463, A4 -> -1.21544, A5 -> 0.362713}}

What I now want my script to do is to create 3 polynomials with each of these solutions as the coefficients, i.e. $f(z) = A_0 + A_1z + A_2z^2 + A_3z^3 + A_4z^4 + A_5z^5 $ and then simply plot the three functions on the same graph. I'm not quite sure how to get Mathematica to grab the ouput as a variable for later use however. I can just hardcode the three functions and plot them obviously but I want to program it in a way so that when I change one of the equations I don't have to change everything. Any suggestions?

My thought would be to store these in a $3 \times 6$ matrix where I could then pull out the coefficients quite easily, but I'm not sure how to do this.

EDIT: Note, I tried to store the NSolve command as a variable called coeffs. However, if I then use

Part[coeffs,1,1]

it outputs

A0 -> 2.866825

whereas ideally I would just want the actual value.

RESOLVED: Thank you @Michael E2 for the link. I was able to resolve this issue using the following commands:

coeffs = NSolve[{e1, e2, e3, e4, e5, e6}, {A0, A1, A2, A3, A4, A5}, 
    Reals, WorkingPrecision -> 7]
mat = {A0, A1, A2, A3, A4, A5} /. coeffs
f[z_] = Part[mat, 1, 1] + Part[mat, 1, 2]*z + Part[mat, 1, 3]*z^2 + 
    Part[mat, 1, 4]*z^3 + Part[mat, 1, 5]*z^4 + Part[mat, 1, 6]*z^5
g[z_] = Part[mat, 2, 1] + Part[mat, 2, 2]*z + Part[mat, 2, 3]*z^2 + 
    Part[mat, 2, 4]*z^3 + Part[mat, 2, 5]*z^4 + Part[mat, 2, 6]*z^5
h[z_] = Part[mat, 3, 1] + Part[mat, 3, 2]*z + Part[mat, 3, 3]*z^2 + 
    Part[mat, 3, 4]*z^3 + Part[mat, 3, 5]*z^4 + Part[mat, 3, 6]*z^5
Plot[{f[z], g[z], h[z]}, {z, 0, 1}, PlotRange -> {-1, 1}, 
    PlotLegends -> "Expressions"]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Mjoseph
  • 233
  • 1
  • 5
  • Maybe this? `In[15]:= NSolveValues[{e1, e2, e3, e4, e5, e6}, {A0, A1, A2, A3, A4, A5}, Reals, WorkingPrecision -> 10] . x^Range[0, 5]

    Out[15]= {2.866828175 + 38.66828175 x - 610.304963 x^2 + 1524.568634 x^3 - 1314.739971 x^4 + 373.439125 x^5, 0.071489207 + 10.71489207 x - 21.71159549 x^2 - 17.5843501 x^3 + 38.3345871 x^4 - 13.57539982 x^5, -0.9577089349 + 0.4229106508 x - 0.539305302 x^2 + 1.23463070 x^3 - 1.21543840 x^4 + 0.362712290 x^5}`

    – Daniel Lichtblau Jun 08 '22 at 15:39
  • 3
    See also https://mathematica.stackexchange.com/a/18706/4999 – Michael E2 Jun 08 '22 at 15:42

1 Answers1

2

I present a solution using Fold. Starting with:

sol = {{A0 -> 2.866825, A1 -> 38.66825, A2 -> -610.304, 
   A3 -> 1524.567, A4 -> -1314.739, A5 -> 373.439}, {A0 -> 0.071490, 
   A1 -> 10.71490, A2 -> -21.71161, A3 -> -17.5844, A4 -> 38.3346, 
   A5 -> -13.57541}, {A0 -> -0.9577089, A1 -> 0.4229107, 
   A2 -> -0.539306, A3 -> 1.23463, A4 -> -1.21544, A5 -> 0.362713}}

vars = ToExpression["A" <> ToString@# & /@ Range[0, 5]]

{A0, A1, A2, A3, A4, A5}

eqn = (Fold[#2 + #1 z &, Reverse@vars] // Expand)
A0 + A1 z + A2 z^2 + A3 z^3 + A4 z^4 + A5 z^5
(eqn /. sol[[#]]) & /@ Range[Length@sol]

{2.86683 + 38.6683 z - 610.304 z^2 + 1524.57 z^3 - 1314.74 z^4 + 373.439 z^5, 0.07149 + 10.7149 z - 21.7116 z^2 - 17.5844 z^3 + 38.3346 z^4 - 13.5754 z^5, -0.957709 + 0.422911 z - 0.539306 z^2 + 1.23463 z^3 - 1.21544 z^4 + 0.362713 z^5}

Syed
  • 52,495
  • 4
  • 30
  • 85