0
$\begingroup$

I am trying to plot a function that I had converted from the returned list of rule assignments from Solve[ ]. I am trying to plot this, but the resulting graph is empty. Does anyone know what I am doing wrong?

I see that the function that I am trying to plot is in a list. However, after returning the first index, the equation, my plot with it is still empty.

deltaO = O == Q + P*V;
Qsolved = Solve[deltaO, Q][[1]] /. Rule -> Equal;
Qsolved
Qrules = Qsolved /. {O -> 27.1, V -> (19.6 - 6.43)}
Qrules[[1]]
Plot[Qrules[[1]], {P, 0, 1}]

enter image description here

I also tried converting it into a Function, like the following, but it doesn't work either.

deltaO = O == Q + P*V;
Qsolved = Solve[deltaO, Q][[1]] /. Rule -> Equal;
Qsolved
Qrules = Qsolved /. {O -> 27.1, V -> (19.6 - 6.43)}
function[P_] := Evaluate[Qrules[[1]]]
Plot[function[P_], {P, 0, 1}]

This may just be a math error on my part, but I figured that the plot would return autoscale the y-axis to show the values.

  • Thank you
$\endgroup$
3
  • 1
    $\begingroup$ O is a reserved symbol, so I would suggest avoiding the use of it as a variable. $\endgroup$ Jan 29 '21 at 19:08
  • $\begingroup$ In deltaO = O == Q + P*V you use the character "O" instead of zero. $\endgroup$ Jan 29 '21 at 19:30
  • $\begingroup$ I used other symbols besides O in the past, such as [CapitalDelta]E, but they didn't work either. Also, I intended for it to be an O, since I was under the assumption that the O was written to a number through the rule assignment. $\endgroup$ Jan 29 '21 at 19:35
2
$\begingroup$

Here's one way to do it:

deltaO = o == q + p*v;
qsolved = q /. First@Solve[deltaO, q]
qrules = qsolved /. {o -> 27.1, v -> (19.6 - 6.43)}
Plot[
  qrules, {p, 0, 1}
]

Plot of the function.

It's best to avoid beginning variable names or function names with capital letters. Mathematica symbols always begin with capital letters. C, D, E, I, K, N, and O all have built-in meaning. If you're lucky, whatever you're trying to do with them won't break your code, but try evaluating E = 5 and you'll immediately get an error since E is Euler's number and Mathematica considers E = 5 to be the same as something like 1 = 2.

The second thing is that the function provided to Plot should not have any equal signs in it. If you would like to, you could do something like:

ContourPlot[
  q == qrules,
  {p, 0, 1}, {q, 0, 40}
]

where qrules comes from the earlier code.

$\endgroup$
1
$\begingroup$

While I find your approach somewhat of an overkill, you can get the result you want by correcting your Part specification as such:

deltaO = Oh == Q + P*V;
Qsolved = Solve[deltaO, Q][[1]] /. Rule -> Equal;
Qsolved
Qrules = Qsolved /. {Oh -> 27.1, V -> (19.6 - 6.43)}
Qrules[[1]]
Plot[Qrules[[1, 2]], {P, 0, 1}]

That is, note the 2nd index in Qrules[[1,2]]. This is needed because Qrules[[1]] alone is an equation, whereas what you want to plot is the RHS of the same.enter image description here

$\endgroup$
1
$\begingroup$

It can be as simple as this:

deltaΟ = Ο == Q + P V;
Qsolved[P_] = Solve[deltaO, Q][[1, 1, 2]];
Plot[Qsolved[P] /. {Ο -> 27.1, V -> 19.6 - 6.43}, {P, 0, 1},
  AxesLabel -> {"P", "Q"}]

plot

Note: What looks like $O$ in the above code is actually \[CapitalOmicron], so it does not conflict with any predefined Mathematica symbols.

$\endgroup$

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