Definition of the error function in lsqnonlin
이전 댓글 표시
Hello, I'm trying to fit experimental data with a function let's say f(x) which is quite complicated and I cannot use the curve fitting tool. The problem is that to define the error function I have to do some operation with this f(x) function, which are not allowed since it is an anonymous function. To explain better I give an example with a simple f(x) function:
x = 0:1/10000:10
f = @(p) x*p(1) + exp(x*p(2));
Now, to define the error function with respect to the experimental data I need to do some operation on this function f, for example:
% Preliminary operations
f = f + abs(min(f));
f = f./f(1) * 3;
idx_start = dsearchn(f, 5);
f = f(idx_start:end);
And finally the error function:
err = f - experimental_data;
So I can use lsqnonlin:
p0 = [1 2];
p = lsqnonlin(err, p0);
Now the problem is that until the initial values p(1) and p(2) are defined, matlab does not allow you to do all those preliminary operations necessary to define the error function since f is an anonymous function (with symbolic p).
Is there a way then to define the error function only after the initial conditions have been chosen? Or is there a way to do those operations directly on f even if it's an anonymous function?
I hope I was clear, have a nice day and thank you!
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
