Can't use conv() after using coeffs()? It works when manually inputting the coefficients but doesn't when it is taken using coeffs
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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)
^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 commentaires
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))
0 commentaires
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 commentaires
Voir également
Catégories
En savoir plus sur Calculus dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!