5
$\begingroup$

I am new in mathematica , I am trying to solve an equation and show a graph of the solution and I am getting empty graph because of the additional Parenthesis I get . for example I want to get {101.,0.26994} but I am getting {101., {0.26994}}

Please see my code below

c = -55.26*0.05;
k = 9.04788;
Bs = 120;
μ = 0.015504;

energy =
 
 Table[{b, λ /. 
    NSolve[{(c λ)/(c λ - 1) - k/c (c λ)^2 + 
        c^2 (μ (b - Bs)) == 0, λ >= 0},
     Reals]}, {b, 101, Bs, 0.2}]

ListPlot[{energy},
 Frame -> True,
 Joined -> True, GridLines -> Automatic, PlotRange -> All] 
$\endgroup$
3
  • $\begingroup$ Check document of Part, First. $\endgroup$
    – xzczd
    Commented Jan 14, 2023 at 9:21
  • $\begingroup$ @user1066 you suggested a nice answer that is different from the proposed solutions. May I suggest that you write it as an answer rather than a comment? I am happy to upvote it :-) $\endgroup$
    – bmf
    Commented Jan 14, 2023 at 12:25
  • 2
    $\begingroup$ This question is answered in this article of the "Pitfalls" Q&A: mathematica.stackexchange.com/questions/18393/… $\endgroup$
    – Michael E2
    Commented Jan 14, 2023 at 14:36

5 Answers 5

5
$\begingroup$
ListPlot[ArrayReshape[energy, {Length@energy, 2}], Frame -> True, 
 Joined -> True, GridLines -> Automatic, PlotRange -> All]

plot

$\endgroup$
2
  • 1
    $\begingroup$ Thank you alot ! $\endgroup$
    – maya
    Commented Jan 14, 2023 at 9:37
  • $\begingroup$ @maya glad I was able to help :-) $\endgroup$
    – bmf
    Commented Jan 14, 2023 at 10:54
6
$\begingroup$

The $X$ problem in this $XY$ question is answered here: What are the most common pitfalls awaiting new users?

The $Y$ problem in this $XY$ question:

I am new in mathematica , I am trying to solve an equation and show a graph

Solving the equation:

Adapted from one of the methods in What are the most common pitfalls awaiting new users? :

fn = Normal@
  First@SolveValues[{(c \[Lambda])/(c \[Lambda] - 1) - 
       k/c (c \[Lambda])^2 + c^2 (\[Mu] (b - Bs)) == 0, 
     101 <= b <= Bs}, \[Lambda], Reals]

SolveValues::ratnz: SolveValues was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.

(*
Root[-8.43611*10^47 + 
   7.03009*10^45 b + (-2.16679*10^48 + 1.94241*10^46 b) #1 + 
   1.48485*10^48 #1^2 + 4.10264*10^48 #1^3 &, 1]
*)

Many users are irritated by the warning message. It's not an error, just an explanation of a step Solve took. The reasons the system is programmed to warn the user in this situation are: (1) Solve is an "exact" solver and produces exact output but only if the input is exact; floating-point numbers are treated as inexact. And (2) using exact methods on inexact input might lead to numerical instability (excessive round-off error).

To get rid of the warning, you can use Quiet[Solve[...]] or explictly do what Solve says it is doing:

fn = (*N[*) (* numericizing the result is optional, imo *)
  Normal@First@SolveValues[
    Rationalize[
     {(c \[Lambda])/(c \[Lambda] - 1) - k/c (c \[Lambda])^2 + 
        c^2 (\[Mu] (b - Bs)) == 0, 101 <= b <= Bs},
     0],
    \[Lambda], Reals]
  (*]*)
(*
Root[-5506087503309720000 + 
   45884062527581000 b + (-14142202016653037763 + 
      126777664763706303 b) #1 + 9691344913034469000 #1^2 + 
   26777185994714237847 #1^3 &, 1]
*)

Plotting the graph:

Plot[fn, {b, 101, Bs}, GridLines -> Automatic, Frame -> True]

To plot the graph without solving the equation (perhaps the true $Y$ question):

ContourPlot[
 {(c \[Lambda])/(c \[Lambda] - 1) - k/c (c \[Lambda])^2 + 
    c^2 (\[Mu] (b - Bs)) == 0},
 {b, 101, Bs}, {\[Lambda], 0, 0.29}, AspectRatio -> 1/GoldenRatio, 
 GridLines -> Automatic]
$\endgroup$
5
$\begingroup$

(1)

One way to get a list of { {x,y}, ... } values suitable for ListPlot is to Splice the list produced by ReplaceAll into the surrounding list, thereby avoiding the problem of the extra set of braces in the first place:

etwo= Table[{b, Splice[λ /. 
    NSolve[{(c λ)/(c λ - 1) - k/c (c λ)^2 + 
        c^2 (μ (b - Bs)) == 0, λ >= 0},
     Reals]]}, {b, 101, Bs, 0.2}]

Short[etwo]

(*  {{101., 0.26994}, {101.2, 0.268292}, <<93>>, {120., 0.}} *)

(2)

ListPlot[etwo, Frame -> True, Joined -> True, GridLines -> Automatic, PlotRange -> All]

enter image description here

(3)

Short[energy]

{{101., {0.26994}}, {101.2, {0.268292}}, <<93>>, {120., {0.}}}

There are many ways to convert the list energy into a list of { {x,y}, ...} values (ie convert energy to etwo), including:

Flatten/@energy==etwo
Delete[#,{2,0}]&/@energy==etwo  (* delete the Head *)
Replace[energy, {x_,{y_}}-> {x,y},{1}] ==etwo 

( *  True
     True
     True *) 

ReplaceAll may also be used, but is risky with pattern matching and IMO is best avoided.

(4) Delete vs Flatten

Compare:

expt={{a1, {b1},{c1}},{a2,{b2},{c2}}}
Flatten/@expt
Delete[#,{2,0}]&/@expt

{{a1, {b1}, {c1}}, {a2, {b2}, {c2}}}
{{a1, b1, c1}, {a2, b2, c2}}
{{a1, b1, {c1}}, {a2, b2, {c2}}}
$\endgroup$
5
$\begingroup$

There are several ways to achieve your goal, e.g.

energy = Table[{b}~Join~
   (\[Lambda] /. 
     NSolve[{(c \[Lambda])/(c \[Lambda] - 1) - k/c (c \[Lambda])^2 + 
         c^2 (\[Mu] (b - Bs)) == 0, \[Lambda] >= 0}, Reals]),
 {b, 101, Bs, 0.2}]

enter image description here

you can also try Flatten to cope with extra curly brakets (namely, braces).

$\endgroup$
1
  • 1
    $\begingroup$ (+1) that's also very nice! $\endgroup$
    – bmf
    Commented Jan 14, 2023 at 10:55
4
$\begingroup$

Your equation can be solved exactly.

Clear["Global`*"]

Use exact input

c = -5526/100*5/100;
k = 904788*^-5;
Bs = 120;
μ = 15504*^-6;

sol = SolveValues[{(c λ)/(c λ - 1) - k/c (c λ)^2 + 
      c^2 (μ (b - Bs)) == 0, λ >= 0}, λ, Reals][[1]]

enter image description here

The exact solution is a ConditionalExpression containing a Root expression.

You can use ToRadicals to convert this Root expression to an explicit form

sol // ToRadicals // Simplify

enter image description here

Clearly, the Root expression is a more compact representation.

Plotting,

Plot[sol, {b, 37, Bs}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Nicely done. I did not even check for an exact solution....(sighs) :/ $\endgroup$
    – bmf
    Commented Jan 14, 2023 at 15:00

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