MATLAB Answers

Using ode45 to solve odes from a matrix

3 views (last 30 days)
This is the code I currently have. It works however I have had to manually input the four odes. (Line 8)
My question is, what code can I use to automate this part by using a matrix and a vector of y(i) variables?
The matrix would be [-f1 f1 0 0; 0 -f2 f2 0; 0 0 -f3 f3; v 0 0 -v]
Thank you!
%=====fx represents transition rate for degredation to next state=====
f1=0.5;
f2=0.5;
f3=0.2;
v=0;
%=====Sets the ordinary differential equations as a vector=====
f = @(t,y)[-f1*y(1)+v*y(4); -f2*y(2)+f1*y(1); -f3*y(3)+f2*y(2); f3*y(3)-v*y(4)];
%=====Sets time period=====
tspan = [0 30];
%=====Sets the initial state conditions as a vector=====
y0 =zeros(4,1);
y0(1) = 1;
%=====Calls the integrator=====
[t, y] = ode45(f,tspan,y0);
%=====Plots the results=====
plot(t,y(:,1),t,y(:,2),t,y(:,3),t,y(:,4))
legend('State 1','State 2','State 3','State 4','Location','best')
title('Probability of being in each state at time t.')

  0 Comments

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 23 Mar 2020
Edited: James Tursa on 23 Mar 2020
F = [-f1 f1 0 0; 0 -f2 f2 0; 0 0 -f3 f3; v 0 0 -v].';
f = @(t,y) F * y;

  1 Comment

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019a