Problem in solving integration inside the for loop

for i=1:6
for j=1:6
f1=@(phi1)(cos(phi1)*i)+2*j;
F=integral(@(phi1)f1,0,1);
end
end
This is the small model of programme which I want to run. This code works if I remove @(phi1) symbol from the integral command, which is possible only for 1-D. But this need to be done for four variable, so I want to make it run in this scenario. Please suggest for solving four variable integral with an additinal 2-D array (i,j-mentioned here) or how to remove @ issue.

10 件のコメント

Torsten
Torsten 2021 年 7 月 17 日
編集済み: Torsten 2021 年 7 月 17 日
Does this help for a 2d-integral (it should be easy to generalize this concept to n-dimensional integrals) ?
f = @(x,y) x.^2 + y.^2;
xl = 0;
xr = 1;
yl = 0;
yr = 1;
F = integral(@(y) integral(@(x) f(x,y),xl,xr),yl,yr,'ArrayValued',true);
integrates f over the rectangle [xl,xr] x [yl,yr].
GAYTRI ARYA
GAYTRI ARYA 2021 年 7 月 17 日
This does not works for array, that is the main issue in MATLAB to tackle array with multidimensional integral.
Torsten
Torsten 2021 年 7 月 17 日
I don't understand what you mean with
This does not work for array
GAYTRI ARYA
GAYTRI ARYA 2021 年 7 月 17 日
Actually problem lies in solving the integration is getting complex due to presence of two variable which we dont want to integrate and we have range values for those two variables.
For e.g
F(x,y,r1,r2)= (x^2+y*p-r1^2+r2*q);
Here we want to integrate with respect to x and y within limits (0,1) and with respect to r1 and r2 within limits (0,pi). The p and q in this case are p=(0:0.01:1)=q, and we want F to be integrated over these ranges of p and q. So that we could plot F with p and q, similar to what we plot for transverse profile using meshgrid.
I am not able to handle p and q with 4-D integral. I hope my concern is now better clear to you. Thanks for attention.
Torsten
Torsten 2021 年 7 月 17 日
編集済み: Torsten 2021 年 7 月 17 日
Untested !
F = zeros(101,101);
for i=0:100
p = 0.01*i;
for j=0:100
q = 0.01*j;
f = @(x,y,r1,r2,p,q) x.^2 + y*p -r1.^2 + r2*q;
F(i+1,j+1) = integral(@(x) integral(@(y) integral (@(r1) integral(@(r2)f(x,y,r1,r2,p,q),0,pi,'ArrayValued',true), ...
0,pi ,'ArrayValued',true),0,1,'ArrayValued',true),0,1,'ArrayValued',true);
end
end
But be prepared that the code will most probably run endlessly, So I suggest you start with only one p-q combination.
GAYTRI ARYA
GAYTRI ARYA 2021 年 7 月 18 日
yeah that is true code is running endlessly, but anyways it is working. Thank you so much. Is there any way to reduce the time of processing. Thanks in advance.
Torsten
Torsten 2021 年 7 月 18 日
編集済み: Torsten 2021 年 7 月 18 日
Whether you can save time depends on the form of the function f.
Maybe some of the 4 integrations can be done analytically by hand or using Matlab's symbolic toolbox using the function "int".
Or you can try integralN from Mike Hosea from the file exchange if it exhibits better performance.
Scott MacKenzie
Scott MacKenzie 2021 年 7 月 18 日
@Torsten It seems you've come up with a good answer for this question. May I suggest you move it into the Answer section so @GAYTRI ARYA can accept it. My answer was clearly off base, so I will delete it.
GAYTRI ARYA
GAYTRI ARYA 2021 年 7 月 18 日
@Torsten Really thankful to you. The symbolic Math toolbox is amazing, I tested it for small expression but for 4_D integration and two array functions and it gives result almost immediately.
GAYTRI ARYA
GAYTRI ARYA 2021 年 7 月 18 日
@Scott MacKenzie Thankyou for the attention in resolving the issue as early as possible.

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

 採用された回答

Torsten
Torsten 2021 年 7 月 18 日

0 投票

Possible approaches to answer the question can be read in the discussion above.

その他の回答 (0 件)

カテゴリ

質問済み:

2021 年 7 月 17 日

回答済み:

2021 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by