MATLAB Answers

Simplify symbolic functions: remove terms

108 views (last 30 days)
Francesco Mela
Francesco Mela on 28 Nov 2016
Commented: Walter Roberson on 29 Nov 2016
Hi I want to simplify a symbolic function in this way:
this is my function:
a*b+dx*dy+dx^2*dy+a*dx+a+dy*dz+dt*da
I want that Matlab:
  1. Remove the terms in which there is a product between dx*dy, dy*dx, dt*da, dx^2*dy etc.
  2. Make two function: In the first there are all terms that are multiplied by dx, dy, dt and in the other, the other terms.
Thanks!

  1 Comment

mor dave
mor dave on 28 Nov 2016
I think your best bet is to check simplify

Sign in to comment.

Answers (2)

Hildo
Hildo on 29 Nov 2016
Do you want to remove a specific variable
You can use
new_equation = subs(equation,var,0);
new_equation2 = simplify(equation); % Maybe is not necessary simplify in this case
Of all the cross multiplication between variables?

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 29 Nov 2016
Note: for this purpose I exclude all squared terms such as dx^2, guessing that your rule was that multiplying two or more derivatives together was going to give a result too small to matter.
syms a b dx dy dz dt da
r = a*b+dx*dy+dx^2*dy+a*dx+a+dy*dz+dt*da;
[A,B] = coeffs(r,[dx,dy,dz,da,dt], 'all');
[tf, idx] = ismember([dx,dy,dz,da,dt],B);
just_constants = cellfun(@(C) isempty(symvar(C)), num2cell(B));
first_term = sum(A(idx(tf)) .* B(idx(tf)));
second_term = sum(A(just_constants) .* B(just_constants));

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 29 Nov 2016
The code already removes dx^2*dy which you had specifically requested to be removed. What was not completely clear though was whether dx^N by itself should be removed, with N greater than 1? Is the rule that the term should be removed if the total order of derivatives is more than 1, or is the rule that the term should be removed only if there are multiple derivative variables involved?
Francesco Mela
Francesco Mela on 29 Nov 2016
Both things: the term should be removed if the total order of derivatives is more than 1 and if if there are multiple derivative variables involved. I want to remove both dx^2 both dx*dy both dx*dy^2, but not a*dx, x*dy ....
Another thing: the code works if I replace this
[A,B] = coeffs(r,[dx,dy,dz,da,dt], 'all');
with this
[A,B] = coeffs(r,[dx,dy,dz,da,dt]);
Walter Roberson
Walter Roberson on 29 Nov 2016
I did test the code with the expression you gave.
My expression originally involved some positional computations, but in mentally reviewing, I think that you are correct that the 'all' is not actually necessary, but it should not hurt.

Sign in to comment.

Sign in to answer this question.

Tags