George Dziouba
George Dziouba

Reputation: 27

MATLAB error at line with ode45

Im trying to get matlab to display a graph of prey vs predetor

function [ output_args ] = Untitled( input_args )

    options = odeset('RelTol', 1e-4, 'NonNegative', [1 2]);

    [t,x] = ode45('lotka_volterra', [0 30], [2 1], options);

    plot(t,x);
    legend('prey', 'predators');

end


function dxdt = lotka_volterra(t,x)

    a     = 1.2;
    b     = 0.6;
    d     = 0.3;
    gamma = 0.8;

    dxdt    = [0;0];
    dxdt(1) = a * x(1) - b * x(1) * x(2);
    dxdt(2) = d * x(1) * x(2) - gamma * x(2);

end

The point is to display the dynamic relationship between the populations of two species living in the same environment.

The two derivatives(dxdt(1) and dxdt(2)) model the rate of change of each population

Im tryint to use the Runge-Kutta method of Integration to get the plot of the population vs time, of the two species

The error just says:

Error in Untitled (line 3)
[t,x] = ode45(@(t,x) lotka_volterra, [0 30], [2 1]);

Im very stumped any help would be great

Upvotes: 0

Views: 101

Answers (1)

user2271770
user2271770

Reputation:

You might want to use the function handle:

[t,x] = ode45(@lotka_volterra, [0 30], [2;1], options);

or, slower but maybe clearer:

[t,x] = ode45(@(t,x) lotka_volterra(t,x), [0 30], [2;1], options);

Upvotes: 1

Related Questions