How to solve multivariable ODE

43 views (last 30 days)
Jessica Dominic
Jessica Dominic on 21 Oct 2020
Answered: Alan Stevens on 27 Oct 2020
I have the equations:
dx/dt = 2 - 0.09x + 0.038y
dy/dt = 0.066x - 0.038y
x(0) = 0
y(0) = 0
Ultimately I need to solve the ODE and plot for x and y versus time (t). I just don't know how to code for the equations in Matlab.
Thanks

Answers (2)

Manvi Goel
Manvi Goel on 27 Oct 2020
In order to solve for a system of differential equations, you could use the dsolve function. You can refer to this link which explains the implementation with an example:

Alan Stevens
Alan Stevens on 27 Oct 2020
If you don't have the symbolic toolbox available you could just do something like the following
% Define gradient function where X(1) = x and X(2) = y
dXdt = @(t,X) [2 - 0.09*X(1) + 0.038*X(2); 0.066*X(1) - 0.038*X(2)];
% Set your timespan
tspan = [0 1]; % Modify as desired
% Set your initial conditions IC = [x(t=0) y(t=0)]
IC = [0 0];
% Call ode solver
[t, X] = ode45(dXdt, tspan, IC);
% Extract x and y
x = X(:,1);
y = X(:,2);
% Plot results
plot(t,x,t,y),grid
legend('x','y')

Tags

Community Treasure Hunt

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

Start Hunting!