is there a a way to collect coefficients to the 3rd argument

5 次查看(过去 30 天)
%%%%%%%%%%%%%%%%%%%%%%% SYMBOLS:
syms c1 c2 c3 n K k alpha real
syms Omg
assume(n,'positive')
assume(alpha,'positive')
%%%%%%%%%%%%%%%%%%%%%%% GREEK SYMB:
alpha = evalin(symengine, '`α`')
alpha = 
α
omg = evalin(symengine, '`Ω`')
omg = 
Ω
%-------------------ddefine N:
N = n*( 1 + 1i*c2 ) + 0.5*alpha*sqrt(n)*(1 + 1i*c3)
Error using symengine
Unable to evaluate to Boolean.

Error in sym/symvar (line 45)
v = mupadmex('symobj::symvar', S.s, num2str(n), scell{:});

Error in sym/getSuggestionsForSym (line 1548)
varsReqSym = setdiff(string(symvar(s)), evalin('base', "string(who)"));

Error in sym/display (line 37)
symSuggestions = getSuggestionsForSym(X,namestr);
No = n*( 1 - 1i*c2 ) + 0.5*alpha*sqrt(n)*(1 - 1i*c3 )
%-----matrix :
%------Elements:
a11 = (1 + 1i*c1)*K^2 -1i*omg + 2*k*K*(1 + 1i*c1) + N
a22 = (1 - 1i*c1)*K^2 - 1i*omg - 2*k*K*(1 - 1i*c1) + No
M = [ a11 N; No a22 ]
D = det(M)
coeffs_omg = collect(D, [omg 1i])
in this bit of code i want to arrange my determinant in such it groups coefficents of omega first, then those coeffients it separates int real and imaginary(this i have done), then again in those i want to order them by K

回答(1 个)

Himanshu
Himanshu 2023-9-28
Hello,
I understand that you are trying to collect coefficients of a symbolic expression in MATLAB and order them by a specific variable, in this case, "K". You can achieve this by combining the "collect" and "coeffs" functions and then manually sorting the coefficients.
You can refer to the example below:
% extract coefficients of K
[coeffs_K, terms] = coeffs(D_collected, K);
% initialize a cell array to store coefficients sorted by the power of K
powers = arrayfun(@(term) double(feval(symengine, 'degree', term, K)), terms);
% extract the power of K from each term and sort the coefficients by the power of K
[~, sorted_indices] = sort(powers, 'descend');
sorted_coeffs = cell(1, length(terms));
% store the sorted coefficients
for i = 1:length(sorted_indices)
sorted_coeffs{i} = coeffs_K(sorted_indices(i));
end
The above code will give you a cell array "sorted_coeffs" where the coefficients are sorted by the power of "K".
You can refer to the below documentation to learn more about MATLAB's Symbolic Toolbox. Also, please refer to the documentations for the "collect", "coeff", "arrayfun" and the "feval" functions in MATLAB.

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by