Mombu the Science Forum sponsored links

Go Back   Mombu the Science Forum > Science > piecewise integration problem.
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 28th December 08:36
nma124
External User
 
Posts: 1
Default piecewise integration problem.



hello;

It seems maple and mma have different semantics when it comes
to piecewise functions, at least when it comes to using them
with integrations.

I define a function f, which is 0 from -Pi to Pi/2 and 1 from Pi/2
to Pi. simple function.

f[x_] := 0 /; -Pi < x < Pi/2
f[x_] := 1 /; Pi/2 < x < Pi

Notice the function f is not defined on point point x=Pi/2.

Then I asked mma to integrate cos[x]*f[x] from 0 to Pi. This
means becuase f is zero over -Pi to Pi/2, I expected the above
integral should be the same as integrating cos[x] from Pi/2 to Pi,
right? This is how we do in by hand.

What do you think the answer should be? -1, right?

---------------

In[1]:= f[x_] := 0 /; -Pi < x < Pi/2

In[2]:= f[x_] := 1 /; Pi/2 < x < Pi

In[3]:= Integrate[Cos[x] f[x], {x, -Pi, Pi}]

Out[3]= Integrate[Cos[x] f[x], {x, -Pi, Pi}]

---------------

see, it does not do it.
So I tell it to use numerical.

---------------

In[4]:= N[%]

NIntegrate::inum: Integrand Cos[x] f[x] is not numerical at {x} = {1.5708}.

NIntegrate::inum: Integrand Cos[x] f[x] is not numerical at {x} = {1.5708}.

NIntegrate::inum: Integrand Cos[x] f[x] is not numerical at {x} = {1.5708}.

General::stop: Further output of NIntegrate::inum
will be suppressed during this calculation.


NIntegrate::inum: Integrand Cos[x] f[x] is not numerical at {x} = {1.5708}.

NIntegrate::inum: Integrand Cos[x] f[x] is not numerical at {x} = {1.5708}.

NIntegrate::inum: Integrand Cos[x] f[x] is not numerical at {x} = {1.5708}.

General::stop: Further output of NIntegrate::inum
will be suppressed during this calculation.
Out[4]= NIntegrate[Cos[x] f[x], {x, -Pi, Pi}]

----------------

see, it does not like the integrand at Pi/2, which is a jump point
(or more accurately I should say a discontinuity point) for the function f[x].

If mma is correct, then how come maple does the same thing with
no problem? without even asking it to use numerical integration:


-1


-1


So, my question to the experts, who is correct in this? I am asking
this, becuase the book I am looking at does show the function f[x]
defined as I showed above. i.e.

f[x_] := 0 /; -Pi < x < Pi/2
f[x_] := 1 /; Pi/2 < x < Pi

and NOT

f[x_] := 0 /; -Pi < x <= Pi/2 <--- notice the <= here. now mma is happy
f[x_] := 1 /; Pi/2 < x < Pi

Also, when I use the above new defintion for f[x], mma still will NOT give
a symbolic answer (I mean without using N[]), but I still must
use numerical integration, then it will give the answer -1. This
is very annoying.


thanks,
Steve
  Reply With Quote


  sponsored links


2 28th December 08:37
jens-peer kuska
External User
 
Posts: 1
Default piecewise integration problem.



Hi,

Integrate[Cos[x] UnitStep[x - Pi/2], {x, -Pi, Pi}]

gives -1 as expected.

Regards
Jens
  Reply With Quote
3 28th December 08:37
t.richard
External User
 
Posts: 1
Default piecewise integration problem.


Note that what you're doing here is not numerical integration but simple
numerical evaluation of the exact result. (It should return -1 with a
trailing decimal point; maybe that was lost during Copy & Paste?).
Purely numerical integration requires Int, the inert form of int:
evalf(Int(cos(x)*f(x),x=-Pi..Pi));
See ?evalfint for background information.
Setting infolevel[`evalf/int`]:=5 will give details about the process.

--
Thomas Richard
Maple Support
Scientific Computers GmbH
http://www.scientific.de
  Reply With Quote
4 28th December 08:37
nma124
External User
 
Posts: 1
Default piecewise integration problem.


I must have.

Also, I was trying different things and meant to paste this line.

-1

Notice, btw, NO decimal point here.

oh, ok. did not know this. so evalf(Int) is the same as int[numeric].
thanks

Steve
  Reply With Quote
5 28th December 08:37
nma124
External User
 
Posts: 1
Default piecewise integration problem.


thanks, but this does not really answer the question. I was not
looking for another way to do this one specific example.

Mathematically speaking, the area under the curve (integration) is
the same if the function is not defined over a countable number of
points.

look at this VERY simple case

f[x_] := x /; -1 < x < 0
f[x_] := x /; 0 < x < 1
Integrate[f[x], {x, -1, 1}]

This, MATHEMATICA did not do.

even with numeric N[]:

NIntegrate::inum: Integrand f[x] is not numerical at {x} = {0.`}

The math book i am studying from does integration of things as the
above every where. i.e. on functions which has 'break' in them, where
the function is not defined one a point where it breaks/jumps.

i.e. it uses < instead of <= in the definition.

But technically/mechanically I can see why MATHEAMTICA would complain.
A function is not defined over one point, but I thought the whole
idea of piecewise function is to be able to handle this? thanks, Steve
  Reply With Quote
6 28th December 08:37
jens-peer kuska
External User
 
Posts: 1
Default piecewise integration problem.


Hi,

and I was not giving you the answer of this specific example.
I showed you how to define a picewise functions with UnitStep[]
in a way that Mathematica can handle it.

Mathematica knows several ways to define a picewise function
and it seems to be complicated to bring Switch[], Which[], If[] and
Condition[] that can be used, into a canonical form that Integrate[]
can handle. Mathematica will not try to transform all your function
definition into an expression that include only sums of UnitStep[]
function
and you have to do it. *If* you do that, Mathematica does the
integration
and it handles the discontinuity of UnitStep[x] at x==0.

Regards
Jens
  Reply With Quote
7 28th December 08:37
david w. cantrell
External User
 
Posts: 1
Default piecewise integration problem.


But it's not applicable only to that example. Rewriting in terms of
UnitStep is probably the best way to deal with piecewise-defined functions
in Mathematica if they are to be either differentiated or integrated.
Such rewriting is a generally applicable technique.


Right, but that's not because f is undefined at 0. (Define f at 0 and it
still won't work.) Mathematica didn't do it because it never (at least
AFAIK) symbolically integrates functions defined in pieces that way.

David
  Reply With Quote
8 28th December 08:37
nma124
External User
 
Posts: 1
Default piecewise integration problem.


Hello;

I asked my math professor on this.

Let me quickly recall the problem with a simple example:

given a piecwise function not defined on a single point in its
domain, I want to integrate it over a range that includes the point.

f[x_] := x /; -1 < x < 0
f[x_] := x /; 0 < x < 1
Integrate[f[x], {x, -1, 1}]

Here mma will not integrate it, but maple will:

0

Accoding to the math teacher, mma is correct in the strict mathematical
sense. Since a function must be defined at EACH point on the domain of
the integration (normal sense of integration).

However, he also said that the function can have any value at the
point 0 above (where it is not defined) and the answer will still be
the same, which is 0 for the area under the curve in this example.

So, mma is correct to complain, but maple is also correct in its
answer. It seems maple must be using hueristics here to determine
this, and it must have seen that the function is not defined over countable
number of points and 'let is pass' through the integration.

Which do you think is the better approach?
  Reply With Quote
9 28th December 08:37
nma124
External User
 
Posts: 1
Default piecewise integration problem.


I have a typo there, I meant to type

and not


funny thing is that maple did not complain at all about my typo
that why I did not notice it! This should be a bug.
  Reply With Quote
10 28th December 08:37
israel
External User
 
Posts: 1
Default piecewise integration problem.


As you were told before, neither of these is the way to do numerical
integration. evalf(Int(...)) is.

In general, Maple does not check functions for subscripts. So pick
your typical function f, enter f[foo](bar), and if f(bar)
returns some result (rather than returning unevaluated), f[foo](bar)
will probably return the same result.
The main exceptions:
1) functions whose calling sequence can include a subscript.
2) results returned via a remember table: f[foo] and f will have separate
remember tables.

Robert Israel israel@math.ubc.ca
Department of Mathematics http://www.math.ubc.ca/~israel
University of British Columbia
Vancouver, BC, Canada V6T 1Z2
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666