integral2 for a non-scalar matlabFunction

I'm trying to use integral2 for a non-scalar matlab function. In the following I define 3 symbolic functions, put 5 set of data for (y1, y2 and y3) , which in total makes it 15 functions. I convert them into anonymous function with "matlabFunction" so that I could use the integral2. The problem is integral2 is not working with this output. Is there a way around it ? The purpose is to vectorized the operation of integration to speed up the code.
syms zi eta
syms N1 N2 N3
syms y_1 y_2 y_3
N1=1-zi-eta;
N2=zi;
N3=eta;
N=[N1,N2,N3];
for i=1:3
cons_int(i)=N(i)*sinh(y_1.*N1+y_2.*N2+y_3.*N3);
end
y_1_val=[1:5]';
y_2_val=[1:5]';
y_3_val=[1:5]';
intagrand_temp=subs(cons_int,{y_1, y_2, y_3},{y_1_val,y_2_val,y_3_val});
if ~isempty(intagrand_temp)
intagrand_1 = matlabFunction(intagrand_temp);
zi_max = @(eta) 1 - eta;
q_cons = integral2(intagrand_1,0,1,0,zi_max);
end
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.

Error in symengine>@(eta,zi)reshape([-sinh(1.0).*(eta+zi-1.0),-sinh(2.0).*(eta+zi-1.0),-sinh(3.0).*(eta+zi-1.0),-sinh(4.0).*(eta+zi-1.0),-sinh(5.0).*(eta+zi-1.0),zi.*sinh(1.0),zi.*sinh(2.0),zi.*sinh(3.0),zi.*sinh(4.0),zi.*sinh(5.0),eta.*sinh(1.0),eta.*sinh(2.0),eta.*sinh(3.0),eta.*sinh(4.0),eta.*sinh(5.0)],[5,3])

Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;

Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);

Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);

Error in integral2 (line 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);

回答 (1 件)

Torsten
Torsten 2023 年 7 月 16 日
移動済み: Torsten 2023 年 7 月 16 日

1 投票

"intagrand_temp" is a 5x3 matrix of symbolic functions in eta and zi. I don't understand which scalar-valued function(s) you want to integrate. Each of the 15 functions one by one to get a 5x3 matrix as result ?
Maybe like this ?
syms zi eta
syms N1 N2 N3
syms y_1 y_2 y_3
N1=1-zi-eta;
N2=zi;
N3=eta;
N=[N1,N2,N3];
for i=1:3
cons_int(i)=N(i)*sinh(y_1.*N1+y_2.*N2+y_3.*N3);
end
y_1_val=[1:5]';
y_2_val=[1:5]';
y_3_val=[1:5]';
intagrand_temp=subs(cons_int,{y_1, y_2, y_3},{y_1_val,y_2_val,y_3_val})
intagrand_temp = 
q_cons = int(int(intagrand_temp,zi,0,1-eta),eta,0,1)
q_cons = 

6 件のコメント

Mojtaba Norouzisadeh
Mojtaba Norouzisadeh 2023 年 7 月 16 日
編集済み: Mojtaba Norouzisadeh 2023 年 7 月 16 日
yes, that's technically what I want. However I'm going to need to integrate it numerically since the actual funcion that I'm going to use is more complex and is not easily integratable.
Torsten
Torsten 2023 年 7 月 16 日
編集済み: Torsten 2023 年 7 月 16 日
syms zi eta
syms N1 N2 N3
syms y_1 y_2 y_3
N1=1-zi-eta;
N2=zi;
N3=eta;
N=[N1,N2,N3];
for i=1:3
cons_int(i)=N(i)*sinh(y_1.*N1+y_2.*N2+y_3.*N3);
end
y_1_val=[1:5]';
y_2_val=[1:5]';
y_3_val=[1:5]';
intagrand_temp=subs(cons_int,{y_1, y_2, y_3},{y_1_val,y_2_val,y_3_val});
if ~isempty(intagrand_temp)
zi_max = @(eta) 1 - eta;
for i = 1:size(intagrand_temp,1)
for j = 1:size(intagrand_temp,2)
intagrand_1 = matlabFunction(intagrand_temp(i,j),'Vars',{eta,zi});
q_cons(i,j) = integral2(intagrand_1,0,1,0,zi_max);
end
end
end
q_cons
q_cons = 5×3
0.1959 0.1959 0.1959 0.6045 0.6045 0.6045 1.6696 1.6696 1.6696 4.5483 4.5483 4.5483 12.3672 12.3672 12.3672
double(int(int(intagrand_temp,zi,0,1-eta),eta,0,1))
ans = 5×3
0.1959 0.1959 0.1959 0.6045 0.6045 0.6045 1.6696 1.6696 1.6696 4.5483 4.5483 4.5483 12.3672 12.3672 12.3672
Mojtaba Norouzisadeh
Mojtaba Norouzisadeh 2023 年 7 月 16 日
Is there anyway to avoid the for loop? I was thinking if I could use a vectorize system I could speed it up.
Torsten
Torsten 2023 年 7 月 16 日
編集済み: Torsten 2023 年 7 月 16 日
Is there anyway to avoid the for loop?
For "integral": yes, for "integral2": no.
Mojtaba Norouzisadeh
Mojtaba Norouzisadeh 2023 年 7 月 16 日
Thanks for the answer. I think my other option is to use parfor loop.
Torsten
Torsten 2023 年 7 月 16 日
編集済み: Torsten 2023 年 7 月 16 日
Yes, that's true. These are 15 independent integrations that should be possible to be run in parallel.

サインインしてコメントする。

質問済み:

2023 年 7 月 16 日

編集済み:

2023 年 7 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by