Hi all,
I want to calculate symbolically area of a surface of a sphere.
y = f(x) = \sqrt{r^{2} - x^{2}} with -r \le x \le r
this is my code:
using SymPy
x, r = symbols("x r")
f(x) = sqrt(r^2 - x^2)
# simplify(diff(f(x),x))
# we cannot use diff inside the fd(x) since QuadGK can't comprehend diff
fd(x) = -x/(sqrt(r^2 - x^2))
g(x) = sqrt(1 + (fd(x))^2)
h(x) = f(x)*g(x)
Area(x) = 2pi*h(x)
d = integrate(Area(x), (x, -r,r))
println("Area = ", d)
the problem is the final result should be: 4 \pi r^{2}
what I get is the unfinished simplify
after 60 minutes of waiting… and the form of integral still:
mkitti
2
You need to add some assumptions.
See Core - SymPy 1.12.dev documentation
In particular r
should be nonnegative
.
1 Like
Great !
Now I am trying this:
using SymPy
x = symbols("x")
r = symbols("r", nonnegative=True)
# check by typing r.is_nonnegative
f(x) = sqrt(r^2 - x^2)
# simplify(diff(f(x),x))
# we cannot use diff inside the fd(x) since QuadGK can't comprehend diff
fd(x) = -x/(sqrt(r^2 - x^2))
g(x) = sqrt(1 + (fd(x))^2)
h(x) = f(x)*g(x)
Area(x) = 2pi*h(x)
d = simplify(integrate(Area(x), (x, -r,r)))
by tomorrow (if it takes 24 hours and still not finished yet, I will tell)…
the above code that took 60 minutes still not done yet and it is already 2 hours…
The answer is very numerics:
12.5663706143592⋅r^{2}
Use PI, not pi. When you compute 2pi it converts to a float.
1 Like
To elaborate, it also really helps to call simplify
prior to computing:
julia> @time ex = integrate(2PI*simplify(h(x)), (x, -r, r))
0.043363 seconds (13.71 k allocations: 715.979 KiB, 9.07% compilation time)
2
4⋅π⋅r
1 Like
Yes it works and produce the result faster than integrate first then simplify… thank you very much!