user3309479
user3309479

Reputation: 327

Matlab get symbols from matrix to assign value

I am creating a matrix of symbols and I declare a function using them:

x = syms('x', [1 2]);
f = x(1) + x(2)

So x and f are:

x = [x1 x2];
f = x1 + x2

Now I want to give values to x1 and x2 in a for loop and evaluate f. But when I use:

x(1) = value;

then x becomes:

x = [1 x2]

and x1 is lost, so I cannot evaluate f. How can I assign a value to x1, x2, ..., xn and then evaluate f?

Upvotes: 0

Views: 73

Answers (1)

OmG
OmG

Reputation: 18838

You should use subs like the following:

subs(f,x1,value)

instead of replacing symbol of x1 with a value. You can see the details of the function here.

Upvotes: 1

Related Questions