Can I get the integral formula using Matlab?

16 views (last 30 days)
Haya M
Haya M on 24 Apr 2021
Answered: Walter Roberson on 23 Apr 2025
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 23 Apr 2025
Hello Haya,
The integral is very much defined for m=n. In that case you are integrating the function
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
which is a cubic zero when m-->n. So when m--> n the numerator also has to be reducible to a factor of (n-m)^3 so that it can lead to a (so far) undetermined 0/0 form. With
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.

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!