Using string to declare function variables
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!