How do I extract one point from a piecewise function?

31 visualizaciones (últimos 30 días)
Allie Ludovice
Allie Ludovice el 13 de Abr. de 2024 a las 0:20
Editada: John D'Errico el 13 de Abr. de 2024 a las 1:21
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.

Respuestas (2)

Paul
Paul el 13 de Abr. de 2024 a las 0:31
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 el 13 de Abr. de 2024 a las 1:17
Editada: John D'Errico el 13 de Abr. de 2024 a las 1:21
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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by