fminsearch - passing back variables calculated in anonymous functions
Show older comments
Hi there, I often use anonymous functions in fminsearch. If I'm right, these can only pass back scalar values to fminsearch. But I often would like to have access to variables calculated in the anonymous function from where I called fminsearch. How do I do this? Do I have to declare these variables as global?
Here is a stylised example, where V1 and V2 are calculated in the anonymous function (called minError), and I'd like to use it in the function that included the call to fminsearch (called mainFunction)...
function V=mainFunction
(...)
[xOpt, m, ~]=fminsearch(@(x) minError(x), GuessForX);
V=[V1 V2];
end
function m=minError(x)
V1=x^2;
V2=x;
m=(V1-V2)^2;
end
Thank yoU!
Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!