Creating an inline-function in dependence of a parameter
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I want to use the newtzero-function which is not able to handle functions with parameters, so I thougth about writing a method which returns an inline function depending on a parameter, for example: fun_gen (5) should return inline('5*x^2').
Any guess?
0 个评论
回答(3 个)
Oleg Komarov
2011-5-31
c = 5;
f1 = inline([sprintf('%d',c) '*x^2'])
f2 = str2func(['@(x)' sprintf('%d',c) '*x^2'])
isequal(f1(2),f2(2)) % ok
0 个评论
Walter Roberson
2011-5-31
You can do it with anonymous functions instead of inline:
fun_gen = @(c) @(x) c*x^2;
Note that if you do this, then any one generated function will show c when it is displayed instead of the "captured" value of c, but it does work properly.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!