Getting "Error evaluating model function" message with nlinfit

% Current vs. Voltage %
rng 'default'
xcurr = 10e6*ydata; % Scaling factor
yvolt = xdata;
for i = 1:10 % For the 10 temperatures
f = @(F,xcurr) F(1).*xcurr(:,i) + F(2).*log(1 + xcurr(:,i)./F(3));
%f = @(F,xcurr) F(1).*(xcurr(:,i) + log(1 + xcurr(:,i)./F(2)));
F_fitted = nlinfit(xcurr(:,i),yvolt,f,[1 1 1]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure(i)
plot(xcurr(:,i),yvolt,':',xcurr(:,i),f(F_fitted,xcurr(:,i)),'g');
title(T(i) ,'FontSize',20)
xlabel('Current (\muA)','FontSize',20)
ylabel('Voltage (V)','FontSize',20)
h = legend('data','fit','Location','Best');
%h = legend('293','293','250','250','200','200','175','175','150','150','100','100','80','80','60','60', '40', '40', '20', '20', 'Location','Best');
set(h,'FontSize',12);
end
I'm trying to use nlinfit with 10 sets of x and y data to fit a function to those 10 sets, then plot the data and the fit function. The code runs once perfectly, then gives me this error message upon the second run through:
Error using nlinfit (line 205)
Error evaluating model function '@(F,xcurr)F(1).*xcurr(:,i)+F(2).*log(1+xcurr(:,i)./F(3))'.
Error in diodeFitting (line 207)
F_fitted = nlinfit(yvolt,xcurr(:,i),f,[1 1 1]);
Caused by:
Index exceeds matrix dimensions.
What's strange is that it runs once perfectly but then during the second run gives me the error message. All the data is the same length (each column vector), so I'm not sure what's wrong. xcurr and yvolt are the sets of data. xcurr is 101x10 double (hence running this 10 times), and yvolt is 101x1 double.

1 Comment

In
F_fitted = nlinfit(yvolt,xcurr(:,i),f,[1 1 1]);
pass "xcurr" instead of "xcurr(:,i)"
You are passing 101X1 matrix and trying to access 2nd column of it

Sign in to comment.

Answers (0)

Asked:

on 4 Aug 2017

Community Treasure Hunt

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

Start Hunting!