Using "fit" function to create a fit type and fit a curve with a function I created

6 visualizzazioni (ultimi 30 giorni)
Hello everyone,
I defined a function
function [CPSC] = generate_current(a,b,c,d,e,f,g,h,G_max_chl, G_max_glu, EGlu, EChl, ...
Vm, tau_rise_In, tau_decay_In, tau_rise_Ex, tau_decay_Ex,t)
% Compute compound current
CPSC = ((G_max_chl * a) .* ((1 - exp(-t / tau_rise_In * b)) .* ...
exp(-t / tau_decay_In * c)) * (Vm - EChl * d)) + ((G_max_glu * e) .* ...
((1 - exp(-t / tau_rise_Ex * f)) .* exp(-t / tau_decay_Ex * g)) * (Vm - EGlu * h));
end
That I want to use to fit some data I have. I am trying to use "fit" and doing this:
% I generate a first current CPSC with the aforementioned function
CPSC = generate_current(1,1,1,1,1,1,1,1,20,30,0,-70,-50,0.15,0.9,0.23,0.2);
% I generate a second current y with the same equation
% However, its values are: 1,1,1,1,1,1,1,1,10,40,0,-70,-50,0.15,0.9,0.23,0.2,1:0.1:16-0.1)
% y has exactly the same values as CPSC except for the first 10 and 40. I want to fit y to CPSC. This fitting
% procedure should therefore automatically multiply 10 by a (which would be 2), and 40 by e (which would be 0.75). All the other parameters should stay fixed.
y = generate_current2;
% Initialize time
tmax = 15; % Duration of experiment
dt = 0.1; % Time step duration (ms)
x = 0:dt:tmax-dt; % Time, on the x axis
ft = fittype( ['generate_current(a,b,c,d,e,f,g,h,G_max_chl, G_max_glu, EGlu, EChl, ' ...
'Vm, tau_rise_In, tau_decay_In, tau_rise_Ex, tau_decay_Ex)'] );
f = fit( [G_max_chl, G_max_glu, EGlu, EChl, Vm, tau_rise_In, tau_decay_In, tau_rise_Ex, tau_decay_Ex], ...
y, ft, 'StartPoint', [1,1,1,1,1,1,1,1,20,30,0,-70,-50,0.15,0.9,0.23,0.2] );
plot( f, x, y )
This is the error I receive:
Error using fittype>iDeduceCoefficients (line 621)
The independent variable x does not appear in the equation expression.
Use x in the expression or indicate another variable as the independent variable.
Error in fittype>iCreateCustomFittype (line 477)
obj = iDeduceCoefficients(obj);
Error in fittype>iCreateFittype (line 353)
obj = iCreateCustomFittype( obj, varargin{:} );
Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
Error in optimization_at_intermediate_hp2 (line 13)
ft = fittype( ['generate_current(a,b,c,d,e,f,g,h,G_max_chl, G_max_glu, EGlu, EChl, ' ...
Thanks!

Risposta accettata

Cris LaPierre
Cris LaPierre il 19 Feb 2021
Modificato: Cris LaPierre il 22 Feb 2021
There is a subtle detail made in this example.
  • Define a function in a MATLAB file.
  • Save the file.
To use the approach you are using, you must save the generate_current function to a file. It will not work if the function is defined as a local/in-file function.
Next, tell it that the independent variable is t. using the 'independent','t' name-value pair.
Even though I could get that working, it didn't give a good result. However, when I used the curve fitting app, it did. Below is the generated script.
% Create inputs
tmax = 15; % Duration of experiment
dt = 0.1;
t = 0:dt:tmax-dt;
y = generate_current(1,1,1,1,1,1,1,1,10,40,0,-70,-50,0.15,0.9,0.23,0.2,t);
%% Perform fit
[xData, yData] = prepareCurveData( t, y );
% Set up fittype and options.
ft = fittype( '((G_max_chl * a) .* ((1 - exp(-t / tau_rise_In * b)) .* exp(-t / tau_decay_In * c)) * (Vm - EChl * d)) + ((G_max_glu * e) .* ((1 - exp(-t / tau_rise_Ex * f)) .* exp(-t / tau_decay_Ex * g)) * (Vm - EGlu * h))', 'independent', 't', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf -Inf -Inf 1 1 1 1 1 1 1 1 -Inf -Inf -Inf -Inf];
opts.StartPoint = [-70 0 20 30 -50 1 1 1 1 1 1 1 1 0.2 0.9 0.23 0.15];
opts.Upper = [Inf Inf Inf Inf Inf 1 1 1 1 1 1 1 1 Inf Inf Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts )
fitresult =
General model: fitresult(t) = ((G_max_chl * a) .* ((1 - exp(-t / tau_rise_In * b)) .* exp(- t / tau_decay_In * c)) * (Vm - EChl * d)) + ((G_max_glu * e) .* ((1 - exp(-t / tau_rise_Ex * f)) .* exp(-t / tau_decay_Ex * g)) * (Vm - EGlu * h)) Coefficients (with 95% confidence bounds): EChl = -100.9 (-1.145e+08, 1.145e+08) EGlu = 0.2453 (-2.95e+06, 2.95e+06) G_max_chl = 10.15 (-1.164e+07, 1.164e+07) G_max_glu = 17.29 (-2.059e+07, 2.059e+07) Vm = -74.81 (-9.214e+07, 9.214e+07) a = 1 (fixed at bound) b = 1 (fixed at bound) c = 1 (fixed at bound) d = 1 (fixed at bound) e = 1 (fixed at bound) f = 1 (fixed at bound) g = 1 (fixed at bound) h = 1 (fixed at bound) tau_decay_Ex = 0.2518 (0.0617, 0.4418) tau_decay_In = 0.8188 (0.7481, 0.8895) tau_rise_Ex = 0.141 (-0.05693, 0.3388) tau_rise_In = 0.182 (-2.218, 2.582)
gof = struct with fields:
sse: 117.0176 rsquare: 0.9996 dfe: 141 adjrsquare: 0.9996 rmse: 0.9110
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. t', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 't', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on

Più risposte (1)

Steven Lord
Steven Lord il 19 Feb 2021
From the description of the 'independent' name-value pair input argument on the documentation page for the fittype function: "Independent (response) variable names, specified as the comma-separated pair consisting of 'independent' and a character vector or cell array of character vectors. If you do not specify the independent variable, the function assumes x is the independent variable." Your fittype does not include the variable x at all, so fittype cannot deteremine the independent variable for your expression.
See the "Create Fit Types Using Anonymous Functions" example on that documentation page for an illustration of how to specify the independent variable.

Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by