HP Forums
Help with Piecewise function - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Help with Piecewise function (/thread-2983.html)



Help with Piecewise function - salvomic - 02-01-2015 10:46 AM

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


RE: Help with Piecewise function - Snorre - 02-01-2015 12:34 PM

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


RE: Help with Piecewise function - salvomic - 02-01-2015 01:37 PM

(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


RE: Help with Piecewise function - Snorre - 02-01-2015 02:00 PM

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. ;-)


RE: Help with Piecewise function - salvomic - 02-01-2015 02:24 PM

yes, I'm skeptical also of our beloved Prime Smile

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 Wink

Greetings


RE: Help with Piecewise function - Snorre - 02-01-2015 02:47 PM

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), ...


RE: Help with Piecewise function - salvomic - 02-01-2015 02:51 PM

(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


RE: Help with Piecewise function - parisse - 02-01-2015 02:55 PM

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)


RE: Help with Piecewise function - salvomic - 02-01-2015 03:10 PM

(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 Wink

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...


RE: Help with Piecewise function - parisse - 02-01-2015 04:10 PM

what's wrong? Your function is odd.


RE: Help with Piecewise function - salvomic - 02-01-2015 04:43 PM

(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 Smile

Thank you

salvo


RE: Help with Piecewise function - parisse - 02-01-2015 05:44 PM

No, it's only internal (i.e. a giac c++ routine).


RE: Help with Piecewise function - Han - 02-01-2015 06:09 PM

(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 Smile

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).


RE: Help with Piecewise function - salvomic - 02-01-2015 06:12 PM

(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 Smile

Cheers