Jacobian of equation with left and right hand sides
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a program where the user inputs an equation in the form of a string:
'2*a + 3*b = 5*c' % (just an example, it can be any linear equation)
in sequence I list the variables and take the Jacobian:
allvars = symvar(input);
J = jacobian(input, allvars);
However, because of the "=" in the equation my output is:
J = [ 2 = 0, 3 = 0, 0 = 5]
and instead I *need*** it to be:
J = [ 2, 3, -5]
How can I solve this? Having the user input '2*a + 3*b - 5*c' is not an option.
I tried looking for a rhs/lhs (right/left hand side) function but there aren't any, the @children function is available only in R2012a and greater.
2 Kommentare
Matt Kindig
am 9 Jul. 2013
Hmmm, interesting question. Maybe you'll need to parse the input string and move everything to the left hand side first. Something like this might work:
input = '2*a + 3*b = 5*c'; %for example
sides= regexp(input, '=', 'split'); %chop by equals sign
%replace with expression where everything is on left hand side
modified = sprintf('%s - (%s)', sides{1}, sides{2});
allvars = symvar(modified);
J = jacobian( modified, allvars); %this should give the correct answer.
Akzeptierte Antwort
Daniel
am 24 Feb. 2014
1 Kommentar
Karan Gill
am 9 Mai 2017
Starting R2017a, The "lhs" and "rhs" functions are available. See my answer below.
Weitere Antworten (2)
Matt J
am 9 Jul. 2013
Could you do something like
>> str='2*a + 3*b = 5*c';
>> newstr=[strrep(str,'=','-(') , ')']
newstr =
2*a + 3*b -( 5*c)
0 Kommentare
Karan Gill
am 9 Mai 2017
Bearbeitet: Karan Gill
am 17 Okt. 2017
Starting R2017a, The "lhs" and "rhs" functions are available. See:
Here's a toy example.
>> syms a b c d
>> eqn = a+b == c+d
eqn =
a + b == c + d
>> lhs(eqn)
ans =
a + b
>> rhs(eqn)
ans =
c + d
0 Kommentare
Siehe auch
Kategorien
Mehr zu Calculus 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!