Using string to declare function variables
1 view (last 30 days)
Show older comments
I have a string containing the names of the variables in the function: varlst = 'x1,x2'
I have function fx= 10*(x1 - 3)^2 + (x2 + 5)^2
I want fx(x1,x2) = 10*(x1 - 3)^2 + (x2 + 5)^2
But when I type fx(varlst) I get fx = [ 10*(x1 - 3)^2 + (x2 + 5)^2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10*(x1 - 3)^2 + (x2 + 5)^2, 0, 0, 0, 0, 10*(x1 - 3)^2 + (x2 + 5)^2, 10*(x1 - 3)^2 + (x2 + 5)^2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10*(x1 - 3)^2 + (x2 + 5)^2]
Does anyone have any idea why this is happening and what I can do to get my desired result?
2 Comments
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!