Multiplication of non-zero symbolic functions yields zero answer

1 visualizzazione (ultimi 30 giorni)
I want to multiply two symbolic functions. However at the point of multiplying the functions I do not know which combination of the two variables are variable for each of the functions (findSymType(f,'variable') can be either empty, [t1], [t2], or [t1 t2] and the same applies to g).
The code below shows what I want to do.
syms f(t1,t2) g(t1,t2)
assume(t1, 'real')
assume(t2, 'real')
f(t1, t2) = piecewise(t1 <= 2, 1/2, 0)
g(t1, t2) = piecewise(t2 < 1, 1/2 - t2/2, 1 <= t2, 0)
a=f*g
res1 = a(0.5, 0.5)
res2 = f(0.5, 0.5) * g(0.5, 0.5)
When I multiply the two symbolic functions to yield a new symbolic function 'a', I unexpetedly yield 0. I also checked this answer by computing the value of the function for values of t1 and t2 that should yield a non-zero example. Specifically I would want res1 to yield the value of res2 (1/8) and a to yield something like:
a(t1,t2)=piecewise(t1 <= 2 & t2 < 1, 1/4-t2/4, 0)
How do I get 'a' to yield this answer?
I'll be thankful for any help!

Risposta accettata

Paul
Paul il 25 Feb 2022
Modificato: Paul il 25 Feb 2022
Disclaimer: The doc page shows no examples of multivariate piecewise functions. I assume that is ok and that piecewise still just marches through each condition in order as it does in the single variable case.
It seems like if f does or does not use the "otherwiseval" in its definiton then so must g.
syms f(t1,t2) g(t1,t2)
assume(t1, 'real')
assume(t2, 'real')
f(t1, t2) = piecewise(t1 <= 2, 1/2, 0)
f(t1, t2) = 
g(t1, t2) = piecewise(t2 < 1, 1/2 - t2/2, 0) % use the "otherwiseval" option for g
g(t1, t2) = 
a=f*g
a(t1, t2) = 
res1 = a(0.5, 0.5)
res1 = 
res2 = f(0.5, 0.5) * g(0.5, 0.5)
res2 = 
fsurf(a)
Now try not using otherwiseval in f and g
f(t1, t2) = piecewise(t1 <= 2, 1/2, t1>2,0);
g(t1, t2) = piecewise(t2 < 1, 1/2 - t2/2, 1 <= t2, 0);
a1 = f*g
a1(t1, t2) = 
fsurf(a1)

Più risposte (0)

Tag

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by