필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Tlotlo Oepeng
Tlotlo Oepeng 2022년 7월 15일
답변: Himanshu 2023년 9월 28일
%%%%%%%%%%%%%%%%%%%%%%% 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