Mr Alpha
Mr Alpha

Reputation: 1843

Using the output of Solve

I had a math problem I solved like this:

In[1]:= Solve[2x(a-x)==0, x]
Out[1]= {{x->0}, {x->a}}

In[2]:= Integrate[2x(a-x), {x,0,a}]
Out[2]= (a^3)/3

In[3]:= Solve[(a^3)/3==a, a]
Out[3]= {{a->0}, {a->-Sqrt[3]}, {a->Sqrt[3]}}

My question is if I could rewrite this to compute it in one step, rather than having to manually input the result from the previous line. I could easily replace the integral used in step three with the Integrate command from step two. But what I can't figure out is how I would use the result from step 1 as the limits of integration in the integral.

Upvotes: 3

Views: 3019

Answers (2)

Sasha
Sasha

Reputation: 5954

If you agree to delegate the choice of the (positive oriented) domain to Integrate, by means of using Clip or Boole:

In[77]:= Solve[
 Integrate[
   Clip[2 x (a - x), {0, Infinity}], {x, -Infinity, Infinity}] == a, a]

Out[77]= {{a -> 0}, {a -> Sqrt[3]}}

or

In[81]:= Solve[
 Integrate[
   2 x (a - x) Boole[2 x (a - x) > 0], {x, -Infinity, Infinity}] == 
  a, a]

Out[81]= {{a -> 0}, {a -> Sqrt[3]}}

The reason only non-negative roots are found, is that Integrate will integrate from the smallest root to the largest root, i.e. from {x,0,a} for positive a and {x,a,0} for negative a.

Upvotes: 2

Heike
Heike

Reputation: 24420

You could combine step 1 and 2 by doing something like

Integrate[2 x (a - x), {x, ##}] & @@ (x /. Solve[2 x (a - x) == 0, x]);

Upvotes: 6

Related Questions