How do I extract one point from a piecewise function?

2 次查看(过去 30 天)
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.

回答(2 个)

Paul
Paul 2024-4-13
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 2024-4-13
编辑:John D'Errico 2024-4-13
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

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by