Can I get the integral formula using Matlab?

47 views (last 30 days)
Haya M
Haya M on 24 Apr 2021
Commented: David Goodmanson on 17 Jun 2025 at 23:29
I have the following integration:
and m and n are integers.
Can I get the formula for this integration using Matlab?
Here is my attempt:
clear all
syms x m n
f = (1/pi)*sin(n*x/2)*(pi-x)^2*sin(m*x/2);
Fint = int(f,x,[0 2*pi]);
I got this long result which is ok for me in case it is correct:
(5734161139222659*(8*m^3*cos(pi*m)*sin(pi*n) - 8*n^3*cos(pi*n)*sin(pi*m) - 8*pi*m*n^3 + 8*pi*m^3*n + 4*m^4*pi*sin(pi*m)*sin(pi*n) - 4*n^4*pi*sin(pi*m)*sin(pi*n) + 24*m*n^2*cos(pi*m)*sin(pi*n) - 24*m^2*n*cos(pi*n)*sin(pi*m) - m^5*pi^2*cos(pi*m)*sin(pi*n) + n^5*pi^2*cos(pi*n)*sin(pi*m) - m*n^4*pi^2*cos(pi*m)*sin(pi*n) + m^4*n*pi^2*cos(pi*n)*sin(pi*m) - 2*m^2*n^3*pi^2*cos(pi*n)*sin(pi*m) + 2*m^3*n^2*pi^2*cos(pi*m)*sin(pi*n) - 8*m*n^3*pi*cos(pi*m)*cos(pi*n) + 8*m^3*n*pi*cos(pi*m)*cos(pi*n)))/(9007199254740992*(m^2 - n^2)^3)
when I assume then the result is undefied..
I appreciate any help..
  2 Comments
Stephen23
Stephen23 on 22 Apr 2025
A much more accurate approach is to use SYM(PI):
syms x m n
f = (1/sym(pi))*sin(n*x/2)*(sym(pi)-x)^2*sin(m*x/2);
Fint = int(f,x,[0,2*pi])
Fint = 
"when I assume then the result is undefied.."
Because the integral is undefined for those values:
fsurf(Fint)
fsurf(Fint,[0.5,1.5])
subs(Fint,{m,n},{1,1})
Error using sym/subs (line 156)
Division by zero.
Paul
Paul on 23 Apr 2025
Hi Stephen,
In addition to using sym(pi),
syms x m n
f = (1/sym(pi))*sin(n*x/2)*(sym(pi)-x)^2*sin(m*x/2);
Fint = int(f,x,[0,2*sym(pi)])
Fint = 
One "feature" of int is that it (almost) never recognizes special cases of the parameters of the integrand. For the particular case at hand, if we set m = n = 1 first, then the integral follows
int(subs(f,[m,n],[1,1]),x,[0,2*sym(pi)])
ans = 
This result can also be obtained by taking the limit of Fint (though, TBH I'm not sure if this sequential limit approach is strictly correct)
simplify(limit(limit(Fint,n,1),m,1))
ans = 
If m and n are both integers (not stated by the OP, but might be the case based on the notation), then Fint simplifies further
assumeAlso([m,n],'integer');
simplify(Fint,100)
ans = 

Sign in to comment.

Answers (2)

David Goodmanson
David Goodmanson on 22 Apr 2025
Edited: David Goodmanson on 17 Jun 2025 at 18:43
Hello Haya,
The integral is very much defined for m=n. In that case you are integrating the function
(1/pi)*sin(n*x/2)^2*(x-pi)^2
which is well behaved and has a calculable integral. The syms expression contains a denominator
1/(m^2-n^2)^3
which has a factor of
1/(n-m)^3
and is a cubic zero when m-->n. When m--> n the numerator also must contain a factor of (n-m)^3 so that the entire expression will be a (yet to be determined) 0/0 form. To get the result you don't have to actually suss out the (n-m)^3 factor in the numerator. It's enough to know that it exists. Taking the m=n expression at the top above,
clear all
syms n x
assume(n,{'positive','integer'})
pi = sym('pi')
f = (1/pi)*sin(n*x/2)^2*(pi-x)^2;
Fint = int(f,x,[0 2*pi])
you obtain
Fint =
pi^2/3 - (n*(pi + pi*cos(2*n*pi)) - sin(2*n*pi) + (n^2*pi^2*sin(2*n*pi))/2)/(n^3*pi)
and after setting
sin(2*n*pi) = 0 cos(2*n*pi) = 1
(there must be a way to get syms to do this but I don't know what it is) this reduces to
Fint = pi^2/3 - 2/n^2
which doesn't work for n = 0, but that case can be ignored since the integrand is 0.
  2 Comments
Paul
Paul on 17 Jun 2025 at 23:22
Hi David,
clear all
syms n x
assume(n,{'positive','integer'})
Here, Pi1 is not the sympolic constant pi. Its just a symbolic variable that renders with the pi symbol.
Pi1 = sym('pi')
Pi1 = 
π
which is why, e.g., sin(Pi1) is not known.
sin(Pi1)
ans = 
f = (1/Pi1)*sin(n*x/2)^2*(Pi1-x)^2;
Fint = int(f,x,[0 2*Pi1])
Fint = 
Consequently, Fint doesn't simplify
simplify(Fint)
ans = 
Instead, define the symbolic constant this way
Pi2 = sym(pi)
Pi2 = 
π
Which is not the same as Pi1
Pi2 - Pi1
ans = 
sin(Pi2)
ans = 
0
Now we get the same-looking integral
f = (1/Pi2)*sin(n*x/2)^2*(Pi2-x)^2;
Fint = int(f,x,[0 2*Pi2])
Fint = 
but it simplifies as expected.
simplify(Fint)
ans = 
IIRC, sym('pi') used to return the symbolic constant pi, but that behavior changed a few releases ago.
David Goodmanson
David Goodmanson on 17 Jun 2025 at 23:28
Hi Paul, thanks for the explanation

Sign in to comment.


Walter Roberson
Walter Roberson on 23 Apr 2025
syms x m n
f = (1/sym(pi))*sin(n*x/2)*(pi-x)^2*sin(m*x/2);
Fint = int(f,x,[0 2*pi]);
limit(Fint, m, n)
ans = 
subs(ans, n, 1)
ans = 
simplify(ans)
ans = 

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!