Rhinocerotidae
Rhinocerotidae

Reputation: 925

Input matrix to function file of ode45

I have a code(which requires a lot input be given by the user) which will give me a n x n matrix(say A), which I have to use to solve a system of ODEs X'=AX. How do I include this matrix A in the function file(.m file) of ode45. If I include the code in the function file as below:

function xp=g_test(t,x);
k=input('something');
A=some manipulation of inputs;
xp=A*x;
end

Matlab asks for input at each timestep(typically my problem has 30k timesteps). So how do I include/ pass the matrix A to the function?

Upvotes: 2

Views: 1089

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 14939

You can create a function that returns a function handle:

function odeFcn = makeODE(a,b,c)  
    A = some_function(a, b, c);
    odeFcn = @(t,x) A*x;
end

Now you can call ode45 with input matrices a, b, c:

outputVector = ode45(makeODE(a,b,c), [t0, t1], x0);

The inspiration is taken from gnovice's answer here.

Upvotes: 3

Related Questions