MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

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
learn about Mathematica.

The first is the way a person would normally code the solution. It has
several features. Functional notation (q[1] 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[1] for instance). Set after SetDelayed causes the value of
certain computations to be remembered (eqn[1] 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 ( [[1]] ) operator
forms in the first solution.

As a side note:
Multiple clicks of the mouse on brackets, operators, function names,
etc will greatly help you see how Mathematica parses expressions. This
technique is also good for reading long outputs.

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

Set[q[1],Plus[a,Times[b,p[1]],Times[g,p[2]]]]
Set[q[2],Plus[c,Times[d,p[2]],Times[e,p[1]]]]
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[1],Solve[Map[eqn,Range[2]],Map[p,Range[2]]]]
SetDelayed[pstar[Pattern[i,Blank[]]],
  Set[pstar[i],ReplaceAll[p[i],Part[sol[1],1]]]]
Map[pstar,Range[2]]

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