How can I determine the order of a symbolic differential equation?

조회 수: 11 (최근 30일)
Bill Tubbs
Bill Tubbs 2020년 7월 24일
댓글: Paul 2023년 4월 2일
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.

채택된 답변

Ayush Gupta
Ayush Gupta 2020년 9월 10일
There doesn’t exist a direct function to determine the order of a differential equation. However, there is a workaround, and we can use the reduceDifferentialOrder and get newvars from where we can get the last element and see the occurrence of t and this is one plus than the order of equation. Refer to the following code:
syms x(t) y(t) f(t)
eqs = [diff(x(t),t,t) == diff(f(t),t,t,t), diff(y(t),t,t,t) == diff(f(t),t,t)];
vars = [x(t), y(t)];
[newEqs, newVars, R] = reduceDifferentialOrder(eqs, vars)
l = length(newVars);
s = string(newVars(l,1));
order_of_equation = count(s, 't') -1;
  댓글 수: 1
Paul
Paul 2023년 4월 2일
It's a bit easier if dealing wth ODEs as in the Question, at least to find the order of the ODE
syms t s y(t) u(t) R L
diff_eqn = R*y(t) + L*diff(y(t), t) == u(t) % differential equation
diff_eqn = 
%The order (w.r.t. y(t)) is 1.
numel(odeToVectorField(diff_eqn))
ans = 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)
diff_eqn = 
numel(odeToVectorField(diff_eqn))
ans = 2
Finding the order wrt u(t) would take more work.

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

추가 답변 (2개)

Ganesh
Ganesh 2023년 4월 1일
편집: Walter Roberson 2023년 4월 1일
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
Ganesh
Ganesh 2023년 4월 1일
this is for finding the order of polynomial function
Walter Roberson
Walter Roberson 2023년 4월 1일
You can use coeffs to get the information without a loop

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


Walter Roberson
Walter Roberson 2023년 4월 1일
이동: Walter Roberson 2023년 4월 1일
p = poly2sym(randi([-9,9],1,randi(15)))
p = 
poly_degree = length(coeffs(p,'all'))-1
poly_degree = 6
  댓글 수: 3
Paul
Paul 2023년 4월 1일
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
c = 1×16
0 0 0 8 -7 8 3 -8 -4 1 9 9 -7 9 9 0
tic
for ii = 1:1e3
p = poly2sym(c);
poly_degree_s = length(coeffs(p,'all'))-1;
end
toc
Elapsed time is 2.962911 seconds.
tic
for ii = 1:1e3
poly_degree_d = numel(c) - find(c,1,'first');
end
toc
Elapsed time is 0.004062 seconds.
[poly_degree_s poly_degree_d]
ans = 1×2
12 12
Walter Roberson
Walter Roberson 2023년 4월 2일
@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.

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

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by