Hi there,
is there a way that I provide nlinfit with some constants that are taken into account when solving a function?
So, for example, I use this code for nlinfit:
[xfit,resnorm, Jacob, CovB, MSE] = nlinfit( handles.timecorr,handles.datacorr',@DiffEqSolver300, B );
I would like to give another set of parameters to the function DiffEqSolver300 that should not be fitted, but they depend on calculations that happens before I call the nlinfit function. Is this somehow possible?
Just a simple example. Let's assume the function that should be fitted is
y = A*x + B;
Depending on some input parameters, A could be 1 or 10 or 100 and B is a fitting parameter. How can I tell the function which value A should have?

 채택된 답변

Star Strider
Star Strider 2017년 12월 21일
편집: Star Strider 2017년 12월 21일

0 개 추천

Writing your own objective function, you simply need to pass ‘A’ as a parameter.
Example
objfcn = @(B,x,A) A*x + B;
then call it in nlinfit as:
beta = nlinfit(x, y, @(B,x) objfcn(B,x,A), ... );
so that ‘objfcn’ accepts ‘A’ as a parameter, and the function works with nlinfit as it would if no additional parameters were passed.

댓글 수: 6

Silke
Silke 2017년 12월 22일
Hi Star Strider,
thank you for your reply. It seems very straight forward what you suggest, but I am not sure how to implement it in my case, as my function is more complicated then just A*x + B.
So I have a function
function dCCconv = DiffEqSolver300(param, t)
in an individual file, that I call from a different file with
[xfit,resnorm, Jacob, CovB, MSE] = nlinfit( handles.timecorr,handles.datacorr',@DiffEqSolver300, handles.x0 );
Where and how do I have to define the objective function then?
My pleasure.
I have no idea what your ‘DiffEqSolver300’ is or does. The essential idea will work. you can add additional parameters to it and call it from nlinfit as:
@(param,t) DiffEqSolver300(param, t, additionalParameter1, additionalParameter2)
Then pass the additional parameters to your function.
Silke
Silke 2017년 12월 22일
I am still confused about how this works. Well, I think it is not really necessary to understand what DiffEqSolver300 does, as it is quite a general problem. I have now defined the function like this
function dCCconv = DiffEqSolver300(param, t, FA, WL);
and I am using nlinfit like this:
[xfit,resnorm, Jacob, CovB, MSE] = nlinfit( handles.timecorr,handles.datacorr',@(param,t) DiffEqSolver300(param, t, FA, WL), handles.x0 );
I am not sure if this is the correct way to do it, as for example "param" is not defined in the matlab script that uses nlinfit. But handles.x0 is giving the "param" to DiffEqSolver300.
However, I tried it like that, and I got the following error message: Error using nlinfit (line 205) Error evaluating model function '@(param,t)DiffEqSolver300(param,t,FA,WL)'.
Error in TRMC_GuiFit_SD_DiffEqfit_1>FITANDPLOT_Callback (line 416) [xfit,resnorm, Jacob, CovB, MSE] = nlinfit( handles.timecorr,handles.datacorr',@(param,t) DiffEqSolver300(param, t, FA, WL), handles.x0 );%, handles.lb, handles.ub
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in TRMC_GuiFit_SD_DiffEqfit_1 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)TRMC_GuiFit_SD_DiffEqfit_1('FITANDPLOT_Callback',hObject,eventdata,guidata(hObject))
Caused by: Not enough input arguments.
Error while evaluating UIControl Callback
What am I doing wrong here?
As far as nlinfit goes, your objective function call:
@(param,t) DiffEqSolver300(param, t, FA, WL)
should work. The ‘Not enough input arguments’ error is probably because ‘FA’ and ‘WL’ are not defined by those exact names in your workspace.
Also, ‘param’ and ‘t’ must be defined in your script by the same names you return them in your function. For example:
function dCCconv = DiffEqSolver300(param, t, G, H)
...
dCCconv = (param * t + G) / H; % Example — Not Intended To Be Your Actual Function
...
end
This will work if you call it as:
@(param,t) DiffEqSolver300(param, t, FA, WL)
providing that ‘FA’ and ‘WL’ exist in your workspace by those names. The function has its own workspace, and the parameter and variable names are only important in the function.
That is the only possibility I can think of.
Silke
Silke 2017년 12월 22일
Thanks for your help. Indeed, it is working now. I had an error in DiffEqSolver300.
Star Strider
Star Strider 2017년 12월 22일
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2017년 12월 21일

댓글:

2017년 12월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by