Why do I receive "Not enough input arguments" error message?
古いコメントを表示
Dear all,
I try to fit a model of three differential equations with 10 parameters to a set of experimental data using lsqnonlin and ode45. Now I receive the "Not enough input arguments" error message. As I am quite new to MATLAB, I fail to find the solution myself and would appreciate your help. Please find the code below:
function optimguard
%Script to optimize model
clear global, clear all, clear clc
global Data tspan y0 p0
%parameter and initial data
%time
tspan = [0.1672 3.1179 5.0734 7.2074 9.2317 11.0262 13.9450 17.0018];
% biomass (bm), substrate (su), product (pr)
Data = [3.7731 4.5776 5.74589 7.336 10.1128 12.0855 13.79801 12.5260;
29.7731 28.0920 24.8625 14.5990 5.7511 0.4424 0.3982 0.2654;
0.017906 0.040799 0.072079 NaN 0.113105 0.135998 0.047146 0.027426];
% initial parameters
mux_max = 0.105;
mum_max = 0.007;
ms = 0.2;
mup_max = 0.0016;
Yx = 0.8;
Yp = 0.01;
K1 = 0.1;
K2 = 0.18;
K3 = 0.4;
kd = 0.006;
p0 = [mux_max mum_max ms mup_max Yx Yp K1 K2 K3 kd];
%Optimization function and options
options = optimoptions(@lsqnonlin, 'Display', 'iter');
pnew = lsqnonlin(optimfunct, p0,[],[],options);
%solve ode with optimal parameters
[t, sol] = ode45 (@guard, t, Data,'', pnew);
function grm = optimfunct(p);
%solve ode with initial parameters
[t, sol] = ode45(@guard, tspan, y0,' ',p);
%addition residues
grm = sum(abs(sol(1)-Data(1,:)))+sum(abs(sol(2,:)-Data(2)))+sum(abs(sol(3)-Data(3)));
%FILE guardiola.m
function dsolution = guard(t,y,p)
%initial vaules for ode: bm0, su0, pr0, vi0
bm0 = 3.7731;
su0 = 29.7737;
pr0 = 0.017906;
y0= [bm0 su0 pr0 ]
%parameter
mux_max = p(1);
mum_max = p(2);
ms = p(3);
mup_max = p(4);
Yx = p(5);
Yp = p(6);
K1 = p(7);
K2 = p(8);
K3 = p(9);
kd = p(10);
%equations
G1 = y(2) / (y(2) + K1);
G2 = y(2) / (y(2) + K2);
G3 = y(2) / (y(2) + K3);
dbm_dt = (mux_max*G2)*y(1)-(mum_max*(1-G1))*y(1);
dsu_dt = -(mux_max/Yx)*G2*y(1)-ms*G1*y(1)-(mup_max/Yp)*G3*y(1);
dpr_dt = y(1)*mup_max*G3-y(3)*kd;
dsolution = [dbm_dt;dsu_dt;dpr_dt];
end
end
end
3 件のコメント
Please give the full error message, including the line that causes the error. It is a lot harder for us to see at a glance what is the problem when we don't know which line is actually causing the error.
Looking at the documentation for ode45, the function handle seems to expect two arguments, not 3 and I'm not seeing a calling syntax for ode45 that takes 5 arguments, with 'p' as its 5th either.
@(t,y) guard( p )
may be the syntax you are looking for to pass to ode45 and leave p off the end of the argument list.
Sebastian Hans
2017 年 4 月 24 日
That sounds like the problem that I was looking at then. I have never used ode45, so I'm not familiar with the other arguments it takes, but in terms of the function handle argument it should behave like a function handle passed anywhere else.
You get a 'Not enough input arguments' error because your function expects 3 inputs and ode45 is only giving it 2 when it calls it.
i.e. you have two distinct sets of arguments - those defined at the time you define the function handle (these are effectively baked into the function as constants) and those that are passed in when the function is called, for the sake of argument call these free variables.
The ode one, judging by a quick look at the documentation, expects your function to take 2 free variables. You can embed/bake in as many other variables as you like when you initially define the function (e.g. you can embed 'p' into it), but it must accept two free variables e.g.
a = 5;
b = 7;
c = 8;
f = @(x,y) a + b + c + x + y;
is a function handle taking two free arguments:
K>> nargin( f )
ans =
2
The fact that you defined it based on 5 variables does not matter, when you call it you will only give it two at calling time:
f(4,7);
This allows you to pass some very complex functions into generic solvers and other functions that accept a function handle, because so long as your function accepts the arguments that are expected it can do whatever else it likes, defined by as many other variables as you like so long as they exist at the time the function handle is defined.
回答 (1 件)
In the line
pnew = lsqnonlin(optimfunct, p0,[],[],options);
the function optimfunct is called without inputs. Do you mean:
pnew = lsqnonlin(@optimfunct, p0,[],[],options);
?
See http://www.mathworks.com/matlabcentral/answers/1971 for providing parameters to functions to be integrated or optimized.
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!