error in mdl/rsquared.ordniary

6 views (last 30 days)
Noa Prasquier
Noa Prasquier on 8 May 2021
Commented: Noa Prasquier on 16 May 2021
Hi,
I am trying to use fitlm and rsquared ordinary functions and I can't figure out where the errors comes from.
Moreover, I want to plots 3 graphs but only the first one appears.
I would really appreciate your help.
here is my code :
function[]=reaction_order(A)
fit0=A;
fit1=log(A);
fit2=1./A;
time=[1:11]';
f0=fit(time,fit0','poly1');
figure
plot(f0,time,fit0);
xlabel('Time [s]');
ylabel('A [M]')
title('Zero order fit, A concentration as a function of time')
legend('A concentration', 'Fit curve')
mdl0=fitlm(time,f0);
rsquared0=mdl0.Rsquared.ordinary;
fprintf(rsquared0)
f2=fit(time,fit2','poly1');
figure;
plot(f2,time,fit2);
xlabel('Time [s]');
ylabel('1/A [1/M]')
title('Second order fit, 1/A as a function of time')
legend('1/A', 'Fit curve')
mdl2=fitlm(time,f2);
rsquared2=mdl2.Rsquared.ordinary;
The error is :
Error using classreg.regr.FitObject/assignData (line 134)
Predictor and response variables must have the same length.
Error in classreg.regr.TermsRegression/assignData (line 245)
model =
assignData@classreg.regr.ParametricRegression(model,X,y,w,asCat,varNames,excl);
Error in LinearModel.fit (line 1030)
model =
assignData(model,X,y,weights,asCatVar,dummyCoding,model.Formula.VariableNames,exclude);
Error in fitlm (line 121)
model = LinearModel.fit(X,varargin{:});
Error in chimieq2>reaction_order (line 50)
mdl0=fitlm(time,f0);
Error in chimieq2 (line 4)
reaction_order(A)

Answers (1)

Sindhu Karri
Sindhu Karri on 12 May 2021
Hii,
The error is in the line
mdl0=fitlm(time,f0);
It is because of passing incorrect inputs to the fitlm function,here the error message
"Predictor and response variables must have the same length"
refer to the variables time and f0.Refer to below attach for more details on fitlm function
And the reason for getting only one graph is because of this error and other two plots have not been calculated.
  1 Comment
Noa Prasquier
Noa Prasquier on 16 May 2021
ok, do you know what input I am supposed to pass to the fitlm?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by