Problem in solving integration inside the for loop
Show older comments
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 Comments
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
on 17 Jul 2021
Torsten
on 17 Jul 2021
I don't understand what you mean with
This does not work for array
GAYTRI ARYA
on 17 Jul 2021
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
on 18 Jul 2021
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
on 18 Jul 2021
@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
on 18 Jul 2021
GAYTRI ARYA
on 18 Jul 2021
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!