I'm solving the system of equations that is equivalent to polynomial equation and I'm trying to plot the solutions as a function of one parameter. The problem is that instead of continuous functions I have switching between two solutions at some point. Below you can see as two solutions plotted in red and orange flips in the middle. Still it is clear that there are tree distinct branches. Further I will choose the only one branch that is always positive. But to do so I need to define the function which describe only this branch.
Here is the code to solve the system of equations:
Manipulate[
m = n + 1;
solution = Solve[{r*M0 ==
P + M0*Sum[
i*Product[Evaluate[Symbol["K" <> ToString[j]]]*1, {j, i}]*
P^i, {i, n}]/
Sum[Product[Evaluate[Symbol["K" <> ToString[j]]]*1, {j, i}]*
P^i, {i, 0, n}],
M == M0/Sum[
Product[Evaluate[Symbol["K" <> ToString[j]]]*1, {j, i}]*
P^i, {i, 0, n}],
Table[Evaluate[Symbol["MP" <> ToString[j]]]*1 ==
Times @@ (Table[
Evaluate[Symbol["K" <> ToString[i]]]*1, {i, j}]) M P^j, {j,
n}]} //
Flatten, {P, M,
Table[Evaluate[Symbol["MP" <> ToString[j]]]*1, {j, n}]} //
Flatten];, {n, 3, 1, 1}]
And here is the code to plot the solutions as a functions of parameter 'r':
valP = P /. Take[solution, {1, 4}];
molP[rVal_, K1Val_, K2Val_, K3Val_, M0Val_, DifInd_: 0] :=
Re@With[{r = rVal, K1 = K1Val, K2 = K2Val, K3 = K3Val, M0 = M0Val, índex = DifInd}, (Evaluate@D[valP, {r, índex}])];
Manipulate[
Plot[Evaluate[{molP[r, 10^k1, 10^k2, 10^k3, 10^p0, 0]}],
{r, 0.1, rrmax}, ImageSize -> 500, Frame -> True],
{{rrmax, 10, "r"}, 1, 10},
{{k1, 6, "k1"}, 2, 9},
{{k2, 3, "k2"}, 2, 9},
{{k3, 6, "k3"}, 2, 9},
{{p0, -3.5, "10^P0"}, -1, -10},
ControlPlacement -> Left]
Parameter n in Manipulate define the system of equations and at n=1 or n=2 there is no problems with branches switching:
The problems starts only when n=3. So, the question is - How I can mix the solutions in such way, so I can define all branches as continuous functions ?
Other example is below
solution = Solve[{
K1*P*L == A,
K2*P*L*L == B,
K2*A*L*L == F,
P0 == P + A + B + F,
r*P0 == L + A + 2*B + 3*F
}, {P, L, A, B, F}];
valF = F /. Take[solution, {1, 4}];
FF[rVal_, K1Val_, K2Val_, P0Val_, DifInd_: 0] :=
Re@With[{r = rVal, K1 = K1Val, K2 = K2Val, P0 = P0Val,
índex = DifInd}, (Evaluate@D[valF, {r, índex}])];
Plot[Evaluate[FF[r, 10^6, 10^7, 10^-3.8, 0]],
{r, 0.05, 2},
PlotStyle -> {Red, Blue, Green, Magenta},
PlotRange -> {{0, 1.5}, {-0.00005, 0.0001}},
ImageSize -> Large
]
I need to define 4 continuous functions instead of 4 current not continuous functions!
Solve
withQuartics->False
. $\endgroup$Quit
and try theManipulate
block yourself. You that there are several undefined variables includingP
,M0
, and so on. $\endgroup$