# How to replace variables in symbolic function with values from a vector

5 visualizzazioni (ultimi 30 giorni)
SM il 24 Ott 2016
Commentato: SM il 25 Ott 2016
I am working on a simple optimization code that has to account for varying number of equations. Since the existence of a type of equation adds one more variable, and the code cannot know how many it will encounter, it creates variables based on the input.
For example, the symbolic function might be:
L = L(x1, x2, v1)
L = L(x1, x2, v1, u1, u2)
...
x = sym('x',[2 1]) % variables of objective function
v = sym('v',[p 1]) % input requires p more variables
v = sym('u',[m 1]) % input requires m more variables
k = vertcat(x,v,....) % all variables based on input
The code does symbolic differentiation on k(i), and after manipulations obtains a final function.
result = (x1 - 3)^2 + (x2 - 3)^2 - v1*(3*x2 - x1 + 1) + u1*(s1^2 + x1 + x2 - 4)
The code is able to calculate values for all of the variables that exist for a particular case.
k = [x1,x2, v1, u1, s1] = [ 13/4, 3/4, -5/4, 3/4, 0]
However, I was unable to substitute these values back in the symbolic functions "L", and "result" to calculate their actual numeric values at this point, and for plotting them.
I appreciate any help on this.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Marc Jakobi il 24 Ott 2016
You probably want the function
subs()
##### 5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Marc Jakobi il 25 Ott 2016
subs(result, [x; v; u; s], k)
should work.
SM il 25 Ott 2016
Thank you Marc.

Accedi per commentare.

### Categorie

Scopri di più su Common Operations in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by