Ask Your Question
0

how to use monte_carlo_integral ?

asked 2025-10-04 19:40:37 +0200

ortollj gravatar image

I can't use the monte_carlo_integral function, what am I doing wrong?

var('u,v')
r_uvN(u,v)=sqrt(abs(-(cos(u) + 2)*cos(v)^2*sin(u) - (cos(u) + 2)*sin(u)*sin(v)^2)^2 + 
           abs((cos(u) + 2)*cos(u)*cos(v))^2 + abs((cos(u) + 2)*cos(u)*sin(v))^2)
sS=integral(integral( r_uvN,(u,0,2*pi)),(v,0,2*pi) )
print('s symbolic : ',sS,' num : ',sS.n(20) )

f(u, v)= u * v
res=monte_carlo_integral(f, [0,0], [2,2], 10000)  # abs tol 0.1
print(' with f  :',res)

sNL=monte_carlo_integral(r_uvN, [0,2*pi], [0,2*pi], 10000)
print('with r_uvN numerical ?? : ',sNL)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2025-10-04 21:08:35 +0200

ortollj gravatar image

ok I just asked the Copilot AI, Copilot gives me the answer:⚠️ Likely Cause: r_uvN is a symbolic expression, not a callable function The monte_carlo_integral function expects a Python callable—something like lambda u,v: ... or a def-defined function. But r_uvN(u, v) is a symbolic expression, and Sage doesn’t automatically convert it into a callable for GSL-based numerical integration.

That’s why your result is near-zero: the integrator is likely sampling r_uvN as a constant or failing silently to evaluate it properly. try this:

f_uv = lambda u, v: r_uvN(u, v).n()
sNL = monte_carlo_integral(f_uv, [0, 0], [2*pi, 2*pi], 10000)

and it gives :(79.04914441964019, 0.27982586027289974) which is ok

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2025-10-04 19:40:37 +0200

Seen: 4 times

Last updated: 1 hour ago