Extract vector of positions from a parmetric piecewise

조회 수: 4 (최근 30일)
Teodoro Lima
Teodoro Lima 2022년 9월 28일
답변: Pratheek 2023년 5월 23일
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)

답변 (1개)

Pratheek
Pratheek 2023년 5월 23일
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)

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by