필터 지우기
필터 지우기

Jacobian of equation with left and right hand sides

조회 수: 4 (최근 30일)
Daniel
Daniel 2013년 7월 9일
편집: Karan Gill 2017년 10월 17일
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
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
Daniel 2013년 7월 10일
Hi Matt,
Thank you for your swift reply.
Currently I'm doing something like this as a temporary fix but I was hoping to achieve a solution using the symbolic toolbox.

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel
Daniel 2014년 2월 24일
Oh, I didn't realize I hadn't given this thread closure.
I went with Matt Kindig's approach of making the symbolic equation completely left sided, this is a minimum example:
sumblocks = {'y = x1 + x2'}
sides = regexp(sumblocks, '=', 'split');
for i=1:size(sides,1)
sumblocks{i} = sprintf('%s - (%s)', sides{i}{1}, sides{i}{2});
end
The output given in "sumblocks" will be a left-sided equation.
  댓글 수: 1
Karan Gill
Karan Gill 2017년 5월 9일
Starting R2017a, The "lhs" and "rhs" functions are available. See my answer below.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt J
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)

Karan Gill
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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by