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

New to MATLAB?

From: bob bobby

### bob bobby (view profile)

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;
g= @(y) normpdf(y)*f2(y);

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 ==> @(y)normpdf(y)*f2(y)

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

Error in ==> file at 6

Cheers
Rob

From: bob bobby

### bob bobby (view profile)

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;
> g= @(y) normpdf(y)*f2(y);
>
> 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 ==> @(y)normpdf(y)*f2(y)
>
> Error in ==> quad at 77
> y = f(x, varargin{:});
>
> Error in ==> file at 6
>
>
> Cheers
> Rob

From: John D'Errico

### John D'Errico (view profile)

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;
> g= @(y) normpdf(y)*f2(y);
>
> 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 ==> @(y)normpdf(y)*f2(y)
>
> Error in ==> quad at 77
> y = f(x, varargin{:});
>
> Error in ==> file at 6
>
>
> Cheers
> Rob

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

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

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

John

From: bob bobby

### bob bobby (view profile)

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);
> >
> > 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
> >
> >
> > Cheers
> > Rob
>
> (By the way, this is a bit of a quiet time of the year, so
> don't be so impatient.)
>
>
> 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!

From: John D'Errico

### John D'Errico (view profile)

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)
> > >
> > >
> > > 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

From: Erim

### Erim (view profile)

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;
> g= @(y) normpdf(y)*f2(y);
>
> 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 ==> @(y)normpdf(y)*f2(y)
>
> Error in ==> quad at 77
> y = f(x, varargin{:});
>
> Error in ==> file at 6
>
>
> Cheers
> Rob

 Subject: Nested integration using quad From: Steven_Lord Date: 14 Apr, 2011 13:35:49 Message: 7 of 7 "Erim " 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

### 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.