Jacobian of equation with left and right hand sides
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Matt Kindig
2013 年 7 月 9 日
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.
採用された回答
Daniel
2014 年 2 月 24 日
1 件のコメント
Karan Gill
2017 年 5 月 9 日
Starting R2017a, The "lhs" and "rhs" functions are available. See my answer below.
その他の回答 (2 件)
Matt J
2013 年 7 月 9 日
Could you do something like
>> str='2*a + 3*b = 5*c';
>> newstr=[strrep(str,'=','-(') , ')']
newstr =
2*a + 3*b -( 5*c)
0 件のコメント
Karan Gill
2017 年 5 月 9 日
編集済み: Karan Gill
2017 年 10 月 17 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!