简体   繁体   中英

Extracting arrays of a symbolic vector in MATLAB

I have a vector of symbolic function of the following form defined in MATLAB R2021a:

O_BF(t) =

[JTP*(diff(theta(t), t)*(w1 + w3 - w4 - w2*(cos(beta)*cos(gamma) + 
sin(alpha)*sin(beta)*sin(gamma))) - w2*(cos(gamma)*sin(beta) - 
cos(beta)*sin(alpha)*sin(gamma))*diff(say(t), t)), -JTP*(diff(phi(t), t)*(w1 + w3 - w4 - w2* 
(cos(beta)*cos(gamma) + sin(alpha)*sin(beta)*sin(gamma))) + 
w2*cos(alpha)*sin(gamma)*diff(say(t), t)), JTP*(w2*(cos(gamma)*sin(beta) - 
cos(beta)*sin(alpha)*sin(gamma))*diff(phi(t), t) + w2*cos(alpha)*sin(gamma)*diff(theta(t), 
t))]

And I want the following results, which obviously is the extracted parts of the above vector:

[JTP*(diff(theta(t), t)*(w1 + w3 - w4 - w2*(cos(beta)*cos(gamma) + 
sin(alpha)*sin(beta)*sin(gamma))) - w2*(cos(gamma)*sin(beta) - 
cos(beta)*sin(alpha)*sin(gamma))*diff(say(t), t))

-JTP*(diff(phi(t), t)*(w1 + w3 - w4 - w2* 
(cos(beta)*cos(gamma) + sin(alpha)*sin(beta)*sin(gamma))) + 
w2*cos(alpha)*sin(gamma)*diff(say(t), t))

JTP*(w2*(cos(gamma)*sin(beta) - 
cos(beta)*sin(alpha)*sin(gamma))*diff(phi(t), t) + 
w2*cos(alpha)*sin(gamma)*diff(theta(t), 
t))]

As You can see, this is clearly a vector, but I cannot extract the arrays because MATLAB considers this vector as a function of time. How can I do that?

O_BF(t) in your question is a symbolic function with respect to t that returns a matrix.

This a common issue when working with symbolic functions and Matlab symfun objects. Here's a simpler example that I think reproduces the behavior observed in the question:

syms t a; %define symbolic variables
f(t) = [t a*t 2*t^2]; %define symfun array as function of t

Calling f(1) evaluates the symfun for t = 1 rather than extracting the first element of the 1-by-3 array:

>> f(1)
 
ans =
 
[1, a, 2]

To access the elements of the underlying array one must assign the contents to an intermediate symbolic variable:

g = f(t); %or even f=f(t) to redefine f as sym rather than symfun
g(1)

>> g(1)
 
ans =
 
t

This is equivalent to using the formula function:

g = formula(f); 
g(1)

>> g(1)
 
ans =
 
t

I know of no current way to directly access the elements of an array defined as a symfun without going through the above steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM