MATLAB: Nonlinear fit instead of setting up a non linear equation

currentsfitfittingMATLABneurosciencenonlinear equationnonlinear fitoptimizationOptimization Toolbox

Hello everyone,
I'll attach my script here, in case you want to run the simulation yourself.
My task is to find two values x and y so that if I multiply them by G_max_chl2 and G_max_glu2 (two scalar values that I pre-defined in previous parts of the code) respectively, this:
CPSC_2 = ((G_max_chl2 * x) .* (1 - exp(-t / tau_rise_In2)) .* (exp(-t / tau_decay_In2)) * (Vm - EChl2)) + ((G_max_glu2 * y) .* (1 - exp(-t / tau_rise_Ex2)) .* exp(-t / tau_decay_Ex2) * (Vm - EGlu2))
becomes the same as this:
CPSC_1 = ((G_max_chl) .* (1 - exp(-t / tau_rise_In)) .* (exp(-t / tau_decay_In)) * (Vm - EChl)) + (G_max_glu .* (1 - exp(-t / tau_rise_Ex)) .* exp(-t / tau_decay_Ex) * (Vm - EGlu))
1 All the variables above are the same for the two equations, except for the four "G_max" values. The only two variables that change are G_max_chl2 and G_max_glu2, so that they become the same as G_max_chl and G_max_glu.
2 Some of these variables are matrices, but in this case they are not involved and should remain the same (only the two scalars G_max_chl2 and G_max_glu2 should change)
I solved the task setting up a nonlinear equation:
% This is the difference that has to be minimized
Diff_CPSC = abs((((G_max_chl2) .* ((1 - exp(-t / tau_rise_In2)) .* ...
(exp(-t / tau_decay_In2))) * (Vm - EChl2)) + ((G_max_glu2) .* ...
((1 - exp(-t / tau_rise_Ex2)) .* exp(-t / tau_decay_Ex2)) * (Vm - EGlu2))) - (((G_max_chl) .* ...
((1 - exp(-t / tau_rise_In)) .* exp(-t / tau_decay_In)) * ...
(Vm - EChl)) + ((G_max_glu) .* ((1 - exp(-t / tau_rise_Ex)) .* exp(-t / tau_decay_Ex)) * (Vm - EGlu))));
% EQUATION
% Create a nonlinear equation
x = optimvar('x',1);
y = optimvar('y',1);
% Define function to minimize
eq1 = (((G_max_chl2 * x) .* ((1 - exp(-t / tau_rise_In2)) .* ...
(exp(-t / tau_decay_In2))) * (Vm - EChl2)) + ((G_max_glu2 * y) .* ...
((1 - exp(-t / tau_rise_Ex2)) .* exp(-t / tau_decay_Ex2))* ...
(Vm - EGlu2))) == ((G_max_chl) .* ((1 - exp(-t / tau_rise_In)) .* ...
exp(-t / tau_decay_In)) * (Vm - EChl)) + ((G_max_glu) .* ...
((1 - exp(-t / tau_rise_Ex)) .* exp(-t / tau_decay_Ex)) * (Vm - EGlu));
% Create an equation problem, and place the equation in the problem
prob = eqnproblem;
prob.Equations.eq1 = eq1;
% Show the problem
show(prob);
% Specify the initial point as a structure
x0.x = G_max_chl / G_max_chl2;
x0.y = G_max_glu / G_max_glu2;
[sol,fval,exitflag] = solve(prob,x0);
% View the solution point and convert to double
disp(sol.x);
disp(sol.y);
x = sol.x;
y = sol.y;
However, I find this procedure a bit laborious because, if I wanted to optimize other variables, I'd have to hard code the unknown variables everytime. My question is:
  • This procedure forces me to know all of the values of CPSC_2, which in this case is true because I generated the simulation. However, if it were coming from experimental recordings, it wouldn't be the case anymore. Is there a way to fit a CPSC_2 (whose variables are unknown) to CPSC_1 (whose variables are known)?

Best Answer

Your problem appears to be a linear fit. I see no non-linear dependence on x or y. You should therefore use lsqlin, if you also have bounds or linear constraints.
Otherwise, you could just use mldivide() or linsolve().