Python integrals
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开发者_如何学Go 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.
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!
The problem is that Python sees (1/2)
and evaluates it with integer division, yielding zero.
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.
精彩评论