Error evaluating inline function - Not enough inputs to inline function

2 views (last 30 days)
Hello everyone,
I am trying to fit a concentration vs time data on Excell that I am trying to fit using an Ordinary Differential Equation. I am not a MatLab exper hence I have found the code "ready" to use. However I always get an error saying -Not enough imput to the inline function- . I don't know how to solve this, can someone help?
This is the code I am using
%% Initialize Data
%set the name of the file containing C vs. T data, where Col(A) contains
%time and Col(B) contains concentration. Must include file extension
filename = 'MyData.xlsx';
direct = 'D:\ Kin';
path = strcat(direct,'\',filename);
%set the sheet number for an excel file. .csv files are read with only 1
%sheet, and this variable will not be used. Excel sheet numbering begins at 1
sheet = 1;
%count the number of lines in the data file using an activeX server
excelObj = actxserver('Excel.Application');
fileObj = excelObj.Workbooks.Open(path);
%Read data
if (~isempty(strfind(filename,'.xls'))) %excel
sheetObj = excelObj.Worksheets.Item(sheet);
rowEnd = sheetObj.Range('A1').End('xlDown').Row;
Timerange = strcat('A1:A',num2str(rowEnd));
Concrange = strcat('B1:B',num2str(rowEnd));
time = xlsread(path,sheet,Timerange);
conc = xlsread(path,sheet,Concrange);
invoke(excelObj,'Quit')
delete(excelObj);
elseif (~isempty(strfind(filename,'.csv'))) %csv file
sheetObj = excelObj.Worksheets.Item(1);
rowEnd = sheetObj.Range('A1').End('xlDown').Row;
time = csvread(path,0,0,[0 0 rowEnd-1 0]);
conc = csvread(path,0,1,[0 1 rowEnd-1 1]);
invoke(excelObj,'Quit')
delete(excelObj);
else
invoke(excelObj,'Quit')
delete(excelObj);
error('no .csv or excel file found');
end
%Assign initial concentration as the first entry
global SM0;
SM0 = conc(1);
%% ODE fitting Initial parameter values for ODE fitting arranged as [a,b,c,d,e,f]
initial = [1,1,1,1,1,1];
%Optimize ODE parameters, results stored in Coeff_ODE.
options = statset('Display', 'iter');
[Coeff_ODE,Residuals_ODE] = nlinfit(time, conc, 'D:\ Kin\ fitODE', initial, options);
%Create C vs. T profile of the fit. Interval time step can be changed from tspan = 0:STEP:time(end)
tspan = 0:60:time(end);
[ODE_time, ODE_conc] = ode45('ODE', tspan, SM0);
The fitODE function is:
function y = fitODE(a, x)
global a1 b c d e f;
a1 = a(1);
b = a(2);
c = a(3);
d = a(4);
e = a(5);
f = a(6);
%set the time step for integration, 0:x(end) uses the default step.
%Data sets with large a large absolute time span (not number of data
%points) can be manually set a step size via 0:STEP:x(end). If desired,
%this can be set back to the default after a good set of initial
%parameters are located
tspan = 0:x(end);
%output the C vs. T profile defined from the current set of parameters
[t,y] = ode45('D:\Kin\Ode.m',tspan, SM0);
%return the concentration values at the time points defined in the
%input file to determine the residuals used by nlinfit for parameter
%optimization
y = interp1(t, y, x,'nearest');
The ODE function is:
function dx = ODE(t, x)
global a1 b c d e f;
dx = -1*(a1*x+b*x^2+c*x^3)/(d*x+e*x^2+f*x^3+1);

Accepted Answer

Jan
Jan on 19 Aug 2021
These linesmust fail:
ode45('D:\Kin\Ode.m',tspan, SM0);
[Coeff_ODE,Residuals_ODE] = nlinfit(time, conc, 'D:\ Kin\ fitODE', initial, options);
Providing the path of the m files does not work. Add the folder to Matlab's path using pathtool or addpath(foldername, '-end) instead.
This is working, but outdated for 20 years:
[ODE_time, ODE_conc] = ode45('ODE', tspan, SM0);
Providing the function o be integrated as CHAR vector 'ODE' is working for reasons of backward compatibility, but is is recommended to use a function handle instead:
[ODE_time, ODE_conc] = ode45(@ODE, tspan, SM0);
Are you sure that there is an initial space in the folder name?
direct = 'D:\ Kin';
Avoid strcat, but use fullfile to join a path name:
% path = strcat(direct,'\',filename);
pathname = fullfile(direct, filename);
Shadowing the important function path by a variable can cause very strange side-effects during debugging.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!