lsqcurvefit error - Matrix dimensions must agree

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?

Réponses (1)

Star Strider
Star Strider le 14 Mar 2019

1 vote

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 commentaires

Silke
Silke le 14 Mar 2019
Modifié(e) : Silke le 14 Mar 2019
Hi!
Thanks for your quick reply.
Sorry, I made a mistake copying the code. I have corrected the code in my initial question. CCx is defined now.
My pleasure.
I still can’t run your code.
What are the results of:
sz_timecorr = size(timecorr)
sz_data_x = size(data_x)
sz_S = size(S)
I would just use this for ‘tspan’:
tspan = t;
The way you are currently defining it could be the problem, because it may not be the same size as the original ‘t’ vector. Besides, you want ‘t’ to define the integration.
Also, you are passing the initial conditions to ode45 as a separate argument, ‘af’. Those will not be modified by lsqcurvefit. If you want to estimate them rather than fix them, append them as the last two elements of ‘param’, and make appropriate changes to your code to segregate the first four arguments and use the last two as your initial conditions.
If you are still having problems after that, completely vectorise ‘f’ and see if that changes the outcome.
Silke
Silke le 14 Mar 2019
Modifié(e) : Silke le 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'?
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.

Connectez-vous pour commenter.

Produits

Version

R2015b

Question posée :

le 14 Mar 2019

Commenté :

le 14 Mar 2019

Community Treasure Hunt

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

Start Hunting!

Translated by