How to fit a custom function using nlinfit?

3 Ansichten (letzte 30 Tage)
Steven C
Steven C am 11 Dez. 2017
Kommentiert: Image Analyst am 11 Dez. 2017
I have written a function 'fun' in a separate .m file that I would like to fit to some xy data, using a parameter 'A' as a fitting parameter. However, since the function depends on the input of xdata and the parameter A to run, I am no sure how to fit it using nlinfit (or similar). When I enter the following, matlab returns an error:
nlinfit(xdata,ydata,fun,A)
Error in fun(line 13)
Not enough input arguments.
So apparently, the xdata and A do not end up like this:
fun(xdata,A)
Does anybody know how to fit a function that was not defined using a function handle?
Thanks in advance!
  2 Kommentare
Image Analyst
Image Analyst am 11 Dez. 2017
Please attach your data and tell us the model you wish to fit to it. In the meantime, I've attached 3 demos that perhaps can be adapted to your situation.
Steven C
Steven C am 12 Dez. 2017

Thanks for your quick reply and examples. However, these examples all use function handles (fun=@(x) x...) to define the function, whereas my question is how to fit a function that is defined in a separate .m file. Let's say this is the function I want to fit, which is defined in a separate .m file located in the same folder as the script I will use during the fitting. xdata and ydata are an example dataset to be fitted.

function [Tg]=fun_Tg(yw)
dCpw=1830;%
dCps=420.0067;%
Tg_w=134;%
Tg_s=420;%
Tg=(yw.*dCpw.*Tg_w+(1-yw).*dCps.*Tg_s)./(yw.*dCpw+(1-yw).*dCps);
disp(Tg)
end
xdata=[0.13491713;0.1762431;0.18508287;0.23878452];%example xdata
ydata=[341.105345;323.197005;317.909106;298.661164];%example ydata
Tg_s0=[420];%initial guess Tg_s

What I would now like to do is use fun_Tg in nlinfit, by fitting xy data with respect to Tg_s. However, I do not know how to present the data to nlinfit...

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by