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
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
2021년 7월 17일
Torsten
2021년 7월 17일
I don't understand what you mean with
This does not work for array
GAYTRI ARYA
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
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
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
2021년 7월 18일
GAYTRI ARYA
2021년 7월 18일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!