Fitting nonlinear model to data

I have been trying to fit nonlinear model to my data
Here are the codes:
X = [0.05 0.06 0.07 0.08 0.09 0.1 0.125 0.15 0.175 0.2 0.25 0.3 0.4 0.5 0.6 0.7 0.8 -0.1 -0.5 -0.8];%FAW40;
Y = [0.3 0.4 0.35 0.42 0.3 0.32 0.35 0.37 0.38 0.495 0.45 0.55 0.6 0.55 0.6 0.6 0.65 0.2 0.3 0.5]; %NDVI;
figure
% plot(FAW40_GS,NDVI_GS,'ok');
k = diff(x)<0; %select only data when the soil is drying
x = X(1:end,:);
y = Y(2:end,:);
plot(x(k(:,1),1),y(k(:,1),1),'ok')
hold all
plot(x(k(:,2),2),y(k(:,2),2),'+k')
plot(x(k(:,3),3),y(k(:,3),3),'dk')
set(gca,'XLim',[0 1])
set(gca,'XTick',(0:0.1:1))
%set(gca,'YLim',[0 1])
set(gca,'YTick',(0:0.1:1));
hold all
ylabel('\bf NDVI','fontsize',14);
xlabel('\bf FAW','fontsize',14);
slope = 0.2;
r = 0.40;
ypred = @(A, x)(A(1) ./ (A(2) + exp([(r-Y)*slope])));
A0 = [0.2;-0.5];
A1 = [ 0.3; -0.2];
opts = statset('nlinfit');
opts.RobustWgtFun = 'bisquare';
A_fit = nlinfit(x,y,ypred,A0,A1,opts);
hold all
plot(x',y','ko', x,ypred(A_fit,x'))
legend('Data','Fitted','location', 'northwest')
_But when I did I get the error
Error using nlinfit (line 185)
Requires a vector second input argument. Any help would be appreaciated.
_

10 Comments

nlinfit has at most 5 inputs, not 6.
I do not know what is the meaning of second input argument? Even I deleted the 6th input, I get the same error.
x and y must have the same length (y is the measured value at time x). Your y has one element less than x.
And the other error remains. Either you specify A0 or A1 as initial guess for the parameters you want to determine.
I still get the same error. What does this error mean?
Error using nlinfit (line 185) Requires a vector second input argument.
It means that the second argument in the call to nlinfit must be a vector. In your case, x is a scalar and y is empty.
Try
x = X;
y = Y;
instead of
x = X(1:end,:);
y = Y(2:end,:);
Best wishes
Torsten.
This also doesn't work.
Please give us the version of the code that you are actually running. The code you have posted gives a different error,
Undefined function or variable 'x'.
Error in test (line 8)
k = diff(x)<0; %select only data when the soil is drying
x = 0.637259937 0.621294757 0.672668532 0.521265646 0.421903196 0.549649009 0.312588087 0.28327584 0.359424364 0.253502479 0.253985122 0.313290724 0.209563459 0.2319699 0.274603618 0.156334057 0.183523167 0.236265826 0.475258711 0.560432822 0.629973282 0.238506777 0.253712482 0.302720237
y = 0.611673879 0.672925747 0.681451013 0.577414406 0.702284322 0.680381636 0.470820795 0.554830443 0.577386675 0.405224584 0.534853857 0.530397181 0.391375854 0.42629674 0.366951257 0.344208072 0.360370785 0.342645703 0.365246711 0.471838895 0.41643422 0.401104282 0.397572023 0.411658118
Nope. Those lines will not execute
>> x = 0.637259937 0.621294757 0.672668532 0.521265646 0.421903196 0.549649009 0.312588087 0.28327584 0.359424364 0.253502479 0.253985122 0.313290724 0.209563459 0.2319699 0.274603618 0.156334057 0.183523167 0.236265826 0.475258711 0.560432822 0.629973282 0.238506777 0.253712482 0.302720237
x = 0.637259937 0.621294757 0.672668532 0.521265646 0.421903196 0.549649009 0.312588087 0.28327584 0.359424364 0.253502479 0.253985122 0.313290724 0.209563459 0.2319699 0.274603618 0.156334057 0.183523167 0.236265826 0.475258711 0.560432822 0.629973282 0.238506777 0.253712482 0.302720237
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced
delimiters, or other syntax error. To construct matrices, use brackets instead of
parentheses.
Look at this file and hope it will works.

Sign in to comment.

Answers (0)

Tags

Asked:

on 5 Sep 2018

Commented:

on 5 Sep 2018

Community Treasure Hunt

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

Start Hunting!