How can I determine the order of a symbolic differential equation?
Show older comments
I'm writing a function that takes a differential equation in symbolic form as an argument and I want to determine the order of the equation in terms of certain variables.
Example 1 A first-order system:
syms t s y(t) u(t) R L
diff_eqn = R*y(t) + L*diff(y(t), t) == u(t); % differential equation
The order (w.r.t. y(t)) is 1.
Example 2 A second-order system:
syms t s y(t) u(t) omega_n z K
diff_eqn = 1/omega_n^2*diff(y(t), t, 2) + 2*z/omega_n*diff(y(t), t) + y(t) == K*u(t);
The order is 2.
I would also like to know the order w.r.t. u(t) if possible as well which in general might not be 0.
Accepted Answer
More Answers (2)
Ganesh
on 1 Apr 2023
Edited: Walter Roberson
on 1 Apr 2023
function order = order_polynomial(poly1,x)
count=0;
temp=1;
while(temp~=0)
poly1=diff(poly1,x);
if poly1==0
temp=0;
else
count=count+1;
end
end
disp(count);
end
2 Comments
Ganesh
on 1 Apr 2023
this is for finding the order of polynomial function
Walter Roberson
on 1 Apr 2023
You can use coeffs to get the information without a loop
Walter Roberson
on 1 Apr 2023
Moved: Walter Roberson
on 1 Apr 2023
p = poly2sym(randi([-9,9],1,randi(15)))
poly_degree = length(coeffs(p,'all'))-1
3 Comments
Bill Tubbs
on 1 Apr 2023
Moved: Walter Roberson
on 1 Apr 2023
If the coefficient vector is double there's no need to go the Symbolic route.
rng('default')
c = [0 0 0 randi([-9,9],1,randi(15))] % add some leading zeros
tic
for ii = 1:1e3
p = poly2sym(c);
poly_degree_s = length(coeffs(p,'all'))-1;
end
toc
tic
for ii = 1:1e3
poly_degree_d = numel(c) - find(c,1,'first');
end
toc
[poly_degree_s poly_degree_d]
Walter Roberson
on 2 Apr 2023
@Paul is of course correct that if you have a vector of coefficients then the difference between the length of the vector and the position of the first non-zero tells you about the degree.
However... the original question deals with symbolic polynomials. My creation of p with intermediate numeric form was just to have some polynomial to work with, and to demonstrate that I my code worked with polynomials of different degrees, not just something that "happened" to work with a particular length.
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!