Chimi
Chimi

Reputation: 313

MATLAB Symbolic coeffs issues

I'm working with MATLAB's symbolic toolbox, and I'm having some issues pulling out the coefficients of derivatives. Maybe MATLAB can't do what I am looking for. Anyway, code that reproduces the issue I'm having is shown below:

clear ; close all; clc;
syms a b t
x = sym('x(t)');
y = sym('y(t)');
syms a b;

ra = a*cos(x);
radot = diff(ra, t);
xdot = diff(x,t);
ydot = diff(y,t);

% This one works as expected
works = coeffs(radot(1), xdot)
% This doesn't work as expected
fails = coeffs(radot(1), ydot)

Comments in the above code sections highlight what works and what does not work as expected. Specifically, the outputs are:

radot =
-a*sin(x(t))*diff(x(t), t)
works =
-a*sin(x(t))
fails =
-a*sin(x(t))*diff(x(t), t)

Does anyone know why this happens or whether I'm doing something wrong?

Upvotes: 0

Views: 336

Answers (2)

horchler
horchler

Reputation: 18484

It looks like you may be using coeffs for something that it wasn't meant for. Look at the help. It's designed to give the coefficients of a polynomial, not wether a differential equation is a function of one variable or another.

If you happen to be trying to take the derivative with respect to xdot and ydot, you can do this

syms z; % Subsitution variable for diff(x(t), t) and diff(y(t), t)
diff(subs(radot(1),xdot,z),z)
diff(subs(radot(1),ydot,z),z)

which returns

ans =

-a*sin(x(t))


ans = 

0

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112669

The result of the last line is constant with respect to ydot, and therefore the whole expression is seen as a single coefficient (a constant).

What is your expected result for coeffs(radot(1), ydot)?

Upvotes: 2

Related Questions