how to correctly use the function "subs"

Hi there,
I am using subs to replace some symbols in a symbolic expression Jacobian_mtx_1. The symbols that I want to be replaced are A, B and C, which are a 6*1 symbolic vector, a symbolic scalar, and a 2*1 symbolic vector, respectively. I write the code:
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , { A , B , C } , { A_val ,B_val , C_val } ) ;
where A_val is a 6*1 vector containing 6 function calls, B_val is a numeric scalar, and C_val is a 2*1 numeric vector.
However, the software reminds me that
Errors using sym/subs
Entries in second argument must be scalar.
Could anyone tell me how to resolve this issue?
Many thanks!

3 comentarios

Umar
Umar el 24 de Jul. de 2024

Hi Tony,

When using the subs function in MATLAB to substitute symbols in a symbolic expression, the replacement values must be scalar. In your case, A_val is a 61 vector, B_val is a numeric scalar, and C_val is a 21 numeric vector, which is causing the error. To resolve this issue, you need to ensure that the replacement values are scalar. One approach is to replace each element of the vector with the corresponding element in the replacement vector. Here's how you can modify your code to achieve this:

% Define symbolic vectors and scalars

syms A B C

A_val = sym('A_val', [6, 1]);

B_val = sym('B_val');

C_val = sym('C_val', [2, 1]);

% Define the symbolic expression Jacobian_mtx_1

Jacobian_mtx_1 = A + B + C; % Example expression

% Perform substitution with scalar values

for i = 1:numel(A)

    Jacobian_mtx_1 = subs(Jacobian_mtx_1, A(i), A_val(i));

end

Jacobian_mtx_1 = subs(Jacobian_mtx_1, B, B_val);

for i = 1:numel(C)

    Jacobian_mtx_1 = subs(Jacobian_mtx_1, C(i), C_val(i));

end

So, in the above modified code snippet, I iterate over each element of the symbolic vectors A and C to perform individual substitutions with the corresponding elements in A_val and C_val, respectively. For the scalar symbol B, a direct substitution is made with B_val. By ensuring that the replacement values are scalar or by individually substituting elements of vectors, you can resolve the error and successfully substitute symbols in your symbolic expression. I hope this answers your question.

Tony Cheng
Tony Cheng el 24 de Jul. de 2024
Hi Umar,
Many thanks to your reply! I think your way is feasible, but the following manner is a little bit easier
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , A , A_all ) ;
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , B , B_val ) ;
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , C , C_val) ;
cause on
it says If old and new are both vectors or cell arrays of the same size, subs replaces each element of old with the corresponding element of new.
Then we can use vector replacements directly.
Cheers
Umar
Umar el 24 de Jul. de 2024
No problem, Tony, glad to help you out. Please let us know if you have any further questions.

Iniciar sesión para comentar.

 Respuesta aceptada

Hi Tony,
I tried reproducing the issue you are facing -
syms B
A = sym('A', [6 1]);
C = sym('C', [2 1]);
A_val = [1, 2, 3, 4, 5, 6];
B_val = 7;
C_val = [8, 9];
Jacobian_mtx_1 = [A(1)*B + C(1), A(2), A(3);
A(4), A(5)*B + C(2), A(6);
B, C(1), C(2)];
disp(Jacobian_mtx_1);
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , { A , B , C } , { A_val ,B_val , C_val } ) ;
Error using sym/subs (line 156)
Entries in second argument must be scalar.
Instead of directly replacing A and C with vectors, I tried replacing the elements one by one and it wokred for me -
syms B
A = sym('A', [6 1]);
C = sym('C', [2 1]);
A_val = [1, 2, 3, 4, 5, 6];
B_val = 7;
C_val = [8, 9];
Jacobian_mtx_1 = [A(1)*B + C(1), A(2), A(3);
A(4), A(5)*B + C(2), A(6);
B, C(1), C(2)];
disp(Jacobian_mtx_1);
for i = 1:length(A)
Jacobian_mtx_1 = subs(Jacobian_mtx_1, A(i), A_val(i));
end
for i = 1:length(C)
Jacobian_mtx_1 = subs(Jacobian_mtx_1, C(i), C_val(i));
end
% Now replace B
Jacobian_mtx_1 = subs(Jacobian_mtx_1, B, B_val);
disp(Jacobian_mtx_1);

1 comentario

Tony Cheng
Tony Cheng el 24 de Jul. de 2024
Hi Kumar,
Many thanks to your reply! I think your way is feasible, but the following manner is a little bit easier
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , A , A_all ) ;
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , B , B_val ) ;
Jacobian_mtx_1 = subs( Jacobian_mtx_1 , C , C_val) ;
cause on
it says If old and new are both vectors or cell arrays of the same size, subs replaces each element of old with the corresponding element of new.
Then we can use vector replacements directly.
Cheers

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Etiquetas

Preguntada:

el 24 de Jul. de 2024

Comentada:

el 24 de Jul. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by