Reputation: 4596
I have a use case as follows:
Inside F.m
I have a function F
that takes as its argument a 2 x 1
matrix x
. F
needs to matrix multiply the matrix kmat
by x
. kmat
is a variable that is generated by a script.
So, what I did was set kmat
to be global in the script:
global kmat;
kmat = rand(2);
In F.m
:
function result = F(x)
global kmat;
result = kmat*x;
end
Then finally, in the script I have (x_0
has already been defined as an appropriate 2 x 1
matrix, and tstart
and tend
are positive integers):
xs = ode45(F, [tstart, tend], x_0);
However, this is causing the error:
Error using F (line 3)
Not enough input arguments.
Error in script (line 12)
xs = ode45(F, [tstart, tend], x_0);
What is going on here, and what can I do to fix it? Alternatively, what is the right way to pass kmat
to F
?
Upvotes: 0
Views: 1301
Reputation: 8459
Firstly, the proper way to handle kmat
is to make it an input argument to F.m
function result = F(x,kmat)
result = kmat*x;
end
Secondly, the input function to ode45
must be a function with inputs t
and x
(possibly vectors, t
is the dependent variable and x
is the dependent). Since your F
function doesn't have t
as an input argument, and you have an extra parameter kmat
, you have to make a small anonymous function when you call ode45
ode45(@(t,x) F(x,kmat),[tstart tend],x_0)
If your derivative function was function result=derivative(t,x)
, then you simply do ode45(@derivative,[tstart tend],x_0)
as Erik said.
Upvotes: 1
Reputation: 4563
I believe F
in ode45(F,...)
should be a function handle, i.e. @F
. Also, you can have a look at this page of the MATLAB documentation for different methods to pass extra parameters to functions.
Upvotes: 0