# Can integral2 left a variable inside?

56 views (last 30 days)
Guan Hao on 21 Jul 2024 at 5:12
Commented: Guan Hao on 23 Jul 2024 at 10:08
Hi,everyone.There're three variables in my function,x,y and w.
I want to integral x and y first,but there were a variable w stuck in my function.Can I left it and integral x,y first? Thank you.
clear
L=0.1;
section=50;
a=L/2;
b=L/section;
v=3e8;
f1=1.2e9;
f2=4.8e9;
f3=7.5e9;
f4=10e9;
w1=(2*pi*f1);
w2=(2*pi*f2);
w3=(2*pi*f3);
w4=(2*pi*f4);
Z01=50;
Z02=75;
a0=(log(Z02/Z01))./(2.*L);
T=a;
Ub=a;
Lb=-a;
ub=a;
lb=-a;
k=6;
syms x y w
l=0:(2*L)/(2*k):L % Approximate sections dvided
sec=numel(l)-1;
% Cm0
for s=1:1:sec
for t=1:1:sec
for m=1:1:k
P1=matlabFunction(((cos((m.*pi.*(y))./a)).*(cos(2.*(x-y).*w./v))));
P2(1,m)=integral2(P1,Lb+l(s),Lb+l(s+1),Lb+l(t),@(x)x);
P3(1,m)=integral2(P1,Lb+l(s),Lb+l(s+1),@(x)x,Lb+l(t+1));
P{s,t}=(P2+P3) % s section multiply t section
end
end
Umar on 21 Jul 2024 at 8:20
Edited: Walter Roberson on 21 Jul 2024 at 19:07
Hi Guan,
That was a very good question. When dealing with multiple variables in an integration scenario, it is crucial to ensure that all variables are appropriately handled to achieve accurate results. In this case, the variable w appears in the integrand function, which might complicate the integration process if left unaddressed.To address the issue of the stuck variable w, one approach could involve separating the integration steps for x and y from the part of the code that involves w. By isolating the integrations for x and y, you can focus on resolving any dependencies on w separately.
Here is a modified version of the code snippet that separates the integration of x and y from the part involving w:
% Define the integrand function without the variable w
P1 = @(x, y) cos((m * pi * y) / a) * cos(2 * (x - y) / v);
% Perform integration for x and y separately
int_x = integral2(@(x, y) P1(x, y), Lb + l(s), Lb + l(s + 1), Lb + l(t), Lb + l(t + 1));
int_y = integral2(@(x, y) P1(x, y), Lb + l(s), Lb + l(s + 1), Lb + l(t), Lb + l(t + 1));
% Proceed with further computations using int_x and int_y
Please let me know if you have any further questions.
Guan Hao on 21 Jul 2024 at 9:22
Yeah,it works.However, I was confused if I can just neglect w, since it was inside the cosine function.

Torsten on 21 Jul 2024 at 9:51
Moved: Walter Roberson on 21 Jul 2024 at 20:23
You cannot simply remove the w in your expression as suggested by @Umar. I'd do the general integration first and then substitute x and y according to the bounds you specified. The result will be symbolic expressions in w, not simple numbers.
L = 0.1;
a = L/2;
v = 3e8;
m = 3;
syms x y w real
expr = cos(m*sym(pi)*y/a).*cos(2*(x-y)*w/v)
expr =
int(int(expr,x),y)
ans =
##### 3 CommentsShow 1 older commentHide 1 older comment
Torsten on 23 Jul 2024 at 9:52
Edited: Torsten on 23 Jul 2024 at 9:53
How can the integration results be "numbers" if w is an unspecified variable in the integrand ? integral3 would not help in this case, either.
Guan Hao on 23 Jul 2024 at 10:08
@Torsten Because I got about 10 other functions of w to integrte . If I use integral3 to run,it's too slow.
That's why I want to integrate x and y first, and save w to the last.

Walter Roberson on 21 Jul 2024 at 20:25
I want to integral x and y first,but there were a variable w stuck in my function.Can I left it and integral x,y first?
No, integral2() is strictly numeric. Every variable involved must have a numeric value (or be one of the two named parameters.)
Symbolic integration is the way to go for this task.
Guan Hao on 23 Jul 2024 at 4:27
@Walter Roberson Ok, thank you~