       Re: using a previous result

• To: mathgroup at smc.vnet.net
• Subject: [mg67354] Re: [mg67324] using a previous result
• From: "Chris Chiasson" <chris at chiasson.name>
• Date: Mon, 19 Jun 2006 00:01:25 -0400 (EDT)
• References: <200606180913.FAA03229@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```I wrote you two solutions that demonstrate a lot you might want to

The first is the way a person would normally code the solution. It has
several features. Functional notation (q vs q1) is used so that
formulas can be written once and then applied multiple times via Map
(/@). Patterns are used to enable application of automatic rules
created by Set (=) and SetDelayed (:=) to different expressions
(eqn for instance). Set after SetDelayed causes the value of
certain computations to be remembered (eqn for instance), instead
of recomputed for each call. The result of the Solve command is also a
set of rules (Rule or ->) which may be used with ReplaceAll (/.), but
they are not used automatically like Set or SetDelayed.

The second solution shows how Mathematica will parse (due to order of
operations) the infix (/@ = := /. ->) and postfix ( [] ) operator
forms in the first solution.

As a side note:
Multiple clicks of the mouse on brackets, operators, function names,
technique is also good for reading long outputs.

q=a+b p+g p
q=c+d p+e p
profit[i_]=p[i] q[i]
eqn[i_]:=eqn[i]=D[profit[i],p[i]]\[Equal]0
sol=Solve[eqn/@Range,p/@Range]
pstar[i_]:=pstar[i]=p[i]/.sol[]
pstar/@Range

Set[q,Plus[a,Times[b,p],Times[g,p]]]
Set[q,Plus[c,Times[d,p],Times[e,p]]]
Set[profit[Pattern[i,Blank[]]],Times[p[i],q[i]]]
SetDelayed[eqn[Pattern[i,Blank[]]],Set[eqn[i],D[profit[i],p[i]]\[Equal]0]]
Set[sol,Solve[Map[eqn,Range],Map[p,Range]]]
SetDelayed[pstar[Pattern[i,Blank[]]],
Set[pstar[i],ReplaceAll[p[i],Part[sol,1]]]]
Map[pstar,Range]

On 6/18/06, ridley_david at yahoo.com <ridley_david at yahoo.com> wrote:
> Once I've solved a system of equations, I would like to use the
> previous results in future equations. If my output is {{p1->x,p2->y}}
> how do I assign those values as p1star and p2star so I can reuse them?
>
> Thank you,
> David
>
> For example:
> q1=a+b p1 +g p2;q2 = c+d p2 + e p1;profit1=p1 q1;profit2=p2 q2;
> FullSimplify[Solve[{D[profit1,p1]==0,D[profit2,p2]==0},{p1,p2}]]
>
>

--
http://chris.chiasson.name/

```

• Prev by Date: Re: matrix substitution--> Gell-Mann su(3) as a real 6by6 matrix group by substitution
• Next by Date: Sturm-Liouville (eigenvalue/eigenfunction) problems
• Previous by thread: using a previous result
• Next by thread: Re: using a previous result