- Define a function in a MATLAB file.
- Save the file.

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

6 views (last 30 days)

Show older comments

Samuele Bolotta
on 19 Feb 2021

Edited: Cris LaPierre
on 22 Feb 2021

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!

##### 0 Comments

### Accepted Answer

Cris LaPierre
on 19 Feb 2021

Edited: Cris LaPierre
on 22 Feb 2021

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.

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 )

% 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

##### 0 Comments

### More Answers (1)

Steven Lord
on 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.

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!