need to use double quad function for four integration with four unknowns

3 views (last 30 days)
I need to write the correct command that enables me to use the "dblquad" function to solve four integrations with four different variables.
for example: yy = dblquad(....dblquad(@myfun,0,2*pi,0,2*pi))
now the funtion myfun contains phi1,phi2,phi3,phi4 four different variables.
The command line I wrote is not yet complete and I am not sure if it is correct or not.
The myfun should it be then written as follow y = myfun(phi1,phi2,phi3,phi4)
Please I need an answer ASAP
Thank you
  2 Comments
Mohamed
Mohamed on 23 Sep 2012
I really appreciate it Mike, things are working great now. Thank you so much. I was able to get it to work exactly as you mentioned. I was using this to simulate a 2 dimension problem. I am upgrading my code to a 3D one. I have 8 nested integrals and are all function of phi. Ecatly as the one I described earlier but this time I have 8 integrals instead of 4. I asked the University here to purchase Matlab R 2012b. I need to know the command line to use to run the 8 integrals using "integral2" funtion.
Thank you
Mike Hosea
Mike Hosea on 23 Sep 2012
I rather doubt 8 nested integrals is practical. You might want to look into sparse grid methods, such as can be found at http://sparse-grids.de/ . Your integrand will need to be well-behaved.

Sign in to comment.

Accepted Answer

Mike Hosea
Mike Hosea on 17 Sep 2012
Edited: Mike Hosea on 17 Sep 2012
You should be using INTEGRAL2, but the solution is almost the same.
Using INTEGRAL2:
integral2(@(u,v)arrayfun(@(z,w)integral2(@(x,y)myfun(x,y,z,w),0,2*pi,0,2*pi),u,v),0,2*pi,0,2*pi)
Using DBLQUAD
dblquad(@(u,v)arrayfun(@(z,w)dblquad(@(x,y)myfun(x,y,z,w),0,2*pi,0,2*pi),u,v*ones(size(u))),0,2*pi,0,2*pi)
The INTEGRAL2 way finished in a couple of seconds with the example I gave it, DBLQUAD took 2852 seconds.
You can use QUAD2D instead of INTEGRAL2 if you don't have R2012a. If you do have R2012a, you can also do this
integral(@(x)integral3(@(y,z,w)myfun(x,y,z,w),0,2*pi,0,2*pi,0,2*pi),0,2*pi,'ArrayValued',true)
-- Mike
  4 Comments
tida
tida on 4 Oct 2012
I am also have the same problem to solve 4 integrals using this dblquad. Could you please explain more about this arrayfun. I mean what is in this function thanx Tida
Mike Hosea
Mike Hosea on 8 Oct 2012
Edited: Mike Hosea on 8 Oct 2012
The integration functions in MATLAB expect to be able to make "vectorized" calls. An example would be
>> sin(0:pi/12:pi/2)
ans =
0 0.2588 0.5000 0.7071 0.8660 0.9659 1.0000
A "vectorized" function will accept arrays as input and do its thing on each element of the input array to produce the output array. This is called an "elementwise" operation. Usually all you need to do in MATLAB to vectorize a function is use .* instead of *, ./ instead of /, and .^ instead of ^, but for some functions this is not enough. The ARRAYFUN function applies a function elementwise on input arrays. The line
arrayfun(@sin,0:pi/12:pi/2)
does the same thing as the example I gave before, but this time the sin() function is only ever called with scalar inputs. First it is called with the input 0, then with pi/12, then with pi/6, and so forth. This extends naturally to functions of two variables. For example
arrayfun(@atan2,0:0.1:1,-1:0.1:0)
builds an output array like so
[atan2(0,-1),atan2(0.1,-0.9),atan2(0.2,-0.8), ..., atan2(1,0)]
Since the output of DBLQUAD is only ever a scalar, we need to do something like this. We could do it by hand, but by using ARRAYFUN with anonymous functions (functions defined "on-the-fly" using @), we don't have to write an m-file with a loop to get it done. And ARRAYFUN is usually pretty fast in terms of the overhead it adds because it is a built-in function in MATLAB.

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations 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!