2

I want to know how we could use the quantities we found by solving simultaneous equations, directly into plotting graphs. For example I have this Mathematica code; it gives the values of a and b but does not plot.

Clear[a, b] 
Solve[{a + b == 30, 2*a + 3*b == 50}, {a, b}]
Plot[a*x + b*x, {x, 0, 10}]
JungHwan Min
  • 4,664
  • 1
  • 22
  • 36
Ajib Paudel
  • 109
  • 6

2 Answers2

2

You'll want to do

sol = First@Solve[{a+b==20,2a+3b==50},{a,b}];
Plot[a x + b x/.sol,{x,0,10}]

Solve gives you a list of Rules. Here, there's just one such rule, but it's still wrapped in an "extra" set of braces because It's a list of rules. Use First@ (equivalent to wrapping the expression in First[]) to get the first element of that list, which is your desired Rule.

Using /. just lets you plug the rule from 'sol' into your equation; i.e. a x + b x /. sol = a x + b x /. {a->10,b->10} = 10x + 10x = 20x, which you can Plot.

Ben Kalziqi
  • 1,082
  • 8
  • 17
  • Thanks a lot Ben, it really works. Actually it was for a bigger project than that and really helpful, saved a lot of work for me. – Ajib Paudel Jun 16 '16 at 18:05
  • Happy to help. Remember to search the documentation; it's extremely helpful! Be sure to read through this as well, as @Michael E2 suggested. – Ben Kalziqi Jun 16 '16 at 18:09
2
sol = Solve[{a + b == 20, 2 a + 3 b == 50}, {a, b}]

{{a -> 10, b -> 10}}

See ReplaceAll (/.) and Applying Transformation Rules

point = {a, b} /. sol

{{10, 10}}

ContourPlot[{a + b == 20, 2 a + 3 b == 50}, {a, 0, 15}, {b, 0, 15}, 
Epilog -> {Red, PointSize[Large], Point@point}]

enter image description here