Thread Subject: Integrating function of a function

Subject: Integrating function of a function

From: Ian Taylor

Date: 20 Jun, 2008 13:01:44

Message: 1 of 4

I have a function (let us call it shape), and one example I could use
for this is:

shape = hmin + x*x/(2*R)

so there are three input arguments: x, hmin, R

I want to integrate 1/(shape*shape) and 1/(shape*shape*shape).
I'm having trouble with the syntax needed to do this with quad.

I know that if I simply wanted to integrate shape, I would use
quad(@(x)shape(x,hmin,R),a,b)

Is it best to define two functions, one being 1/(shape*shape) and the
other being 1/(shape*shape*shape), or can I call quad directly in some
way ?

Thanks

Subject: Integrating function of a function

From: Loren Shure

Date: 20 Jun, 2008 14:14:44

Message: 2 of 4

In article <39e38ef4-9894-45c8-9b17-e264ccf03233
@m44g2000hsc.googlegroups.com>, robert.ian.taylor@gmail.com says...
> I have a function (let us call it shape), and one example I could use
> for this is:
>
> shape = hmin + x*x/(2*R)
>
> so there are three input arguments: x, hmin, R
>
> I want to integrate 1/(shape*shape) and 1/(shape*shape*shape).
> I'm having trouble with the syntax needed to do this with quad.
>
> I know that if I simply wanted to integrate shape, I would use
> quad(@(x)shape(x,hmin,R),a,b)
>
> Is it best to define two functions, one being 1/(shape*shape) and the
> other being 1/(shape*shape*shape), or can I call quad directly in some
> way ?
>
> Thanks
>

Why not just make an anonymous function for the integrand, either
straight from your formula, or using the function shape?

quad(@(x) 1. / (hmin + x.^2 / (2*R)).^2, a,b) AND
quad(@(x) 1. / (hmin + x.^2 / (2*R)).^3, a,b)

OR

quad(@(x) 1. / shape(x,hmin,R).^2, a,b) AND
quad(@(x) 1. / shape(x,hmin,R).^3, a,b)

I didn't try these so they might not be correct, but the idea is there.

--
Loren
http://blogs.mathworks.com/loren/

Subject: Integrating function of a function

From: Ian Taylor

Date: 21 Jun, 2008 10:24:36

Message: 3 of 4

On 20 Jun, 15:14, Loren Shure <lo...@mathworks.com> wrote:
> In article <39e38ef4-9894-45c8-9b17-e264ccf03233
> @m44g2000hsc.googlegroups.com>, robert.ian.tay...@gmail.com says...
>
>
>
>
>
> > I have a function (let us call it shape), and one example I could use
> > for this is:
>
> > shape =3D hmin + x*x/(2*R)
>
> > so there are three input arguments: x, hmin, R
>
> > I want to integrate 1/(shape*shape) and 1/(shape*shape*shape).
> > I'm having trouble with the syntax needed to do this with quad.
>
> > I know that if I simply wanted to integrate shape, I would use
> > quad(@(x)shape(x,hmin,R),a,b)
>
> > Is it best to define two functions, one being 1/(shape*shape) and the
> > other being 1/(shape*shape*shape), or can I call quad directly in some
> > way ?
>
> > Thanks
>
> Why not just make an anonymous function for the integrand, either
> straight from your formula, or using the function shape?
>
> quad(@(x) 1. / (hmin + x.^2 / (2*R)).^2, a,b) =A0AND
> quad(@(x) 1. / (hmin + x.^2 / (2*R)).^3, a,b)
>
> OR
>
> quad(@(x) 1. / shape(x,hmin,R).^2, a,b) AND
> quad(@(x) 1. / shape(x,hmin,R).^3, a,b)
>
> I didn't try these so they might not be correct, but the idea is there.
>
> --
> Lorenhttp://blogs.mathworks.com/loren/- Hide quoted text -
>
> - Show quoted text -

Thanks for the help. I will try it out. The second suggestion looks
more useful, because at a later date I may want to change the shape
function.

All the best

Ian Taylor
http://www.iantaylor.org.uk/

Subject: Integrating function of a function

From: John D'Errico

Date: 21 Jun, 2008 11:34:02

Message: 4 of 4

Ian Taylor <robert.ian.taylor@gmail.com> wrote in message <39e38ef4-
9894-45c8-9b17-e264ccf03233@m44g2000hsc.googlegroups.com>...
> I have a function (let us call it shape), and one example I could use
> for this is:
>
> shape = hmin + x*x/(2*R)
>
> so there are three input arguments: x, hmin, R
>
> I want to integrate 1/(shape*shape) and 1/(shape*shape*shape).
> I'm having trouble with the syntax needed to do this with quad.
>
> I know that if I simply wanted to integrate shape, I would use
> quad(@(x)shape(x,hmin,R),a,b)
>
> Is it best to define two functions, one being 1/(shape*shape) and the
> other being 1/(shape*shape*shape), or can I call quad directly in some
> way ?
>
> Thanks

shape = @(x,hmin,R) hmin + x.*x/(2*R);

quad(@(x) 1./shape(x).^n,,a,b)

Note the use of the dotted operators, .* ./ and .^
in these calls. This allows quad to run properly.

John

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
recintegrate FENG GUO 22 Jul, 2008 10:28:43
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com