How do I fix this error: "Error using minus Matrix dimensions must agree"?

조회 수: 1 (최근 30일)
Sean Quinn
Sean Quinn 2016년 1월 28일
댓글: Walter Roberson 2016년 1월 29일
Here is my script:
function f = objectiveL2(p, pIndex, data, trainingIndex, kinesinName, lambda)
if length(p) ~= size(pIndex,1)
error('p and pIndex must have the same length');
end
p00 = InitialParameterValues(kinesinName);
p0 = InitialParameterValues(kinesinName);
for i = 1:size(pIndex,1)
for j = 1:size(pIndex,2)
if pIndex(i,j) ~= 0
p00(pIndex(i,j)) = p(i)*p00(pIndex(i,j));
end
end
end
for i = 1:size(data,1)
if strcmp(data(i,1).kinesinName,kinesinName)
kinesinIndex = i;
break;
end
end
f = 0;
for i = 1:length(trainingIndex)
% Define initial conditions for the model
x0 = zeros(12,1);
x0(1) = data(kinesinIndex,trainingIndex(i)).kinesinConc; % Kinesin
x0(2) = data(kinesinIndex,trainingIndex(i)).MTConc; % MT
x0(8) = data(kinesinIndex,trainingIndex(i)).mantATPConc; % mantATP
tspan = [0;data(kinesinIndex,trainingIndex(i)).time];
[~,x] = ode45(@(t,x,p) SteppingModel_no78(1,x,p00), tspan, x0);
y_model = p00(15)*(x(2:end,9)+x(2:end,10));
y_data = data(kinesinIndex,trainingIndex(i)).fluorescence - 1;
f = f + sum((y_model - y_data).^2)/length(y_data);
end
f = f + lambda*sum(((p0(1:14) - p00(1:14))./p0(1:14)).^2);
end
Here is the error I get
Error using -
Matrix dimensions must agree.
Error in objectiveL2 (line 38)
f = f + sum((y_model - y_data).^2)/length(y_data);
Error in @(p)objectiveL2(p,pIndex,data,trainingIndex,kinesinName,0.0000000000001)
Error in fmincon (line 534)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in main (line 60)
[pFit, fval, flag] = fmincon(@(p) objectiveL2(p, pIndex, data, trainingIndex, kinesinName, 0.0000000000001), ...
Caused by:
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
I know where the error is occurring (line 38), but I really don't know how to fix it. It looks correct to me. Any help would be greatly appreciated.
Thanks!
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2016년 1월 28일
Sean - what are the dimensions of y_model and y_data? Since you are subtracting one from the other, they must have the same number of rows and the same number of columns.
Walter Roberson
Walter Roberson 2016년 1월 29일
Where is kinesinIndex being defined? Is it a function, or is it being defined in an outer function that this one is nested inside?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by