Symbolic integration: error

3 views (last 30 days)
Simone
Simone on 10 Jun 2025
Edited: Torsten on 11 Jun 2025
Hi to all,
I have been trying to write a piece of code to avoid doing some calculations: the purpose is to calculate the integral of a given function and the integrale of the norm squared of its gradient on a shape that resembles a disk with a point, all of which depend on a parameter r.
For the first test, I did the calculations by hand and everything worked. Then I tried to automate the process of calculating the gradient and its norm squared in two different ways, and in both of them the functions are calcukated but the program fails to calculate the integral.
I've attached both the wrking and non-working tests (sorry for the long lines, but the shape and the integral are not easy to express).
Thank you in advance!
%test1 working
syms x y r
u(x,y,r)=((x-1+r)^2+y^2-r^2)*(y^2-r^2/(1-2*r)*x^2);
du2(x,y,r)=(y^2-r^2/(1-2*r)*x^2)^2*(4*(x-1+r)^2+4*y^2)+((x-1+r)^2+y^2-r^2)^2*(4*(r/sqrt(1-2*r))^4*x^2+4*y^2)-8*r^2/(1-2*r)*x*(x-1+r)*(y^2-r^2/(1-2*r)*x^2)*((x-1+r)^2+y^2-r^2)+8*y^2*(y^2-r^2/(1-2*r)*x^2)*((x-1+r)^2+y^2-r^2);
intu= int( int(u,y,-sqrt(r^2-(x-1+r)^2),sqrt(r^2-(x-1+r)^2)), x, (1-2*r)/(1-r), 1) + int( int(u,y,-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x), x, 0, (1-2*r)/(1-r));
intdu2= int( int(du2,y,-sqrt(r^2-(x-1+r)^2),sqrt(r^2-(x-1+r)^2)), x, (1-2*r)/(1-r), 1) + int( int(du2,y,-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x), x, 0, (1-2*r)/(1-r));
%test 2 not working (first way)
syms x y r
u(x,y,r)=((x-1+r)^2+y^2-r^2)*(y^2-r^2/(1-2*r)*x^2);
du(x,y,r)=gradient(u, [x,y]);
du_body=formula(du);
du2(x,y,r)=du_body(1)^2+du_body(2)^2;
intu= int( int(u,y,-sqrt(r^2-(x-1+r)^2),sqrt(r^2-(x-1+r)^2)), x, (1-2*r)/(1-r), 1) + int( int(u,y,-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x), x, 0, (1-2*r)/(1-r));
intdu2= int( int(du2,y,-sqrt(r^2-(x-1+r)^2),sqrt(r^2-(x-1+r)^2)), x, (1-2*r)/(1-r), 1) + int( int(du2,y,-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x), x, 0, (1-2*r)/(1-r));
%test2 not working (second way)
syms x y r
u(x,y,r)=((x-1+r)^2+y^2-r^2)*(y^2-r^2/(1-2*r)*x^2);
du(x,y,r)=gradient(u, [x,y]);
du2(x,y,r)=(norm(du))^2;
intu= int( int(u,y,-sqrt(r^2-(x-1+r)^2),sqrt(r^2-(x-1+r)^2)), x, (1-2*r)/(1-r), 1) + int( int(u,y,-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x), x, 0, (1-2*r)/(1-r));
intdu2= int( int(du2,y,-sqrt(r^2-(x-1+r)^2),sqrt(r^2-(x-1+r)^2)), x, (1-2*r)/(1-r), 1) + int( int(du2,y,-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x), x, 0, (1-2*r)/(1-r));
  1 Comment
Umar
Umar on 11 Jun 2025

To resolve the issues with your integral calculations in MATLAB, it is essential to ensure that the gradient is computed correctly and that the resulting expressions are properly formatted for integration. In your second test, the use of gradient may not yield the expected symbolic output, which can lead to problems when calculating the norm squared.

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 10 Jun 2025
I ran your code in MATLAB Online.
There are some integrands that simply cannot be integrated and give closed-form symbolic results. Yours seem to be among those. One option may be to perhaps do trigonometric substitution (if that is appropriate, since I am not certain what you are doing), and then integrate. If you have actual numeric values instead of symbolic variables, either use integral2 or if you want to stay with the Symbolic Math Toolbos, the vpaintegral function instead of int.
.
  10 Comments
Simone
Simone on 11 Jun 2025
Honestly, I hadn't thought about it, but I'd rather not have a discretization of the values I'm looking for...
Torsten
Torsten on 11 Jun 2025
I hadn't thought about it, but I'd rather not have a discretization of the values I'm looking for...
If "int" doesn't succeed in computing the antiderivatives, it will use a numerical approach for plotting anyway. And the numerical functions are very much faster than the symbolic ones. So if I were you, I'd give it a try.

Sign in to comment.


Torsten
Torsten on 11 Jun 2025
Edited: Torsten on 11 Jun 2025
Here is one possible implementation:
syms x y r
u = ((x-1+r)^2+y^2-r^2)*(y^2-r^2/(1-2*r)*x^2);
du = gradient(u, [x,y]);
du2 = du(1)^2+du(2)^2;
u = matlabFunction(u);
du2 = matlabFunction(du2);
r = 0.45;
fun1 = @(r,x)integral(@(y)u(r,x,y),-sqrt(r^2-(x-1+r).^2),sqrt(r^2-(x-1+r).^2));
result1 = integral(@(x)fun1(r,x),(1-2*r)/(1-r),1,'ArrayValued',1);
fun2 = @(r,x)integral(@(y)u(r,x,y),-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x);
result2 = integral(@(x)fun2(r,x),0,(1-2*r)/(1-r),'ArrayValued',1);
result = result1 + result2
result = 0.0417
fun1 = @(r,x)integral(@(y)du2(r,x,y),-sqrt(r^2-(x-1+r).^2),sqrt(r^2-(x-1+r).^2));
result1 = integral(@(x)fun1(r,x),(1-2*r)/(1-r),1,'ArrayValued',1);
fun2 = @(r,x)integral(@(y)du2(r,x,y),-r/sqrt(1-2*r)*x,r/sqrt(1-2*r)*x);
result2 = integral(@(x)fun2(r,x),0,(1-2*r)/(1-r),'ArrayValued',1);
result = result1 + result2
result = 0.2009

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!