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.

6
  • 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. Commented Dec 15, 2010 at 4:18
  • Also, you do know that the function you've written above is just y/2, right? You're squaring a square root... Commented Dec 15, 2010 at 4: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. Commented Dec 15, 2010 at 4: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. Commented Dec 15, 2010 at 4:22
  • Oh, apologies, did I break your code when I reformatted it? If so oops =) sorry. Commented Dec 15, 2010 at 4:26

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

3 Comments

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?
I am quite involved (as a user, not as a developer). I'm still resurrecting BlenderOS, as that failed years ago.
Also, no problem. Honors Calculus finals are this week, so I integrate in my sleep ;)
2

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

Comments

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.

4 Comments

sorry, thats why I linked to wolframalpha, it should be 6.25. p.s. added some spacing to OP
That may be how wolfram is parsing it, but is that how python is parsing it?
yes, python actually solved for x from y = 2*x**2, and it checked out with wolfram
Why have you posted this as an answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.