How do you create a symbolic function of symbolic vectors?
4 views (last 30 days)
Show older comments
What I'm trying to write is this:
syms T real
r=sym('r', [1 3], 'real');
v=sym('v', [1 3], 'real');
pv=sym('pv', [1 3], 'real');
pr=sym('pr', [1 3], 'real');
u=sym('u', [1 3], 'real');
syms Drag(r,v)
f = dot(pr, v) + dot(pv,( - 1/norm(r)^3 * r + T * u - Drag))
subexpr(simplify(-gradient(f,[r])))
subexpr(simplify(-gradient(f,[v])))
What I want is to get an expression out that has the partial derivatives of the Drag function with respect to r and v (allowing me to divide-and-conquer and go on and symbolically differentiate the actual Drag function).
What I'm getting is that the syms Drag(r,v) line replaces the definitions that I have of r and v with 1x1 symbols and then the dot(pr,v) operation fails with "A and B must be the same size".
I'm not sure that the question that I've asked is correct, but I'd like Drag to be a vector-valued function of the r and v vectors (without giving the actual expression for Drag).
0 Comments
Answers (1)
Walter Roberson
on 17 Dec 2022
Have a look at this:
syms x [1,2]
syms y [1,2]
f(x,y) = x.^2 + y.^2
Notice that the resulting function is a function with four inputs, not a function with two inputs.
It is not possible in MATLAB to define a symbolic function that accepts vectors in positions and treats them as vectors in the calculations.
You can define
syms X
g(X) = X.^2
g(x)
That is, you can define a function that operates on scalar variables but when called with vectors does element-wise calculations (under restrictions -- there are definitely times this does not work out.) You can use vector-valued names in a function -- but if you use the vector-valued variables as the dummy variables standing in for what is being passed in, then the function will treat each one as a separate parameter.
You can use the relatively new symmatrix to write formulas in which the names are understood to stand in for fixed-sized vectors or arrays -- however, you cannot create a function from them. You can symmatrix2sym to convert to something that can appear on the right side of a symbolic function definition... but as soon as you convert the left side as well, you run into the problem that the names will each expand to a separate parameter.
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!