How to insert values for a symbolic variable

I'm calculating some gradients with symbolic variables and after that I want to insert values for the variables.
So I have a function depandent of multiple variables x(1),..., x(20)
and I want to insert some values for the variables, something like
etc.
such that the function replaces all the x(1) values with 10 and x(20) values with 14
How is that done? Should be simple but I can't find anything helpful explaining how to solve this

 Respuesta aceptada

madhan ravi
madhan ravi el 22 de Jun. de 2020
subs(f, {x1, x20}, {10, 14})

10 comentarios

John Miller
John Miller el 22 de Jun. de 2020
Thank you! But how can I handle this when I add the values to a vector x?
madhan ravi
madhan ravi el 22 de Jun. de 2020
Editada: madhan ravi el 22 de Jun. de 2020
syms x [1 20]
x_vars = num2cell(x);
x_vals = num2cell(1:20); % example values for x vector
subs(f, {x_vars{:}}, {x_vals{:}})
% for older versions
x = sym('x', [1, 20]);
syms(x)
x_vars = num2cell(x);
x_vals = num2cell(1:20); % example values for x vector
subs(f, {x_vars{:}}, {x_vals{:}})
if it's not what you are looking for, you need to illustrate with a SHORT example and the desired output.
syms x [1 20]
x_vars = num2cell(x);
x_vals(1) = 1.13;
....
subs(f1, {x_vars}, {x_vals})
Gives the following error
Error using sym/subs>normalize (line 231)
Entries in second argument must be scalar.
Error in sym/subs>mupadsubs (line 157)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
Error in new_file(2) (line 122)
subs(f1, {x_vars}, {x_vals})
subs(f1, x_vars{1}, x_vals{1}) % the number of variables should be equal to the number of values that you want to substitute for.
John Miller
John Miller el 22 de Jun. de 2020
I have 20 Variables and the vector x_vals cotains 20 values (I wanted to indicate that with the "....")
John Miller
John Miller el 22 de Jun. de 2020
I made a new answer to keep discussing. Thanks for the effort though
madhan ravi
madhan ravi el 22 de Jun. de 2020
Editada: madhan ravi el 22 de Jun. de 2020
P.S: By the way stop adding answers just to make a comment, it makes the thread inconsistent.
This is what happens when you refuse to read something.
clear all
x = sym('x_%d',[13 1],'real');
y = sym('y_%d',[3 1],'real');
f1 = x(1)*x(2)
x_vars = num2cell(x(1:2));
x_vals(1)=1.13
x_vals(2)=1.23
x_vals = num2cell(x_vals)
Wanted = subs(f1, {x_vars{:}}, {x_vals{:}})
Thank you. I am not refusing to read, but the statement
x_vals = num2cell(1:20); % example values for x vector
was not well made in order to understand that each value has to be converted later on

Iniciar sesión para comentar.

Más respuestas (1)

John Miller
John Miller el 22 de Jun. de 2020
>> x = sym('x_%d',[13 1],'real');
>> y = sym('y_%d',[3 1],'real');
>> f1 = x(1)*x(2)
f1 =
x_1*x_2
>> x_vars = num2cell(x);
>> x_vals(1)=1.13
x_vals =
1.1300
>> x_vals(2)=1.23
x_vals =
1.1300 1.2300
So far so good
>> subs(f1, {x_vars{:}}, {x_vals{:}})
Brace indexing is not supported for variables of this type.
>> subs(f1, {x_vars}, {x_vals})
Error using sym/subs>normalize (line 231)
Entries in second argument must be scalar.
Error in sym/subs>mupadsubs (line 157)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);
It is possible to pass in one variable at the time, but that is not what I'm trying to do

Categorías

Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Jun. de 2020

Comentada:

el 22 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by