Can't use conv() after using coeffs()? It works when manually inputting the coefficients but doesn't when it is taken using coeffs

6 vues (au cours des 30 derniers jours)
Marc Joseph le 14 Mai 2024
Réponse apportée : Zinea le 14 Mai 2024
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)
Error using conv2
Invalid data type. First and second arguments must be numeric or logical.

Error in conv (line 32)
c = conv2(a(:),b(:));
^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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson le 14 Mai 2024
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))
T = 1x6
2 15 17 -4 3 -1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Plus de réponses (1)

Zinea le 14 Mai 2024
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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Catégories

En savoir plus sur Calculus dans Help Center et File Exchange

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by