Substitute variables in function handle
7 views (last 30 days)
Show older comments
Hello,
I have two implementations.
- Create functions based on the parameter
- Pass this functions to another function and evaluate them there
I'm creating functions like this:
for i=1:n/2-1
funk{i} = @(x)2*x(i)-2+400*x(i)^3-400*x(i)*x(i+1);
end
I want this to create multiple functions (depends on n) with i to be superset by a number, but WITHOUT being solved right away as i want to hand it over to another function in which this should get solved.
How can I achieve this?
Thanks in advance
1 Comment
Answers (1)
Walter Roberson
on 24 Jan 2022
What you have already done should work.
Although all of the function handles will look the same, they would have captured different values of i
For example,
for i = 1 : 2
funk{i} = @(x)2*x(i)-2+400*x(i)^3-400*x(i)*x(i+1);
end
ws1 = functions(funk{1}).workspace{1}
ws2 = functions(funk{2}).workspace{1}
Notice how the information embedded within the function handles funk{1} and funk{2} is different -- they have different embedded i values.
See Also
Categories
Find more on Matrix Indexing 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!