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:
Nested integration using quad

Subject: Nested integration using quad

From: bob bobby

Date: 30 Dec, 2010 00:54:04

Message: 1 of 7

Hi all!
I'm a new Matlab user and I'm loving the software and this newsgroup! I'm having an issue using the quad function to numerically integrate the following and I can't find any solution that is applicable to it on the newsgroup or on google! Any help is much appreciated!

basically have 2 normally distributed random variables s and x, whose pdfs are f and g and cdfs are F and G.

the function i need to solve is the following: (click link for image in clean mathematical format)
http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq=\int_{-100}^{100}(\int_{-B*s}^{1-(B*s)}%20F(x)%20%20dx)%20g(s)ds

f = @(x) normcdf(x);
b = 0.2;
f2 = @(s) quad(f,-(b*s),1-(b*s));
g= @(y) normpdf(y)*f2(y);
quad(g, -100, 100)

I thought I was being smart using the above but I get the following errors:

??? Error using ==> quad at 70
The limits of integration must be scalars.

Error in ==> @(s)quad(f,-(b*s),1-(b*s))

Error in ==> @(y)normpdf(y)*f2(y)

Error in ==> quad at 77
y = f(x, varargin{:});

Error in ==> file at 6
quad(g, -100, 100)


Thanks for any help you can give me
Cheers
Rob

Subject: Nested integration using quad

From: bob bobby

Date: 30 Dec, 2010 10:49:05

Message: 2 of 7

Please guys I still can't figure this out..

"bob bobby" wrote in message <ifgl7c$pf2$1@fred.mathworks.com>...
> Hi all!
> I'm a new Matlab user and I'm loving the software and this newsgroup! I'm having an issue using the quad function to numerically integrate the following and I can't find any solution that is applicable to it on the newsgroup or on google! Any help is much appreciated!
>
> basically have 2 normally distributed random variables s and x, whose pdfs are f and g and cdfs are F and G.
>
> the function i need to solve is the following: (click link for image in clean mathematical format)
> http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq=\int_{-100}^{100}(\int_{-B*s}^{1-(B*s)}%20F(x)%20%20dx)%20g(s)ds
>
> f = @(x) normcdf(x);
> b = 0.2;
> f2 = @(s) quad(f,-(b*s),1-(b*s));
> g= @(y) normpdf(y)*f2(y);
> quad(g, -100, 100)
>
> I thought I was being smart using the above but I get the following errors:
>
> ??? Error using ==> quad at 70
> The limits of integration must be scalars.
>
> Error in ==> @(s)quad(f,-(b*s),1-(b*s))
>
> Error in ==> @(y)normpdf(y)*f2(y)
>
> Error in ==> quad at 77
> y = f(x, varargin{:});
>
> Error in ==> file at 6
> quad(g, -100, 100)
>
>
> Thanks for any help you can give me
> Cheers
> Rob

Subject: Nested integration using quad

From: John D'Errico

Date: 30 Dec, 2010 11:44:06

Message: 3 of 7

"bob bobby" wrote in message <ifgl7c$pf2$1@fred.mathworks.com>...
> Hi all!
> I'm a new Matlab user and I'm loving the software and this newsgroup! I'm having an issue using the quad function to numerically integrate the following and I can't find any solution that is applicable to it on the newsgroup or on google! Any help is much appreciated!
>
> basically have 2 normally distributed random variables s and x, whose pdfs are f and g and cdfs are F and G.
>
> the function i need to solve is the following: (click link for image in clean mathematical format)
> http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq=\int_{-100}^{100}(\int_{-B*s}^{1-(B*s)}%20F(x)%20%20dx)%20g(s)ds
>
> f = @(x) normcdf(x);
> b = 0.2;
> f2 = @(s) quad(f,-(b*s),1-(b*s));
> g= @(y) normpdf(y)*f2(y);
> quad(g, -100, 100)
>
> I thought I was being smart using the above but I get the following errors:
>
> ??? Error using ==> quad at 70
> The limits of integration must be scalars.
>
> Error in ==> @(s)quad(f,-(b*s),1-(b*s))
>
> Error in ==> @(y)normpdf(y)*f2(y)
>
> Error in ==> quad at 77
> y = f(x, varargin{:});
>
> Error in ==> file at 6
> quad(g, -100, 100)
>
>
> Thanks for any help you can give me
> Cheers
> Rob

(By the way, this is a bit of a quiet time of the year, so
don't be so impatient.)

READ THE ERROR MESSAGE!

Your inner integral has the parameter s in the limits of
integration.

What does the error message say? Read it again. Here, I
will repeat it for you.

> ??? Error using ==> quad at 70
> The limits of integration must be scalars.

When quad runs, it does not pass in single points to
evaluate the function at. It can often pass in several
points for efficiency. So what happens inside the
internal integral when it gets several values for s? Yes.
it tries to call quad with a list of limits of integration,
so quad kicks out with an error.

Obviously, you can fix this by putting the internal call
to quad in a loop.

Read the error messages. They are put there for a
reason.

John

Subject: Nested integration using quad

From: bob bobby

Date: 30 Dec, 2010 12:03:05

Message: 4 of 7

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ifhra6$icu$1@fred.mathworks.com>...
> "bob bobby" wrote in message <ifgl7c$pf2$1@fred.mathworks.com>...
> > Hi all!
> > I'm a new Matlab user and I'm loving the software and this newsgroup! I'm having an issue using the quad function to numerically integrate the following and I can't find any solution that is applicable to it on the newsgroup or on google! Any help is much appreciated!
> >
> > basically have 2 normally distributed random variables s and x, whose pdfs are f and g and cdfs are F and G.
> >
> > the function i need to solve is the following: (click link for image in clean mathematical format)
> > http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq=\int_{-100}^{100}(\int_{-B*s}^{1-(B*s)}%20F(x)%20%20dx)%20g(s)ds
> >
> > f = @(x) normcdf(x);
> > b = 0.2;
> > f2 = @(s) quad(f,-(b*s),1-(b*s));
> > g= @(y) normpdf(y)*f2(y);
> > quad(g, -100, 100)
> >
> > I thought I was being smart using the above but I get the following errors:
> >
> > ??? Error using ==> quad at 70
> > The limits of integration must be scalars.
> >
> > Error in ==> @(s)quad(f,-(b*s),1-(b*s))
> >
> > Error in ==> @(y)normpdf(y)*f2(y)
> >
> > Error in ==> quad at 77
> > y = f(x, varargin{:});
> >
> > Error in ==> file at 6
> > quad(g, -100, 100)
> >
> >
> > Thanks for any help you can give me
> > Cheers
> > Rob
>
> (By the way, this is a bit of a quiet time of the year, so
> don't be so impatient.)
>
> READ THE ERROR MESSAGE!
>
> Your inner integral has the parameter s in the limits of
> integration.
>
> What does the error message say? Read it again. Here, I
> will repeat it for you.
>
> > ??? Error using ==> quad at 70
> > The limits of integration must be scalars.
>
> When quad runs, it does not pass in single points to
> evaluate the function at. It can often pass in several
> points for efficiency. So what happens inside the
> internal integral when it gets several values for s? Yes.
> it tries to call quad with a list of limits of integration,
> so quad kicks out with an error.
>
> Obviously, you can fix this by putting the internal call
> to quad in a loop.
>
> Read the error messages. They are put there for a
> reason.
>
> John

I think I have come up with an alternative solution:

quad2d(@(x,s) normpdf(s).*normcdf(x),-100, 100, @(s) -s*b, @(s) 1-(b*s))

does this make sense?
many thanks!

Subject: Nested integration using quad

From: John D'Errico

Date: 30 Dec, 2010 16:02:05

Message: 5 of 7

"bob bobby" wrote in message <ifhsdp$fj$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <ifhra6$icu$1@fred.mathworks.com>...
> > "bob bobby" wrote in message <ifgl7c$pf2$1@fred.mathworks.com>...
> > > Hi all!
> > > I'm a new Matlab user and I'm loving the software and this newsgroup! I'm having an issue using the quad function to numerically integrate the following and I can't find any solution that is applicable to it on the newsgroup or on google! Any help is much appreciated!
> > >
> > > basically have 2 normally distributed random variables s and x, whose pdfs are f and g and cdfs are F and G.
> > >
> > > the function i need to solve is the following: (click link for image in clean mathematical format)
> > > http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq=\int_{-100}^{100}(\int_{-B*s}^{1-(B*s)}%20F(x)%20%20dx)%20g(s)ds
> > >
> > > f = @(x) normcdf(x);
> > > b = 0.2;
> > > f2 = @(s) quad(f,-(b*s),1-(b*s));
> > > g= @(y) normpdf(y)*f2(y);
> > > quad(g, -100, 100)
> > >
> > > I thought I was being smart using the above but I get the following errors:
> > >
> > > ??? Error using ==> quad at 70
> > > The limits of integration must be scalars.
> > >
> > > Error in ==> @(s)quad(f,-(b*s),1-(b*s))
> > >
> > > Error in ==> @(y)normpdf(y)*f2(y)
> > >
> > > Error in ==> quad at 77
> > > y = f(x, varargin{:});
> > >
> > > Error in ==> file at 6
> > > quad(g, -100, 100)
> > >
> > >
> > > Thanks for any help you can give me
> > > Cheers
> > > Rob
> >
> > (By the way, this is a bit of a quiet time of the year, so
> > don't be so impatient.)
> >
> > READ THE ERROR MESSAGE!
> >
> > Your inner integral has the parameter s in the limits of
> > integration.
> >
> > What does the error message say? Read it again. Here, I
> > will repeat it for you.
> >
> > > ??? Error using ==> quad at 70
> > > The limits of integration must be scalars.
> >
> > When quad runs, it does not pass in single points to
> > evaluate the function at. It can often pass in several
> > points for efficiency. So what happens inside the
> > internal integral when it gets several values for s? Yes.
> > it tries to call quad with a list of limits of integration,
> > so quad kicks out with an error.
> >
> > Obviously, you can fix this by putting the internal call
> > to quad in a loop.
> >
> > Read the error messages. They are put there for a
> > reason.
> >
> > John
>
> I think I have come up with an alternative solution:
>
> quad2d(@(x,s) normpdf(s).*normcdf(x),-100, 100, @(s) -s*b, @(s) 1-(b*s))


Yes, it should work, as quad2d does accept problems
where the limits of integration are a function of the
outer variable.

But test it yourself. Verify that the result makes sense.

John

Subject: Nested integration using quad

From: Erim

Date: 14 Apr, 2011 05:13:02

Message: 6 of 7

Hi,
I have a similar problem. I'd like to have a second opinion on what I am doing.

I'm carrying out a double integration, where the lower limit of the inner integral is the variable used in the outer integral. The function that is passed to the first quad call, say y = fun(x), accepts a vector argument x and returns a vector result y, the integrand evaluated at each element of x. I set up a for loop in this function fun(x) so that it calls quad again for each element of x. Therefore, eventually, the integrand of the first integral (or the first quad call) is calculated (at each element of x) via another quad call.

i'd like to make sure that this is correct mathematically. Does anybody have any other ways or suggestions on how to do this or improve this approach? I am integrating piecewise linear and piecewise polynomial functions, so I need something (like quad) to which I can give these piecewise functions.

Any help is very much appreciated. Thank you.

erim




"bob bobby" wrote in message <ifgl7c$pf2$1@fred.mathworks.com>...
> Hi all!
> I'm a new Matlab user and I'm loving the software and this newsgroup! I'm having an issue using the quad function to numerically integrate the following and I can't find any solution that is applicable to it on the newsgroup or on google! Any help is much appreciated!
>
> basically have 2 normally distributed random variables s and x, whose pdfs are f and g and cdfs are F and G.
>
> the function i need to solve is the following: (click link for image in clean mathematical format)
> http://www.sitmo.com/gg/latex/latex2png.2.php?z=100&eq=\int_{-100}^{100}(\int_{-B*s}^{1-(B*s)}%20F(x)%20%20dx)%20g(s)ds
>
> f = @(x) normcdf(x);
> b = 0.2;
> f2 = @(s) quad(f,-(b*s),1-(b*s));
> g= @(y) normpdf(y)*f2(y);
> quad(g, -100, 100)
>
> I thought I was being smart using the above but I get the following errors:
>
> ??? Error using ==> quad at 70
> The limits of integration must be scalars.
>
> Error in ==> @(s)quad(f,-(b*s),1-(b*s))
>
> Error in ==> @(y)normpdf(y)*f2(y)
>
> Error in ==> quad at 77
> y = f(x, varargin{:});
>
> Error in ==> file at 6
> quad(g, -100, 100)
>
>
> Thanks for any help you can give me
> Cheers
> Rob

Subject: Nested integration using quad

From: Steven_Lord

Date: 14 Apr, 2011 13:35:49

Message: 7 of 7



"Erim " <erimkardes@hotmail.com> wrote in message
news:io5vou$k0e$1@fred.mathworks.com...
> Hi, I have a similar problem. I'd like to have a second opinion on what I
> am doing.
> I'm carrying out a double integration, where the lower limit of the inner
> integral is the variable used in the outer integral. The function that is
> passed to the first quad call, say y = fun(x), accepts a vector argument x
> and returns a vector result y, the integrand evaluated at each element of
> x. I set up a for loop in this function fun(x) so that it calls quad again
> for each element of x. Therefore, eventually, the integrand of the first
> integral (or the first quad call) is calculated (at each element of x) via
> another quad call.

Use QUAD2D instead. See example 2 on the reference page.

http://www.mathworks.com/help/techdoc/ref/quad2d.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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