r =
How do I extract one point from a piecewise function?
Show older comments
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.
Answers (2)
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);
r = sqrt( subs(M_xy,t,3.75)^2 + subs(M_xz,t,3.75)^2 )
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)
John D'Errico
on 13 Apr 2024
Edited: John D'Errico
on 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)
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)
(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)
And of course, we can turn these into doubles, or you can use vpa.
double(r(3.75))
Categories
Find more on Assumptions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!