Help with Piecewise function
02-01-2015, 10:46 AM
Post: #1
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
Help with Piecewise function
hi,
I would like to evaluate "square wave" functions (also with Fourier series...), so I create a "piecewise" function:
f(t):=PIECEWISE(-π<t<0,-1,t=0,0,t=-π,0,t=π,0,0<t<π,1)
The interval of values is -pi < t < pi and the function is constant (1 or -1) except in 0, π, -π

If I try f(-4) I get 1 but I should get "No case applies", I think...
f(-3) also 1, but it should be -1
and so on...
I input integrate(f(t),x,-pi,pi) and I get "No case applies"...

Please, help me to find where is the error... Sure I didn't understand how to use "pisewise()"; I read the help...

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-01-2015, 12:34 PM (This post was last modified: 02-01-2015 12:47 PM by Snorre.)
Post: #2
 Snorre Member Posts: 101 Joined: Dec 2013
RE: Help with Piecewise function
Hello,

I think, the Prime doesn't understand "0<t<π" the way you'd expect.
It does a (0<t)<π, which means: if the result of (0<t) -- a boolean 0 or 1 -- is less than π -- which is always true -- then ...

Just write "0<t AND t<π".

Furthermore, there's no "else" case (so f(-4) would be undefined if written with ANDs, but is catched by the always-true (0<t)<π).
I think your function should look more like:

f(t):=piecewise(-π<t and t<0,-1, 0<t and t<π,1, 0)

Greetings
02-01-2015, 01:37 PM
Post: #3
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Help with Piecewise function
(02-01-2015 12:34 PM)Snorre Wrote:  ...
I think your function should look more like:

f(t):=piecewise(-π<t and t<0,-1, 0<t and t<π,1, 0)

Hi Snorre,
thank you.
I tried to do like you suggest:
f(t):=piecewise(-π<t AND t<0,-1, t=0, 0, 0<t AND t<π, 1)
If I, i.e., integrate the function I get [undef] after a warning "Piecewise definite integration: can only handle linear < or > condition"...
In other case it works: f(2) -> 1, f(-2) -> -1, f(4) -> "No case applies"

If I input exactly your formula I get t -> [ABS(π<t AND t<0,-1, 0<t AND t<π,1, 0]: perhaps the final 0 make the difference...

Regards
Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-01-2015, 02:00 PM
Post: #4
 Snorre Member Posts: 101 Joined: Dec 2013
RE: Help with Piecewise function
Hello Salvo,

yes you're right: the final 0 (the default case) matters, if you want your function to be defined everywhere.
Additionally: as the warning told you, a case t=0 (a single point) isn't very meaningful to integration (just think about it a bit). Nevertheless the Prime does it and it's up to you to be always a bit sceptical about machine-generated results. ;-)
02-01-2015, 02:24 PM
Post: #5
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Help with Piecewise function
yes, I'm skeptical also of our beloved Prime

My function has a discontinuity in 0 (and also in -π and π) and is a periodic one.
Analyzing it with Fourier analysis I should have bk = int(f(t)*cos(k*t),x,-pi,pi) -> 0 if even, 4/(k*pi) is odd.
With the above assumptions in Prime I get 0 (after the right warning about integration: "Piecewise definite integration: can only handle linear < or > condition")...

Ok, I'm asking too to Prime

Greetings

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-01-2015, 02:47 PM (This post was last modified: 02-01-2015 03:28 PM by Snorre.)
Post: #6
 Snorre Member Posts: 101 Joined: Dec 2013
RE: Help with Piecewise function
You're integrating only over -π to π, why not simplify your function a bit: f(t):=piecewise(t<0,-1,1)
That way I had some success with b(f,k):=int(f(t)*sin(k*t),t=-π..π)/π and b(f,1), b(f,2), ...
02-01-2015, 02:51 PM
Post: #7
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Help with Piecewise function
(02-01-2015 02:47 PM)Snorre Wrote:  As far as I remember, your function (a superposition of rectangulars) should -- in theory -- transform to values from a superposition of some sinc-like functions (with all ak's being zero).
I doubt the Prime can do that, if any CAS. I think it's even hard for mathematicians (which I'm not, so have to rely on fourier transformation tables in my math book).

You are right, and yes, I agree with you!

However I'll try with other piecewise function to test Prime.

Thank you a lot

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-01-2015, 02:55 PM
Post: #8
 parisse Senior Member Posts: 1,322 Joined: Dec 2013
RE: Help with Piecewise function
Just define
f(t):=piecewise(t<0,-1, 1),
it's the same as your function on [-pi,pi] but in a much simpler form that int can handle.
Then (in Xcas), assume(n,integer); fourier_bn(f(t),t,2*pi,n,-pi) returns (-(-1)^n*2+2)/(n*pi)
As the warning explains, int can not handle piecewise condition that are not linear, in other words you must cut the definition interval in parts, each part being defined by one linear condition and implicit conditions deduced from the fact that previous case were not taken. For example, if a function is defined in [-inf,-2] as blabla, in [-2,0] as blurp, and in [0,inf] as gasp, do f(t):=piecewise(t<-2,blabla,t<0,blurp,gasp)
02-01-2015, 03:10 PM
Post: #9
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Help with Piecewise function
(02-01-2015 02:55 PM)parisse Wrote:  Just define
f(t):=piecewise(t<0,-1, 1),
it's the same as your function on [-pi,pi] but in a much simpler form that int can handle.
Then (in Xcas), assume(n,integer); fourier_bn(f(t),t,2*pi,n,-pi) returns (-(-1)^n*2+2)/(n*pi)
As the warning explains, int can not handle piecewise condition that are not linear, in other words you must cut the definition interval in parts, each part being defined by one linear condition and implicit conditions deduced from the fact that previous case were not taken. For example, if a function is defined in [-inf,-2] as blabla, in [-2,0] as blurp, and in [0,inf] as gasp, do f(t):=piecewise(t<-2,blabla,t<0,blurp,gasp)

I hope they would implement fourier_an, fourier_bn and fourier_cn soon in the Prime's CAS!
This will solve my case

However here, even if I try with your new piecewise, I get (with my program for Fourier Coefficient) {1, 0,0} always, and it's wrong...

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-01-2015, 04:10 PM
Post: #10
 parisse Senior Member Posts: 1,322 Joined: Dec 2013
RE: Help with Piecewise function
what's wrong? Your function is odd.
02-01-2015, 04:43 PM (This post was last modified: 02-01-2015 05:55 PM by salvomic.)
Post: #11
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Help with Piecewise function
(02-01-2015 04:10 PM)parisse Wrote:  what's wrong? Your function is odd.

oh, you are right!
f(t)*cos(t) should be odd and the right formula should be, in this case: bk = (1/π)*Int(f(t)*sin(kt),t,-pi,pi) and all ak are 0...
The result (in my book) it's 4/(kπ) if k is odd (like the formula of XCAS: fourier_bn(f(t),t,2*pi,n,-pi) returns (-(-1)^n*2+2)/(n*pi)), I think...

I must rewrite my program for Fourier!

Off topic: there is in Prime a way to check if a function is even or odd? It would be useful for my program

Thank you

salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-01-2015, 05:44 PM
Post: #12
 parisse Senior Member Posts: 1,322 Joined: Dec 2013
RE: Help with Piecewise function
No, it's only internal (i.e. a giac c++ routine).
02-01-2015, 06:09 PM
Post: #13
 Han Senior Member Posts: 1,882 Joined: Dec 2013
RE: Help with Piecewise function
(02-01-2015 04:43 PM)salvomic Wrote:  Off topic: there is in Prime a way to check if a function is even or odd? It would be useful for my program

You can do a naive check: g(x)-g(-x) == 0 then g(x) is even; g(x)+g(-x) == 0 then g(x) is odd; may need to use the simplify() command. This is not guaranteed to work because the == operator checks for equality in a much stricter sense. That is, if the left hand side doesn't simplify down to 0, and this is due to the CAS being unable to simplify to 0, it may treat the comparison as false even if both sides are mathematically identical to 0.

You could randomly create, say, 50 different values of x and test the same equalities. If equality holds in all test cases, then there is a high probability your function is even (or odd, or neither, depending on which tests hold true).

Graph 3D | QPI | SolveSys
02-01-2015, 06:12 PM
Post: #14
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Help with Piecewise function
(02-01-2015 06:09 PM)Han Wrote:  You can do a naive check: g(x)-g(-x) == 0 then g(x) is even; g(x)+g(-x) == 0 then g(x) is odd; may need to use the simplify() command. This is not guaranteed to work because the == operator checks for equality in a much stricter sense. ...

well, thanks a lot!
It's good enough, like theory

Cheers

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
 « Next Oldest | Next Newest »

User(s) browsing this thread: