How to avoid dynamic variable names when I need to differentiate a function with lots of variables?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello
Take my code for example:
n = 8;
syms f(x) distsq(x)
X=sym('x',[1 n-1]);
syms(X);
P = zeros(1,n)+0.1;
f(X) = sqrt(sum(X));
distsq(X) = sum((X-P(1:end-1)).^2) + (f-P(end)).^2;
grad_distsq = jacobian(distsq,X);
equations = grad_distsq == 0;
bounds = X > 0;
bounds = [bounds X<1];
equations = [equations bounds];
S = solve(equations,X);
The code runs fine, but I have
variables and it's troublesome to do operations with the solution structure S, which has
fields.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/239032/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/239033/image.png)
How should I remove dynamic names from the code?
1 commentaire
Matt J
le 20 Sep 2019
Modifié(e) : Matt J
le 20 Sep 2019
What names do you want instead? You chose the names in the line
X=sym('x',[1 n-1]);
You could have used any other name, e.g.,
X=sym('frank',[1 n-1]);
>> S = solve(equations,X)
S =
struct with fields:
frank1: [1×1 sym]
frank2: [1×1 sym]
frank3: [1×1 sym]
frank4: [1×1 sym]
frank5: [1×1 sym]
frank6: [1×1 sym]
frank7: [1×1 sym]
Réponses (0)
Voir également
Catégories
En savoir plus sur Formula Manipulation and Simplification 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!