Extract vector of positions from a parmetric piecewise
3 views (last 30 days)
Show older comments
i have a problem with piecewise. i need to extract values for each time instant. then substitute a vector of times (for example, t = 1:10) for the symbolic variable.
the end goal is to get a vector that has within it the values calculated at the instants of time.
below I have attached the qd function.
I know I have to use the children function and the subs function. but I don't know how to use them
syms t
qd=piecewise(t<tk(k),qd,tk(k)<=t<tk(k+1),a0(k)+a1(k)*(t-tk(k))+a2(k)*(t-tk(k))^2+a3(k)*(t-tk(k))^3)
0 Comments
Answers (1)
Pratheek
on 23 May 2023
Hi Teodoro,
I understand that you want to extract the values for each time instant and substitute a vector of times for the symbolic variable. You can follow these steps to do that:
1. Use the `children` function to get the conditions and expressions within the `piecewise` function:
[pw_conditions, pw_expressions] = children(qd);
This will give you two arrays: `pw_conditions` with the conditions and `pw_expressions` with the expressions.
2. Use the `subs` function to substitute the vector of times `t = 1:10` for the symbolic variable `t` in each expression:
pw_expressions_at_t = subs(pw_expressions, t, 1:10);
This will give you the values of the expressions at each instant of time.
3. Now combine the values of the expressions at each instant of time into a vector:
values = [pw_expressions_at_t{:}];
This will concatenate the array of values from each expression into a single vector.
Putting it all together:
syms t
qd = piecewise(t<tk(k),qd,tk(k)<=t<tk(k+1),a0(k)+a1(k)*(t-tk(k))+a2(k)*(t-tk(k))^2+a3(k)*(t-tk(k))^3)
[pw_conditions, pw_expressions] = children(qd);
pw_expressions_at_t = subs(pw_expressions, t, 1:10);
values = [pw_expressions_at_t{:}];
This should give you the vector of values calculated at the instants of time.
You can refer to this documentation link for more info: Conditionally defined expression or function - MATLAB piecewise (mathworks.com)
0 Comments
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!