Can't use conv() after using coeffs()? It works when manually inputting the coefficients but doesn't when it is taken using coeffs
조회 수: 4 (최근 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!