2
$\begingroup$

I'm having some problems with my mathematica code, I think I'm overlooking something simple. I get valid solutions for u0 thru u4, but it doesnt generate any plot at all. Also, I'd really like to plot in terms of ph, not ohc, and have given the relationships as well.

Solve[{u0 + u1 + u2 + u4 == 1,
  k1 == u1/(u0*ohc),
  k2 == u2/(u0*ohc^2),
  k4 == u4/(u0*ohc^4)},
 {u0, u1, u2, u4}]

Solve[Log[10, k1] == 9.5, k1]
Solve[Log[10, k2] == 22.8, k2]
Solve[Log[10, k4] == 32.4, k4]

ph == 14 - poh;
poh == -log[ohc];

Plot[{u0, u1, u2, u4}, {ohc, 0, 1}]

*Preferred:* Plot[eqns,{ph,0,14}]
$\endgroup$
  • 1
    $\begingroup$ Look up /.: {u0, u1, u2, u4} /. Solve[(* equations *), {u0, u1, u2, u4}]. $\endgroup$ – J. M.'s discontentment Oct 21 '15 at 18:03
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey Oct 21 '15 at 18:07
  • $\begingroup$ I've tried defining Sol = Solve[...] and using the /. after the {u0-u4} but it still wont plot $\endgroup$ – funkyhunky Oct 21 '15 at 18:10
  • $\begingroup$ sol = Solve[{u0 + u1 + u2 + u4 == 1, k1 == u1/(u0*ohc), k2 == u2/(u0*ohc^2), k4 == u4/(u0*ohc^4)}, {u0, u1, u2, u4}]: Solve[Log[10, k1] == 9.5, k1] Solve[Log[10, k2] == 22.8, k2] Solve[Log[10, k4] == 32.4, k4] : Plot[{u0, u1, u2, u4} /. sol, {ohc, 0, 1}] $\endgroup$ – funkyhunky Oct 21 '15 at 18:16
3
$\begingroup$
ph == 14 - poh;
poh == -log[ohc];

These are equalities, not assignments. Your Solve function is written correctly, but the result needs to be assigned to something like

sol = First@Solve[
  {
    u0 + u1 + u2 + u4 == 1,
    k1 == u1/(u0*ohc), 
    k2 == u2/(u0*ohc^2),
    k4 == u4/(u0*ohc^4)
  },
  {u0, u1, u2, u4}];

To plot this solution with your values of k1, k2 and k4 use /.

Plot[
  Evaluate[
    {u0, u1, u2, u4} /. sol /. {k1 -> 10^9.5, k2 -> 10^22.8, k4 -> 10^32.4}
  ],
{ohc, 0, 10^-4}]

sol wrt. ohc

where I have limited your range of ohc to make the function visible. To plot wrt. ph instead, we can solve your expression

phsol = First@Solve[ph == 14 + Log[ohc], ohc, Reals]

and substitute this into the plot function as well

Plot[
  Evaluate[
    {u0, u1, u2, u4} /. sol /. phsol /. {k1 -> 10^9.5, k2 -> 10^22.8, k4 -> 10^32.4}
  ],
{ohc, 0, 10}]

sol wrt. ph

Note that in both cases, the scale of u0 and u1 is several orders of magnitude different from your other variables. You can rescale them using

Plot[
  Evaluate[
    {10^10 u0, 10^7 u1, u2, u4} /. sol /. phsol /. {k1 -> 10^9.5, k2 -> 10^22.8, k4 -> 10^32.4}
  ],
{ohc, 0, 10}]

sol wrt. ph with scaling u0 and u1

| improve this answer | |
$\endgroup$
  • $\begingroup$ wow you are a god amongst men. May I ask what the First@ and evaluate do in their context? This was a great example of how the /. operator works. I was also missing the "Reals" piece and I think that was what was messing me up in solving for ohc. Thanks! $\endgroup$ – funkyhunky Oct 21 '15 at 19:03
  • $\begingroup$ Congratulations on your first Godhood badge! $\endgroup$ – march Oct 21 '15 at 19:07
  • $\begingroup$ They are just for neatness. The output of Solve is nested in two {{...}}. Doing First (or Last) gets rid of the outer {...} Evaluate is there to get past a funny nuance of plotting lists of variables in this way. Try removing it, and you will see the lines no longer have different PlotStyle $\endgroup$ – Crêpo Oct 21 '15 at 19:08
  • $\begingroup$ Good tips again $\endgroup$ – funkyhunky Oct 21 '15 at 19:19
  • 1
    $\begingroup$ oh never mind, thats just using the log equation. I just realized that the logs I'm using are actually base 10, so it switches up the graphs and they make more sense now. $\endgroup$ – funkyhunky Oct 21 '15 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.