Differentiation with str2sym
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I am trying to differentiate usinf str2sym but i seem to get some errors.
Here is the code:
clear all
y(1) = str2sym ('x(1)');
y(2) = str2sym ('x(2)');
f = y(1) + y(2);
diff(f, y(1))
This is the error i get
Error using sym/diff (line 70)
Second argument must be a variable or a nonnegative integer specifying the number of
differentiations.
Error in prove (line 8)
diff(f, y(1))
0 Kommentare
Antworten (1)
Walter Roberson
am 12 Mai 2020
The error message is correct. You can only differentiate with respect to an entire variable, not with respect to a function call like x(1)
x = sym('x', [1 2]);
y(1) = str2sym ('x(1)');
y(2) = str2sym ('x(2)');
y = mapSymType(y, 'x', @(e) x(children(e)));
f = y(1) + y(2);
diff(f, y(1))
2 Kommentare
Walter Roberson
am 15 Jun. 2020
x = sym('x', [1 2]);
y(1) = str2sym ('x(1)');
y(2) = str2sym ('x(2)');
Y = mapSymType(y, 'x', @(e) x(children(e)));
f = Y(1)^2 + Y(2);
df = diff(f, Y(1));
df = subs(df, x, y);
Siehe auch
Kategorien
Mehr zu Code Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!