How do I define a function to be used in an annonymous function?
1 view (last 30 days)
Show older comments
Hello, I am trying to use the following approach to find the values p(1) and p(2) that gives the best aproximation of curves yreal and yfit:
%Function to calculate the sum of residuals for a given p1 and p2
fun = @(p) sum((yreal - yfit(p(1),p(2),signal1, signal2)).^2);
%starting guess
pguess = [1,0.2];
%optimise
[p,fminres] = fminsearch(fun,pguess)
where y_fit is a function that depends on those parameters and signals 1 and 2. I am building the function yfit because inside it I have to use ode45 to obtain the y_real values, I mean, it is not a function as easy as, for example:
fun = @(p) sum((yreal - p(1)*cos(p(2))*signal1+signal2)).^2);
The problem I am having with my function yfit is:
Undefined function 'minus' for input arguments of type 'struct'.
Error in fun = @(p) sum((yreal - yfit(p(1),p(2),signal1, signal2)).^2);
Any suggestion on how should I define fun or yfit?
Thanks
3 Comments
dpb
on 9 Apr 2014
What's the definition of yfit look like and what are signal1/2 ? I'm puzzled about what generated the reference to a structure.
W/O any more info, I'd start at
fun = @(p,s1,s2) sum((yreal - yfit(p(1),p(2),s1, s2)).^2);
but not positive about it.
One thing I see as a problem in the initial is that for any variable in the anonymous function not in the argument list, the current value of that variable at the time of the creation of the handle is "builtin" to the function and changing its value (or even deleting it) won't change the values used in the function. Hence, in your initial case signal1 and -2 will never change during the search if they're supposed to.
Answers (1)
Star Strider
on 10 Apr 2014
First, since yfit is an external function (we didn’t know that before), change fun to:
fun = @(p) sum((yreal - @yfit(time, e, p(1),p(2))).^2);
and see if that works. (Note the added ‘@’ sign.)
If you still have problems with it, the next step is to call yfit itself (outside of fun) and see what the results are. If yfit has problems in that situation, start troubleshooting by being sure your ODE in FES integrates.
4 Comments
Star Strider
on 10 Apr 2014
First question: If you give your DE the appropriate parameter estimates and initial conditions, how long does it take it to integrate?
Second question: I don’t understand the ind1 ... ind2 loop. It seems to me that you just have to do the fminsearch call once comparing your two signals, be sure you have a good fit to your data and parameter estimates that make sense, and you’ve done what you set out to do.
I admit to not understanding what you are doing, most significantly with respect to your loop. I thought you are doing a relatively straightforward curve-fitting parameter estimation, but apparently I missed something.
I suggest you output and save the p and fminres variables at each iteration. If it’s taking as long as it is, you don’t want to risk losing any information.
See Also
Categories
Find more on Calendar 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!