On Aug 27, 8:45 am, Ondrej Certik <[email protected]> wrote:
> On Wed, Aug 26, 2009 at 2:03 AM, brandyn<[email protected]> wrote:
> > But I would like to be able to double-check this with sympy. (Even if
> > sympy can't figure out this solution on its own, it would be quite
> > nice if I could walk it through the steps and sympy could just make
> > sure I've made no errors.)
>
This a way you might just use it to help in your thinking process:
>>> var('a xi yi')
(a, xi, yi)
>>> ei=(a*xi+(1-a)*yi)**2
>>> #
>>> #derivative of sum is the sum of the derivatives (of the terms)
>>> #so let's see what those derivatives are:
>>> #
>>> deida = ei.diff(a);deida
(-2*yi + 2*xi)*(a*xi + yi*(1 - a))
>>> #
>>> #nothing can be factored out of the sum of these i-dependent products.
>>> #let's expand and see where 'a' goes:
>>> #
>>> collect(deida.expand(),a)
a*(-4*xi*yi + 2*xi**2 + 2*yi**2) + 2*xi*yi - 2*yi**2
>>> #
>>> #ahh...so we have a 3 term sum, only one of which depends on 'a' and it
>>> #is a multiplicative factor on that term so it can be factored out of the
>>> sum
>>> #so our SUM is a sum of sums looking like SUM = a*sum1 + sum2 - sum3
>>> #we don't want sympy to do it's normal simplifications on these terms
>>> #because these are sums, but we can have it help with what we've found:
>>> #
>>> var('sum1 sum2 sum3');sum = a*sum1 + sum2 - sum3
(sum1, sum2, sum3)
>>> solve(Eq(sum,0),a)
[(sum3 - sum2)/sum1]
>>> #
>>> #simplify the num and denom
>>> #
>>> sum2,sum3=2*xi*yi , 2*yi**2 #copied and pasted from above
>>> sum1=(-4*xi*yi + 2*xi**2 + 2*yi**2)
>>> factor(sum2-sum3)
-2*yi*(yi - xi)
>>> factor(sum1)
2*(yi - xi)**2
>>> #
>>> #the 2's will factor out of the sums and cancel to leave
>>> #
>>> # a = -sum{yi*(yi - xi)}/sum{(yi - xi)**2}
>>> #
>>> # ==> you were right :-)
>>> #
/c
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---