MATLAB: Matrix dimensions don’t agree

matrix dimensionsmatrix multiplication

Hello, so I am trying to find the best parameters and RSS value for the following program:
%Program HollingTypeIII.m
%Parameterization mode A1b
%Calls RSSHolling.m and fminsearch.m
global age ulna
%Load data
load ulnaestimation.txt
data = ulnaestimation
age = data(:,1);
ulna = data(:,2);
%Initialize a and b
theta = log([1 1.5]);
%Run Nelder-Mead algorithm and output best parameter to screen
[output RSSbest] = fminsearch('RSSHolling',theta);
params = exp(output);
abest = params(1)
bbest = params(2)
RSSbest
The subroutine is:
%Subroutine RSSHolling.m
%Called by HollingTypeIII.m
function RSS = RSSHolling(theta)
global age ulna K
par = exp(theta);
a = par(1);
b = par(2);
K = 13.94615385;
pred = (((K-b)*(age.^2))/((a^2)+(age.^2)))+b
%Compute RSSHolling on sqrt scale
residuals = sqrt(ulna)-sqrt(pred);
RSS = sum(residuals.^2);
end
However, when I run the program, it comes up with an error:
Error using – Matrix dimensions must agree.
Error in RSSHolling (line 12) residuals = sqrt(ulna)-sqrt(pred);
Somehow, when the pred equation is run, it results in a huge matrix, when I need a 40×1 matrix. I have tried reshaping the matrix; separately, I have tried writing "pred'" in the residuals equation to flip the matrix. This does not help. Does anyone have any answers as to what might be wrong in my program?

Best Answer

Try changing the division to element-wise:
pred = (((K-b)*(age.^2))./((a^2)+(age.^2)))+b; % <-- Changed / to ./