2

I'm trying to solve the integral of (2**(1/2)*y**(1/2)/2)**2 from 0 to 5 (also shown here). I've been using

func = lambda y: (  2**(1/2) * y**(1/2)/2  )**2 and a == 0 and b == 5
from scipy import integrate
integrate.quad(func, a b)

For some reason, I keep getting the value 1.25, while wolfram says it should be 6.25? I can't seem to put my finger on the error.

p.s. sorry for the error katrie, i forgot that python uses and not && for logical AND


SOLVED: this was a silly int/float error. thank you everyone.

Nona Urbiz
  • 4,873
  • 16
  • 57
  • 84
  • 2
    Which version of Python are you using? In many versions of Python, `1/2` will evaluate to `0`. Also, your WolframAlpha link shows the answer for the integral of `y/2` between 0 and 5. – dheerosaur Dec 15 '10 at 04:18
  • Also, you do know that the function you've written above is just `y/2`, right? You're squaring a square root... – Katriel Dec 15 '10 at 04:20
  • the quad function takes the bounds as arguments, however, your worry about the values as integers may be the key. katrie, yes I do, that's a coincidence in this example. thanks though. – Nona Urbiz Dec 15 '10 at 04:20
  • 1
    Python uses `and` for logical and, `&` is bitwise. But why is that in the function you're trying to integrate, anyway? I can't reproduce your bug, btw... `integrate` works just fine for me. – Katriel Dec 15 '10 at 04:22
  • Oh, apologies, did I break your code when I reformatted it? If so oops =) sorry. – Katriel Dec 15 '10 at 04:26
  • heh. you're right again, i just wrote that off the cuff to give background to my question, i should have been more careful. Actually at this point I've confirmed that the problem is that they are integers not floats. The equation was generated by `sympy.Eq(func, var)`, so i'm gonna have to figure out a way to force that to all floats or something i'm not really sure, but that's not really in the scope of this question. thanks again. – Nona Urbiz Dec 15 '10 at 04:26

3 Answers3

3

Well, let me write your function in normal mathematical notation (I can't think in Python). I don't like **, as it gets confusing:

(2**(1/2)*y**(1/2)/2)**2      =>
(2^(1/2) * (1/2) * y^(1/2))^2 =>
2 * (1/4) * y                 =>
y / 2

So to integrate, antidifferentiate (I'm just thinking aloud):

antidifferentiate(y / 2) = y^2 / 4

Therefore

integral('y / 2', 0, 5) =
5^2 / 4 - 0^2 / 4 =
25 / 4 = 
6.25

Right. Have you tried replacing 1/2 with 0.5? It could be interpreted as the quotient of two integers, which is rounded up.

Try this (as the others have suggested):

func = lambda y: (2**(0.5) * y**(0.5) / 2.0)**2.0 & a == 0 & b == 5

from scipy import integrate
integrate.quad(func, a b) # What's 'a b'? Maybe 'a, b' would work?

Good luck!

Blender
  • 289,723
  • 53
  • 439
  • 496
  • The problem was just the integers. thanks for taking the time to write all that! I have to ask, are you involved with Blender somehow? – Nona Urbiz Dec 15 '10 at 06:10
  • I am quite involved (as a user, not as a developer). I'm still resurrecting BlenderOS, as that failed years ago. – Blender Dec 15 '10 at 06:14
  • Also, no problem. Honors Calculus finals are this week, so I integrate in my sleep ;) – Blender Dec 15 '10 at 06:25
2

The problem is that Python sees (1/2) and evaluates it with integer division, yielding zero.

Gabe
  • 84,912
  • 12
  • 139
  • 238
0

Well what's the value supposed to be? Also, you should put more parenthesis in your equation. I have no idea what (2**(1/2)*y**(1/2)/2)**2 gets parsed out to be.

Falmarri
  • 47,727
  • 41
  • 151
  • 191