Azzera filtri
Azzera filtri

How do I extract one point from a piecewise function?

10 visualizzazioni (ultimi 30 giorni)
I have two piecewise functions M_xy and M_xz, which are both 1x1 syms. These function have been plotted, so I know they're structured correctly. I need to know their values at a specific point (i.e. x = 3.75) so I can alter those numbers by taking the root mean square ( sqrt( (M_xz(3.75))^2 + (M_xy(3.75))^2 ). How can I do this? I'm struggling to find any explanations online anywhere.

Risposte (2)

Paul
Paul il 13 Apr 2024
Hi Allie,
If M_xy and M_xz are sym objects, like so
syms t
M_xy = piecewise(t < 0, 0, t > 0, t);
M_xz = piecewise(t < 0, 0, t > 0, 2*t);
then use subs to evaluate each and operate on the results
r = sqrt( subs(M_xy,t,3.75)^2 + subs(M_xz,t,3.75)^2 )
r = 
Or if using symfun objects, then
M_xy(t) = piecewise(t < 0, 0, t > 0, t);
M_xz(t) = piecewise(t < 0, 0, t > 0, 2*t);
r = sqrt(M_xy(3.75)^2 + M_xz(3.75)^2)
r = 

John D'Errico
John D'Errico il 13 Apr 2024
Modificato: John D'Errico il 13 Apr 2024
You can define a piecewise function as a symbolic function. For example...
syms t
M_xy(t) = piecewise(t < 0, 0, t > 0, t); % Note the change to M_xy, making it a function.
M_xz(t) = piecewise(t < 0, 0, t > 0, 2*t);
r = sqrt(M_xy(3.75)^2 + M_xz(3.75)^2)
r = 
That is, you can now evaluate these things directly as functions of t. If you wanted to define r itself as a function of t, you could have done this
r(t) = sqrt(M_xy(t)^2 + M_xz(t)^2);
And now r is itself a function you can evaluate.
r(3.75)
ans = 
(Which is the same number as before. Strangely, it did not decide to simplify that result.) Anyway, you can even use r in a vectorized form.
r(1:.5:4)
ans = 
And of course, we can turn these into doubles, or you can use vpa.
double(r(3.75))
ans = 8.3853

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by