Got Questions? Get Answers.
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:
about Calculate integral

Subject: about Calculate integral

From: dhuan Du

Date: 24 Sep, 2010 15:41:05

Message: 1 of 4

Hi,
In my code I need to calcuate a three-dimensional integral using triplequad(fun,xmin,xmax,ymin,ymax,zmin,zmax), (xmin,xmax,ymin,ymax) are specifice by exact values, but (zmin,zmax) are going to be the function of x, and y. I run the following code where only v1, v2, h are variable in integral other other symples are pre-defined constants. However, I got error information provided below. Could anyone help me out? Thanks,

F=@(v2, v1,h)(1-(0.5*exp(-0.717*(-(h-2.5+y*(1/v2-1/v1))/((2^0.5)*sigama))-0.416*(-(h-2.5+y*(1/v2-1/v1))/((2^0.5)*sigama))^2)))...
            *(1/((2*pi)^0.5*theta)*exp(-((v1-vmu)/theta)^2))...
            *(1/((2*pi)^0.5*theta)*exp(-((v2-vmu)/theta)^2))...
            *(1/((2*pi)^0.5*hvar*h)*exp(-((log(h)-hmean)/theta)^2));
        eff=triplequad(F,0,120*5280/3600,0,120*5280/3600,0,(2.5+y*(1/v1-1/v2)));

and get error inforamtion:

Undefined function or variable 'v1'.

Error in ==> Linear_Searchv1 at 41
        eff=triplequad(F,0,120,0,120,0,(2.5+y*(1/v1-1/v2)));

Subject: about Calculate integral

From: Roger Stafford

Date: 24 Sep, 2010 20:19:20

Message: 2 of 4

"dhuan Du" <dupeony@gmail.com> wrote in message <i7igqg$jni$1@fred.mathworks.com>...
> Hi,
> In my code I need to calcuate a three-dimensional integral using triplequad(fun,xmin,xmax,ymin,ymax,zmin,zmax), (xmin,xmax,ymin,ymax) are specifice by exact values, but (zmin,zmax) are going to be the function of x, and y. I run the following code where only v1, v2, h are variable in integral other other symples are pre-defined constants. However, I got error information provided below. Could anyone help me out? Thanks,
>
> F=@(v2, v1,h)(1-(0.5*exp(-0.717*(-(h-2.5+y*(1/v2-1/v1))/((2^0.5)*sigama))-0.416*(-(h-2.5+y*(1/v2-1/v1))/((2^0.5)*sigama))^2)))...
> *(1/((2*pi)^0.5*theta)*exp(-((v1-vmu)/theta)^2))...
> *(1/((2*pi)^0.5*theta)*exp(-((v2-vmu)/theta)^2))...
> *(1/((2*pi)^0.5*hvar*h)*exp(-((log(h)-hmean)/theta)^2));
> eff=triplequad(F,0,120*5280/3600,0,120*5280/3600,0,(2.5+y*(1/v1-1/v2)));
>
> and get error inforamtion:
>
> Undefined function or variable 'v1'.
>
> Error in ==> Linear_Searchv1 at 41
> eff=triplequad(F,0,120,0,120,0,(2.5+y*(1/v1-1/v2)));
- - - - - - - - - -
  The matlab function triplequad will not accept variable integration limits. The documentation says the following about that:

"triplequad(fun,xmin,xmax,ymin,ymax,zmin,zmax) evaluates the triple integral fun(x,y,z) over the three dimensional rectangular region xmin <= x <= xmax, ymin <= y <= ymax, zmin <= x <= zmax."

  You might try using quad2d to get the double integral with respect to v1 and h. It does allow the inner integration limits to depend on the outer variable. You would use this to define a function of just v2 alone. You could then use quad or quadgk to take the integral of this function with respect to v2 if the function is properly written to accept vector inputs.

  You could run into other kinds of trouble with that integrand you have defined. The variables v2 and v1 become zero at their lower limits and that makes 1/v2 and 1/v1 infinite-valued in the integrand function. triplequad might be unhappy about that even if it turns out to be integrable. You might have to stop just shy of zero.

Roger Stafford

Subject: about Calculate integral

From: dhuan Du

Date: 24 Sep, 2010 20:49:04

Message: 3 of 4

Hi Roger Stafford:

First, thanks a lot for your reply.

I still have a question about quad2d you suggested. Since I have three variables here, if I use qua2d on (v1,h), then h is still a function of (v1, v2).I wonder how quad2d is going to treat v2. Is it able to work and treat v2 as a parameter in the integrand function such as f(v1, v2,h)?

Thanks again




"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i7j148$ctl$1@fred.mathworks.com>...
> "dhuan Du" <dupeony@gmail.com> wrote in message <i7igqg$jni$1@fred.mathworks.com>...
> > Hi,
> > In my code I need to calcuate a three-dimensional integral using triplequad(fun,xmin,xmax,ymin,ymax,zmin,zmax), (xmin,xmax,ymin,ymax) are specifice by exact values, but (zmin,zmax) are going to be the function of x, and y. I run the following code where only v1, v2, h are variable in integral other other symples are pre-defined constants. However, I got error information provided below. Could anyone help me out? Thanks,
> >
> > F=@(v2, v1,h)(1-(0.5*exp(-0.717*(-(h-2.5+y*(1/v2-1/v1))/((2^0.5)*sigama))-0.416*(-(h-2.5+y*(1/v2-1/v1))/((2^0.5)*sigama))^2)))...
> > *(1/((2*pi)^0.5*theta)*exp(-((v1-vmu)/theta)^2))...
> > *(1/((2*pi)^0.5*theta)*exp(-((v2-vmu)/theta)^2))...
> > *(1/((2*pi)^0.5*hvar*h)*exp(-((log(h)-hmean)/theta)^2));
> > eff=triplequad(F,0,120*5280/3600,0,120*5280/3600,0,(2.5+y*(1/v1-1/v2)));
> >
> > and get error inforamtion:
> >
> > Undefined function or variable 'v1'.
> >
> > Error in ==> Linear_Searchv1 at 41
> > eff=triplequad(F,0,120,0,120,0,(2.5+y*(1/v1-1/v2)));
> - - - - - - - - - -
> The matlab function triplequad will not accept variable integration limits. The documentation says the following about that:
>
> "triplequad(fun,xmin,xmax,ymin,ymax,zmin,zmax) evaluates the triple integral fun(x,y,z) over the three dimensional rectangular region xmin <= x <= xmax, ymin <= y <= ymax, zmin <= x <= zmax."
>
> You might try using quad2d to get the double integral with respect to v1 and h. It does allow the inner integration limits to depend on the outer variable. You would use this to define a function of just v2 alone. You could then use quad or quadgk to take the integral of this function with respect to v2 if the function is properly written to accept vector inputs.
>
> You could run into other kinds of trouble with that integrand you have defined. The variables v2 and v1 become zero at their lower limits and that makes 1/v2 and 1/v1 infinite-valued in the integrand function. triplequad might be unhappy about that even if it turns out to be integrable. You might have to stop just shy of zero.
>
> Roger Stafford

Subject: about Calculate integral

From: Roger Stafford

Date: 24 Sep, 2010 21:42:06

Message: 4 of 4

"dhuan Du" <dupeony@gmail.com> wrote in message <i7j2s0$6i1$1@fred.mathworks.com>...
> Hi Roger Stafford:
>
> First, thanks a lot for your reply.
>
> I still have a question about quad2d you suggested. Since I have three variables here, if I use qua2d on (v1,h), then h is still a function of (v1, v2).I wonder how quad2d is going to treat v2. Is it able to work and treat v2 as a parameter in the integrand function such as f(v1, v2,h)?
>
> Thanks again
- - - - - - - - -
  Yes, writing that function of v2 is the tricky part. This function has to be able to accept v2 as a vector of v2 values, so at each call on the function you will have to determine how many elements there are in the v2 vector and then set up a for-loop to do repeated calls on quad2d that many times, once for each different value of v2. This has to be done so that each individual v2 value is passed as a parameter to be used in the integrand function and the variable integration limits of h as each call on quad2d is made. Then outside this function you will call on quad or quadgk and specify the above function as its integrand so as to obtain its integral with respect to v2 over the given limits.

  It means more coding to write but I'm afraid that is the price one has to pay for using variable limits in matlab's triple integration. The only other possibilities that come to mind are a) to write the integrand as a discontinuous function that drops to zero when outside the desired integrations limits and then integrate over the whole 3D rectangle, or b) to devise some kind of change of variable for h that transforms its limits to a pair of constants. For example in case b) define

 h2 = h/(2.5+y*(1/v1-1/v2))

which would have limits for h2 as constants 0 and 1, and rewrite your integrand to be a function of v2, v1, h2 (remembering of course to correct with the appropriate Jacobian.) Also I'm afraid triplequad will not be very happy with a discontinuous function for integrand in case a). It could interfere with its accuracy.

Roger Stafford

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