Fitting nonlinear model to data
Show older comments
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
Torsten
on 5 Sep 2018
nlinfit has at most 5 inputs, not 6.
Sonisa
on 5 Sep 2018
Torsten
on 5 Sep 2018
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.
Sonisa
on 5 Sep 2018
Torsten
on 5 Sep 2018
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.
Sonisa
on 5 Sep 2018
Matt J
on 5 Sep 2018
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
Sonisa
on 5 Sep 2018
Matt J
on 5 Sep 2018
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.
Sonisa
on 5 Sep 2018
Answers (0)
Categories
Find more on Resampling Techniques in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!