Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Double integration

Subject: Double integration

From: bob bobby

Date: 27 Apr, 2011 21:17:05

Message: 1 of 4

hi all!
I've been struggling to figure out how to calculate this double integral using matlab for the last few days and I'm totally stuck. I've read a wide variety of posts about double integrations and quad2d and dblquad but I'm no closer to figuring this out. I'm trying to integrate the following equation (safe link on imageshack):

http://img844.imageshack.us/i/codecogseqn3.gif/

where s and x are random variables and f(x) and g(s) are the respective pdfs.

I have tried to use the following
quad2d(@(x,s) normpdf(s).*normpdf(x).*(s+x-1), @(s) 1-s, 500, -500, 500);

but I get this error message:
??? Error using ==> quad2d at 97
A must be a finite, scalar, floating point constant.

which makes sense sure, and I tried switching round the s and the x the limits but I am getting weird results and I am pretty certain it's totally wrong to do this.

I think I need to use a loop to compute the 1-s prior to the integration but I am totally lost.
I would really appreciate some help here, many thanks.

Regards,
Robert

Subject: Double integration

From: Roger Stafford

Date: 27 Apr, 2011 22:35:22

Message: 2 of 4

"bob bobby" wrote in message <ipa14h$quv$1@fred.mathworks.com>...
> hi all!
> I've been struggling to figure out how to calculate this double integral using matlab for the last few days and I'm totally stuck. I've read a wide variety of posts about double integrations and quad2d and dblquad but I'm no closer to figuring this out. I'm trying to integrate the following equation (safe link on imageshack):
>
> http://img844.imageshack.us/i/codecogseqn3.gif/
>
> where s and x are random variables and f(x) and g(s) are the respective pdfs.
>
> I have tried to use the following
> quad2d(@(x,s) normpdf(s).*normpdf(x).*(s+x-1), @(s) 1-s, 500, -500, 500);
>
> but I get this error message:
> ??? Error using ==> quad2d at 97
> A must be a finite, scalar, floating point constant.
>
> which makes sense sure, and I tried switching round the s and the x the limits but I am getting weird results and I am pretty certain it's totally wrong to do this.
>
> I think I need to use a loop to compute the 1-s prior to the integration but I am totally lost.
> I would really appreciate some help here, many thanks.
>
> Regards,
> Robert
- - - - - - - - -
  The outer variable of integration is 's' and the inner one is 'x', so you should have them arranged accordingly in 'quad2d'. You have them reversed. Also the integration limit function, 1-s, should be given as the fourth argument, not the second one. Read over the documentation of 'quad2d' very carefully.

  Perhaps you should try a very simple double integral with finite limits and with a variable lower inner limit whose answer you already know from calculus so as to check out your procedure. When you can get that one right you can advance to the one above.

Roger Stafford

Subject: Double integration

From: Roger Stafford

Date: 27 Apr, 2011 23:25:08

Message: 3 of 4

"bob bobby" wrote in message <ipa14h$quv$1@fred.mathworks.com>...
> ......
> I have tried to use the following
> quad2d(@(x,s) normpdf(s).*normpdf(x).*(s+x-1), @(s) 1-s, 500, -500, 500);
> ......
- - - - - - - - -
  One more thought. The values you have used in place of plus and minus infinity are very likely much too large and could create difficulties in the integration process. Setting x or s equal to plus or minus 500 produces exp(-125000) which would be an exact zero for matlab floating point numbers. In fact for a very large part of the range of integration you would have a zero integrand. This can cause 'quad2d' to accidentally skip over the range where the integrand has significantly sizable values, or to not sample the integrand with sufficiently fine intervals where it is important for an accurate answer. Try using much, much smaller limits in place of those 500's. There's no reason at all to go out that far.

Roger Stafford

Subject: Double integration

From: bob bobby

Date: 28 Apr, 2011 03:24:07

Message: 4 of 4

"Roger Stafford" wrote in message <ipa8kk$ovd$1@fred.mathworks.com>...
> "bob bobby" wrote in message <ipa14h$quv$1@fred.mathworks.com>...
> > ......
> > I have tried to use the following
> > quad2d(@(x,s) normpdf(s).*normpdf(x).*(s+x-1), @(s) 1-s, 500, -500, 500);
> > ......
> - - - - - - - - -
> One more thought. The values you have used in place of plus and minus infinity are very likely much too large and could create difficulties in the integration process. Setting x or s equal to plus or minus 500 produces exp(-125000) which would be an exact zero for matlab floating point numbers. In fact for a very large part of the range of integration you would have a zero integrand. This can cause 'quad2d' to accidentally skip over the range where the integrand has significantly sizable values, or to not sample the integrand with sufficiently fine intervals where it is important for an accurate answer. Try using much, much smaller limits in place of those 500's. There's no reason at all to go out that far.
>
> Roger Stafford

Many thanks Roger I really appreciate you taking the time to explain these points to me! I am getting outputs which make sense and you were entirely correct about the limits causing matlab to skip over important values due them being too wide!
Best regards,
Robert

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us