Using "fit" function to create a fit type and fit a curve with a function I created
Show older comments
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!
Accepted Answer
More Answers (1)
Steven Lord
on 19 Feb 2021
0 votes
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.
Categories
Find more on Frequently-used Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!