Can't use conv() after using coeffs()? It works when manually inputting the coefficients but doesn't when it is taken using coeffs
2 ビュー (過去 30 日間)
古いコメントを表示
conv() returns an error when trying to use the outputs of coeffs()
syms s
eq1 = 2*s^2 + 3*s - 1;
N = coeffs(eq1, 'All');
eq2 = s^3 +6*s^2 + 1;
D = coeffs(eq2, 'All');
T = conv(N,D)
^this turns to error while
syms s
eq1 = 2*s^2 + 3*s - 1;
N = [2 3 -1];
eq2 = s^3 +6*s^2 + 1;
D = [1 6 0 1];
T = conv(N,D)
^this properly gives the correct result
0 件のコメント
採用された回答
Walter Roberson
2024 年 5 月 14 日
syms s
eq1 = 2*s^2 + 3*s - 1;
N = coeffs(eq1, 'All');
eq2 = s^3 +6*s^2 + 1;
D = coeffs(eq2, 'All');
T = conv( double(N), double(D))
0 件のコメント
その他の回答 (1 件)
Zinea
2024 年 5 月 14 日
Reason behind the error:
The issue you are encountering stems from the difference in the types of objects “coeffs” return versus what “conv” expects. The “coeffs” function, when used with symbolic expressions, returns a symbolic array, not a numeric array. The “conv” function, designed for numerical computations, requires numeric arrays or vectors as input.
In the first example, ‘N” and “D” are symbolic arrays because they are the result of “coeffs” applied to symbolic expressions. To use “conv” with the outputs of “coeffs” when dealing with symbolic expressions, these symbolic arrays need to be converted to numeric arrays first. In the second example, “N” and “D” are manually specified as numeric arrays, which is why “conv” works as expected.
Workaround:
If “coeffs” is still needed to be used in the code, the output of “coeffs” must be converted to “double” before being provided as input to "conv,” as is given below:
syms s
eq1 = 2*s^2 + 3*s - 1;
N_sym = coeffs(eq1, 'All');
N = double(N_sym); % Convert a symbolic array to a numeric array.
eq2 = s^3 +6*s^2 + 1;
D_sym = coeffs(eq2, 'All');
D = double(D_sym); % Convert a symbolic array to a numeric array.
T = conv(N, D);
You may refer to the following documentation links for more detail on the “coeffs” and “conv” functions, respectively:
Hope it helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Symbolic Math Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!