How to substitute multiple values in symbolic matrix?

8 visualizaciones (últimos 30 días)
Amit Kumar
Amit Kumar el 29 de Nov. de 2013
Comentada: Youssef El Seblani el 8 de Abr. de 2018
Hi, How to substitute multiple values in symbolic matrix? With function subs, we can substitute only single value in expression. Writing nested subs(subs(...)) can work but it becomes tedious. Here is sample code:
syms x y z w
N=[ 1y+y+z,x/2 - y/3 + z/3, x*y, x+z];
Is there any way to substitute values to w,x,y,z in single line? I guess then I have to define variables like x(1), x(2) etc. and have to use for loop to change values. Is this approach correct or is there any better way?

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 29 de Nov. de 2013
syms x y z w
N=[ w+y+z,x/2 - y/3 + z/3, x*y, x+z]
X = sym(['[',sprintf('X(%d) ',1:4),']'])
out = subs(N,[x, y, z, w],X)
  2 comentarios
Deepayan Bhadra
Deepayan Bhadra el 5 de Dic. de 2016
Hi,
I wanted some explanation on how the above code works. Say my N is a 6x6 matrix with 5 symbolic variables and I want to evaluate N at one go, for 5 different values of the variables. How do I modify the above answer for that? Thank you.
Karan Gill
Karan Gill el 8 de Dic. de 2016
That answer is wrong. Please do not use string input to sym. Instead:
>> syms x y z w
N=[ 1*y+y+z,x/2 - y/3 + z/3, x*y, x+z];
vars = [w x y z];
newVars = sym('x%d',[1 4]);
subs(N,vars,newVars)
ans =
[ 2*x3 + x4, x2/2 - x3/3 + x4/3, x2*x3, x2 + x4]
Much easier to read and understand. Look up the doc for sym to understand the command I used: https://www.mathworks.com/help/symbolic/sym.html

Iniciar sesión para comentar.

Más respuestas (1)

Youssef El Seblani
Youssef El Seblani el 31 de Mzo. de 2018
Editada: Walter Roberson el 31 de Mzo. de 2018
clc
clear
syms x y z w
N=[ w+y+z,x/2 - y/3 + z/3, x*y, x+z]
A=sym('X',[1,4])
N(A) = subs(N,[x, y, z, w],A)
for example
N(1,2,3,4)
  2 comentarios
Walter Roberson
Walter Roberson el 31 de Mzo. de 2018
This will not work if I recall correctly. You cannot use a vector of symbolic variables after a variable name in order to define a function of multiple parameters. Instead each variable must be a separate parameter.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by