2
$\begingroup$

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}]
$\endgroup$
3
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) 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. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Jun 16, 2016 at 17:53
  • 1
    $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this meta Q&A helpful $\endgroup$
    – Michael E2
    Jun 16, 2016 at 17:53
  • 1
    $\begingroup$ See (18393, ans. 18706) for advice. See also (3175), (6669). $\endgroup$
    – Michael E2
    Jun 16, 2016 at 17:56

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Jun 16, 2016 at 18:05
  • $\begingroup$ Happy to help. Remember to search the documentation; it's extremely helpful! Be sure to read through this as well, as @Michael E2 suggested. $\endgroup$ Jun 16, 2016 at 18:09
2
$\begingroup$
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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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