change parameter values in anonymous function

My problem is one function passing to me a Anonymous function handle, for example,
f_handle = function1(a1,a2,....)
% Assuming f_handle = @(x,y) [a*(x+y),b*(x-y)], a and be are unknown parameters
As I only get this handle, (and I know there are two unknown parameters of a and b), now I want to change (set) the parameter values of a and b, I don't know how to do it.
b=1; a=1:10;
x = 1; y =2;
for i=1:10
% how to passing a(i) and b in the anonymouos function
mm{i} = f_handle(x,y);
end
Should I redefine a anonymous function or by other way?

 採用された回答

madhan ravi
madhan ravi 2020 年 7 月 14 日
編集済み: madhan ravi 2020 年 7 月 14 日

0 投票

b = 1;
a = 1:10;
x = 1;
y = 2;
mm = cell(1, 10);
f_handle = @(x,y,a,b) [a*(x+y),b*(x-y)];
for ii = 1:10
mm{ii} = f_handle(x,y,a(ii),b);
end
celldisp(mm)

9 件のコメント

zongxian
zongxian 2020 年 7 月 14 日
編集済み: zongxian 2020 年 7 月 14 日
I'm sorry to bother you. I read the web page, but I still confusing. Your meaning is redefine a anonymous function which contains (x,y,a,b)? Can you give me a sample? Thank you.
madhan ravi
madhan ravi 2020 年 7 月 14 日
b = 1;
a = 1:10;
x = 1;
y = 2;
mm = cell(1, 10);
f_handle = @(x,y,a,b) [a*(x+y),b*(x-y)];
for ii = 1:10
mm{ii} = f_handle(x,y,a(ii),b);
end
celldisp(mm)
zongxian
zongxian 2020 年 7 月 14 日
編集済み: zongxian 2020 年 7 月 14 日
Maybe I didn’t make it clear, I only know f_handle (other funciton pass to me, it is not a fixed form, only know that there are two unknown parameters of a and b), now I set values for a and b, then calculation based on this f_handle, but your codes rewrite the f_handle (I think it should be auto redefine).
madhan ravi
madhan ravi 2020 年 7 月 14 日
doc matlabFunction % I can’t help further you are making a simple thing really hard to understand
zongxian
zongxian 2020 年 7 月 14 日
@madhan ravi thank you.
madhan ravi
madhan ravi 2020 年 7 月 15 日
n = 1; % predefined
f = @(x) x^n
F = func2str(f);
str = regexprep(F,'.*\)','');
SYM = str2sym(str);
"Input in the order of" + char(symvar(SYM))
F_handle = matlabFunction(SYM)
F_handle(2,3) % 2 assigned to n
zongxian
zongxian 2020 年 7 月 15 日
I also tried to convert it to a string, and then to an anonymous function, but it was not successful. Your way is good, but there is a problem that all variables change to input variables.
for example:
m=1;n=2;
f = @(x) x^n+m ===>
f= @(m,n,x)m+x.^n
If I just want to add a parameter? other parameter sill keep its values
m=1;n=2;
f = @(x) x^n+m ===>
m=1;
f= @(n,x)m+x.^n
Anyway, I think your method has helped me a lot.
madhan ravi
madhan ravi 2020 年 7 月 15 日
編集済み: madhan ravi 2020 年 7 月 15 日
n = 1;
m = 2;
save m
f = @(x) x^n+m
F = func2str(f);
str = regexprep(F,'.*\)','');
SYM = str2sym(str);
syms(symvar(SYM))
clear m
load m
subs(subs(SYM, {x,n},{2,3}))

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 7 月 14 日

0 投票

Once you've created an anonymous function handle that "remembers" a particular value, you cannot change that "remembered" value. You would need to recreate the function handle to change it.
n = 2;
f = @(x) x.^n; % n is locked into the value 2 at this point
y1 = f(3) % 9
n = 4;
y2 = f(3) % still 9 not 3^4 = 81
f = @(x) x.^n; % Recreating f "remembers" the new value of n
y3 = f(3) % 81
If you have a value that you want to be able to change, you should probably have the anonymous function accept it. You could, if you need a function with a specific signature, write an adapter. In the example below g accepts both x and n, while f2 and f3 fix specific values for n but make use of g to compute their results.
g = @(x, n) x.^n;
f2 = @(x) g(x, 2);
f3 = @(x) g(x, 3);

5 件のコメント

zongxian
zongxian 2020 年 7 月 15 日
編集済み: zongxian 2020 年 7 月 15 日
If we have a anonymous function handle like your exmple (n =3; f = @(x) x.^n;), then we can not change the value of n in this handle. We can only redefine anonymous function handle after a predetermined value of n, (i.e. n = 4; f = @(x) x.^n;). Thank you.
Steven Lord
Steven Lord 2020 年 7 月 15 日
In your comment you defined a new function handle F_handle. You didn't modify the existing function handle f.
madhan ravi
madhan ravi 2020 年 7 月 15 日
編集済み: madhan ravi 2020 年 7 月 15 日
Right. I misread OPs comment. What I meant by that was there’s an alternative to overcome that problem.
Mark Wilson
Mark Wilson 2021 年 6 月 21 日
編集済み: Mark Wilson 2021 年 6 月 21 日
I just found this thread while searching for help on some surprising results from odextend(sol,...) and thought I would post my experience here so others can benefit. I was running code similar to this, using ode23 with an anonymous function handle to pass extra parameters, then changing those parameters, and continuing the solution:
aux = [a b];
sol1 = ode23(@(t,y) myfun(t,y,aux),[t0 t1],0);
aux = [c d];
sol2 = odextend(sol1,[],t2);
(Here, [] as the second argument tells odextend to use the same ode function as the original solution.)
However, odextend was internally using aux values of [a b] instead of [c d]. @Steven Lord's answer here taught me that changing the values of the extra parameters in the workspace doesn't affect the function handle stored in sol.extdata.odefun and used by default by odextend(). Once I redefined the anonymous function with the updated aux, it worked.
sol2 = odextend(sol1,@(t,y) myfun(t,y,aux),t2);
This was not clear to me from the odextend documentation.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

2020 年 7 月 14 日

編集済み:

2021 年 6 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by