Problem using nlinfit function

Hi everyone,
I am having trouble using nlinfit for making a regression with an own function.
I have the following script:
xdata = 1:150;
xdata = xdata';
ydata = beta;
beta0 = [0 -1 20];
n = 1:150;
y = nlinfit(xdata,ydata,fun,beta0);.
and the function fun.m is this:
function y = fun(beta0,n);
y = beta0(1)+ beta(2).*exp(-n/beta(3));
The basic original function that I want to use is a +be^(t/tau) to fit a set of data points:
I have tried a lot, but I get different errors, variables not defined, input must be 4 variables etc. Please help me out!

 采纳的回答

1. ydata has to be an array of the same size as xdata (I don't know the size of the array beta).
2. The call to nlinfit must read
y = nlinfit(xdata,ydata,@fun,beta0);
3. fun.m must read
function y = fun(beta0,xdata);
y = beta0(1)+ beta0(2)*exp(-xdata/beta0(3));
Best wishes
Torsten.

1 个评论

I had to use xdata instead of trial number array n, thank you! And I didnt't use @ for the function. Thanks a lot! It worked!

请先登录,再进行评论。

更多回答(1 个)

类别

标签

Community Treasure Hunt

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

Start Hunting!

Translated by