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:
Why am I unable to numerically integrate this?

Subject: Why am I unable to numerically integrate this?

From: KC

Date: 22 Apr, 2010 20:46:22

Message: 1 of 7

Im trying to integrate this expression (k^4*exp(k))/(exp(k) - 1)^2 and need a matrix of values for different limits of integration . But for some reason i am getting NaN as the only answer. Any help would be much appreciated.

So far this is what I have......

theta=225;
T=[(1:1:1000)];

for n=1:1000
    x(n)=theta/T(n);
    Q(n)=quad(@mt2p3b,0,x(n));
    C(n)=9*R*(T(n)/theta)^3*Q(n);
end

where mt2p3b is a function handle
function INT = mt2p3b(k)
INT=(k.^4).*(exp(k))./(((exp(k))-1).^2);

Subject: Why am I unable to numerically integrate this?

From: KC

Date: 22 Apr, 2010 21:00:25

Message: 2 of 7

Update: Well, I get answers upto n=449 (n=450 is 2*theta value). But after that i get NaN. Out of curiosity, I changed theta to 250, and I started getting values upto n=499. I wonder what is happening when n reaches 2*theta values. Why is it unable to compute beyond that? Please help.

Subject: Why am I unable to numerically integrate this?

From: Roger Stafford

Date: 22 Apr, 2010 21:00:25

Message: 3 of 7

"KC " <vistarak@gatech.edu> wrote in message <hqqciu$ip4$1@fred.mathworks.com>...
> Im trying to integrate this expression (k^4*exp(k))/(exp(k) - 1)^2 and need a matrix of values for different limits of integration . But for some reason i am getting NaN as the only answer. Any help would be much appreciated.
>
> So far this is what I have......
>
> theta=225;
> T=[(1:1:1000)];
>
> for n=1:1000
> x(n)=theta/T(n);
> Q(n)=quad(@mt2p3b,0,x(n));
> C(n)=9*R*(T(n)/theta)^3*Q(n);
> end
>
> where mt2p3b is a function handle
> function INT = mt2p3b(k)
> INT=(k.^4).*(exp(k))./(((exp(k))-1).^2);

  You have a singularity in the integrand at k = 0. It is not integrable if zero is included in the range of integration.

Roger Stafford

Subject: Why am I unable to numerically integrate this?

From: KC

Date: 22 Apr, 2010 21:16:22

Message: 4 of 7

> You have a singularity in the integrand at k = 0. It is not integrable if zero is included in the range of integration.
>
> Roger Stafford

Roger....Thank you so much....I think you nailed it.....I can't believe I overlooked something like that. Thanks once again

Subject: Why am I unable to numerically integrate this?

From: Roger Stafford

Date: 22 Apr, 2010 21:27:08

Message: 5 of 7

"KC " <vistarak@gatech.edu> wrote in message <hqqeb6$kg6$1@fred.mathworks.com>...
> > You have a singularity in the integrand at k = 0. It is not integrable if zero is included in the range of integration.
> >
> > Roger Stafford
>
> Roger....Thank you so much....I think you nailed it.....I can't believe I overlooked something like that. Thanks once again
-----------
  I was too hasty and failed to notice the k^4 in the numerator. It is actually integrable with k = 0 as a lower limit, but the form of the function will cause matlab to create a NaN if it sends the value k = 0 to the function. You will either have to move away from 0 by a tiny amount or, what is probably better, redefine the function in the immediate neighborhood of k = 0 by a Taylor expansion - probably only a single term would be needed if your neighborhood is sufficiently small. It is analogous to having a function like sin(x)/x in your integrand at the point x = 0.

Roger Stafford

Subject: Why am I unable to numerically integrate this?

From: Roger Stafford

Date: 22 Apr, 2010 22:15:19

Message: 6 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hqqevc$3gn$1@fred.mathworks.com>...
> I was too hasty and failed to notice the k^4 in the numerator. It is actually integrable with k = 0 as a lower limit, but the form of the function will cause matlab to create a NaN if it sends the value k = 0 to the function. You will either have to move away from 0 by a tiny amount or, what is probably better, redefine the function in the immediate neighborhood of k = 0 by a Taylor expansion - probably only a single term would be needed if your neighborhood is sufficiently small. It is analogous to having a function like sin(x)/x in your integrand at the point x = 0.
>
> Roger Stafford
-------------
  When I talked about a "single term" in the Taylor expansion, I should have made clear that this term would be a certain constant times k-squared. The first two Taylor series terms will actually be zero.

Roger Stafford

Subject: Why am I unable to numerically integrate this?

From: Michael Hosea

Date: 23 Apr, 2010 16:24:20

Message: 7 of 7

As already noted, you can move away from 0 slightly. Another alternative is
to use QUADGK instead of QUAD. It's not as fast as QUAD on this problem,
but it's a lot more accurate, if that matters to you, and it works "out of
the box" here (no need to fudge the left endpoint).
--
Mike

"KC " <vistarak@gatech.edu> wrote in message
news:hqqciu$ip4$1@fred.mathworks.com...
> Im trying to integrate this expression (k^4*exp(k))/(exp(k) - 1)^2 and
> need a matrix of values for different limits of integration . But for some
> reason i am getting NaN as the only answer. Any help would be much
> appreciated.
> So far this is what I have......
>
> theta=225;
> T=[(1:1:1000)];
>
> for n=1:1000
> x(n)=theta/T(n);
> Q(n)=quad(@mt2p3b,0,x(n));
> C(n)=9*R*(T(n)/theta)^3*Q(n);
> end
>
> where mt2p3b is a function handle
> function INT = mt2p3b(k)
> INT=(k.^4).*(exp(k))./(((exp(k))-1).^2);
>

Tags for this Thread

No tags are associated with 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