use compose caculate piecewise fun

1 view (last 30 days)
,
caculate ,,,
but different picecwise expression get different outcome on ,: f(x)=piecewise(x<=0,0,x) or f(x)=piecewise(x>0,x,0); g(x)=piecewise(x<=0,0,-x^2) or g(x)=piecewise(x>0,-x^2,0).
clc,clear
syms x
f(x)=piecewise(x<=0,0,x);
g(x)=piecewise(x<=0,0,-x^2);
compose(f,f)
compose(g,g)
compose(f,g)
compose(g,f)
clc,clear
syms x
f(x)=piecewise(x>0,x,0);
g(x)=piecewise(x>0,-x^2,0);
compose(f,f)
compose(g,g)
compose(f,g)
compose(g,f)

Accepted Answer

Paul
Paul on 2 Feb 2023
Hi jin,
Perhapse I misunderstand the question, but the results all seem to be functionally equivalent.
syms x
f(x)=piecewise(x<=0,0,x);
g(x)=piecewise(x<=0,0,-x^2);
f1(x)=piecewise(x>0,x,0);
g1(x)=piecewise(x>0,-x^2,0);
fplot([compose(f,f) compose(f1,f1)])
fplot([compose(g,g) compose(g1,g1)])
fplot([compose(f,g) compose(f1,g1)])
fplot([compose(g,f) compose(g1,f1)])
  3 Comments
Paul
Paul on 2 Feb 2023
Edited: Paul on 2 Feb 2023
syms x
f(x)=piecewise(x<=0,0,x);
g(x)=piecewise(x<=0,0,-x^2);
f1(x)=piecewise(x>0,x,0);
g1(x)=piecewise(x>0,-x^2,0);
h = compose(f,g)
h(x) = 
h1 = compose(f1,g1)
h1(x) = 
0
As long as x is real, h(x) = h1(x) = 0. However, the Symbolic Math Toolbox assumes that all variables are complex unless assumed otherwise. So it has to account for what happens in h and h1 when the function inputs have non-zero imaginary parts. It looks like the compostion that forms h would not be zero in that case, keeping in mind the rules for how the SMT evaluates relational operators for non-real inputs.
If in this problem (and any other, actually) x is intended to be real, then it's best to include that assumption in the analysis. We can add that assumption now by using assume and then simplify
assume(x,'real')
h = simplify(h)
h(x) = 
0
It would be better to use
syms x real
at the top of the code.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 2 Feb 2023
Moved: Walter Roberson on 2 Feb 2023
syms x truecase falsecase
g(x) = piecewise(x<=0, truecase, falsecase)
g(x) = 
g1(x) = piecewise(x>0, truecase, falsecase)
g1(x) = 
g(1i)
ans = 
falsecase
g1(1i)
ans = 
falsecase
g(1+1i)
ans = 
falsecase
g1(1+1i)
ans = 
falsecase
g(-1+1i)
ans = 
falsecase
g1(-1+1i)
ans = 
falsecase
g(1-1i)
ans = 
falsecase
g1(1-1i)
ans = 
falsecase
Notice that all of the comparisons against complex numbers are treated as false. This makes the tests asymmetric and is why one of your cases allows for non-real elements but the other case does not.
  8 Comments
jin yong
jin yong on 6 Feb 2023
g1(x) should be defined as g1(x) = piecewise(x>0, falsecase,truecase),different with g(x)= piecewise(x<=0,truecase,falsecase)
Walter Roberson
Walter Roberson on 6 Feb 2023
We were testing which branch piecewise was evaluating to. In each case the symbolic toolbox resolved the piecewise to the second branch, the one associated with failure of the condition. It should have been a mix of first branch and second branch when complex values were passed in.
If you were to restrict your inputs to R then in each case the piecewise for both variations would give the same result for all real inputs.
The difference you observe between the two cases is that in one case the result is piecewise 0 if the input is an element of R and -x^2 otherwise, but that piecewise test is missing in the other case and it gives just 0 there.
I tracked that difference down to the fact that piecewise appears to consider the test to fail if the input is not in R, instead of converting the test to check the real() of the input as is otherwise documented for the Symbolic Toolbox. The two versions differ in how they treat complex inputs with an imaginary component known to be non-zero.
Consider -1+2i. Is that <= 0 ? Complex numbers are not mathematically orderable so you would have to say "No, it is not <= 0 because it cannot be ordered". Then test, is it > 0 ? Again you would have to say that it is mathematically not > 0 because it cannot be ordered. So -1+2i <= 0 can be said to be false, but -1+2i > 0 can also be said to be false. Whereas you are using binary reasoning to say that if the first test is false that the "opposite" of the first test must be true. Which turns out not to be the case. The situation is like trying to reason about NaN (Not A Number)

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!