lsqcurvefit error - Matrix dimensions must agree

3 views (last 30 days)
Silke
Silke on 14 Mar 2019
Commented: Star Strider on 14 Mar 2019
Hello!
I am trying to fit complex data using lsqcurvefit. This is the function that I want to fit my data to:
function S = DiffEqSolver1stOrderExcitonAnni(param, t, af);
k_1 = param(1);
k_2 = param(2);
k_a = param(3);
sum_mu_re = param(4);
sum_mu_im = param(5);
mu_x_re = param(6);
mu_x_im = param(7);
thickness= param(8);
function dx = myode( x, k_1, k_2, k_a) %k_3,phi_n,k_1p,
dx = zeros(2,1);
dx(1) = -k_1 * x(1)^2 + k_2*x(2);
dx(2) = k_1 * x(1)^2 - k_2*x(2) - k_a*x(2)^2;
end
tspan = [t(1): (t(end)/(numel(t)-1)) : t(end)];
opts = odeset('RelTol',1e-6,'AbsTol',1e-8);
ic = [af;af];
[t,x(:,:)] = ode45(@(t,x) myode( x, k_1, k_2, k_a), tspan, ic, opts);%k_3,phi_n,, k_1p
CCx = x;
f = (CCx(:,1).*(sum_mu_re-i*sum_mu_im) + CCx(:,2) .* (mu_x_re-i*mu_x_im)).*thickness ./ af;
S(:,1) = real(f);
S(:,2) = imag(f);
end
I am using lsqcurvefit like that, with
timecorr 220*1 double
data_x 220*2 double
param = [1e-3; 100, 1e-5; 1000; 1000; 10000; 10000; 50e-9];
[xfit,resnorm] = lsqcurvefit( @(param,timecorr) DiffEqSolver1stOrderExcitonAnni(param, timecorr, 1e14), param, timecorr, data_x);
I get this error message:
Error using -
Matrix dimensions must agree.
Error in lsqcurvefit/objective (line 262)
F = F - YDATA;
Error in snls (line 329)
newfvec = feval(funfcn{3},xcurr,varargin{:});
Error in lsqncommon (line 156)
snls(funfcn,xC,lb,ub,flags.verbosity,options,defaultopt,initVals.F,initVals.J,caller,
...
Error in lsqcurvefit (line 254)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,caller,...
What exactly is going wrong here? Can someone please elaborate on the error?

Answers (1)

Star Strider
Star Strider on 14 Mar 2019
I can’t run your code.
However, some things are immediately obvious: (1) you are not integrating ‘myode’ inside ‘DiffEqSolver1stOrderExcitonAnni’, and (2) your ‘f’ variable is a function of ‘Ccx’ that does not ever appear to have been defined in ‘DiffEqSolver1stOrderExcitonAnni’.
My impression is that ‘Ccx’ is the output of the integration of ‘myode’, however that was never coded.
  4 Comments
Silke
Silke on 14 Mar 2019
Edited: Silke on 14 Mar 2019
Thanks for your quick reply.
The results of
sz_timecorr = size(timecorr)
sz_data_x = size(data_x)
sz_S = size(S)
are sz_timecorr =
220 1
sz_data_x =
220 2
sz_S =
1 2
I guess because it does not iterate until it reaches the end of t. But why is that?
af should be a fixed value. And I changed tspan.
What do you mean by completely vectorizing 'f'?
Star Strider
Star Strider on 14 Mar 2019
If you use ‘t’ for ‘tspan’, ‘S’ should be the same size as ‘data_x’.
I believe that’s the problem.
By ‘completely vectorising’, change all the multiplications to (.*). That may not be the problem, however unless you intend to use matrix opertations, it is the safest option.

Sign in to comment.

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!