use compose caculate piecewise fun
1 次查看(过去 30 天)
显示 更早的评论
,
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)
0 个评论
采纳的回答
Paul
2023-2-2
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 个评论
Paul
2023-2-2
编辑:Paul
2023-2-2
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)
h1 = compose(f1,g1)
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)
It would be better to use
syms x real
at the top of the code.
更多回答(1 个)
Walter Roberson
2023-2-2
移动:Walter Roberson
2023-2-2
syms x truecase falsecase
g(x) = piecewise(x<=0, truecase, falsecase)
g1(x) = piecewise(x>0, truecase, falsecase)
g(1i)
g1(1i)
g(1+1i)
g1(1+1i)
g(-1+1i)
g1(-1+1i)
g(1-1i)
g1(1-1i)
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 个评论
Walter Roberson
2023-2-6
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!