Symbolic Functions of Symbolic Vectors

7 vues (au cours des 30 derniers jours)
Alex B
Alex B le 3 Fév 2023
Réponse apportée : Raghvi le 14 Fév 2023
I have a function where x and y are both vectors of an arbitrary length. The function d is a small part which appears many times in a larger function and I'd like to be able to have the derivatives of d show up as as opposed to the behavior that occurs if I fully define . However, if I try to do this with something like:
syms d(x,y) real
syms x y real [1 4]
I'll end up with vectors:
x = [x1, x2, x3, x4]
y = [y1, y2, y3, y4]
Which means that if I take the Jacobian of this function I get:
jacobian(d,x) = [0,0,0,0]
As opposed to something like:
jacobian(d,x) = [diff(d,x1), diff(d,x2), ....]
Which is my desired behavior.
Additionally, if I use syntax like:
syms x [1 4]
syms d(x) [1 4] matrix keepargs
jacobian(d,x)
I get the error:
Incorrect number or types of inputs or outputs for function 'jacobian'.
And if I use the syntax:
syms x [1 4] matrix
syms d(x) [1 4] matrix keepargs
diff(d,x)
I get the output:
diff(d(x), x)
Calling Jacobian gives the same error as before and attempting to differentiate w/r.t. any of the variables within symmatrix x throws an error saying it isn't a recognized variable.
Does anyone know how to get that behavior? In this case would I need to explicitly define something like ?
I'm working on r2023a right now if that helps.

Réponse acceptée

Raghvi
Raghvi le 14 Fév 2023
I understand that you are trying to figure out the partial derivatives of your function using the Jacobian function, and your desired output is jacobian(d,x) = [diff(d,x1), diff(d,x2), ....]
The following piece of code worked for me:
>> syms x y [1 4] real
>> d(x,y)= sqrt((x-x0).^2 + (y-y0).^2);
>> jacobian(d,x)
For more information on Jacobian, you can refer to the documentation:

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Symbolic Math Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by