How to build a vector for a system of ODEs

1 view (last 30 days)
Hi,
I'm looking to automate some code used to solve systems of ODEs. Right now I'm having to type in my system of ODEs to be solve, but as the problems get bigger, and as I need more equations, I'd really like to have it done for me. This is the code I wrote to try to achieve this, however as I build dxdt and dydt in the nested for loop the actual indices aren't getting inserted into the equations, the i's and j's get left in there and I get the error telling me my indices need to be real or positive when I try to solve it. Is there a way to get the numbers to stay for my indexing?
Thanks in advance
N = 2;
gamma1 = 1;
gamma2 = 2;
x0 = [1 2]; %Initial Conditions
y0 = [1 1];
sum = 0;
for j = 1:N
for i = 1:N
if(i ~= j)
dxdt{j} = @(t,x) gamma(i)/2/pi*-1*(x(j)-x(i))/((x(j+N)-x(i+N))^2 + (x(j)-x(i)));
dydt{j} = @(t,x) gamma(i)/2/pi*(x(j+N)-x(i+N))/((x(j+N)-x(i+N))^2 + (x(j)-x(i)));
end
end
end
for i = 1:N
f{i}= @(t,x) dxdt{i};
end
for i = 1:N
f{i+N}= @(t,x) dydt{i};
end
[t,x] = ode113(@(t,x) f(t,x), [0 200], [y0 x0]);
plot(x(:,3),x(:,1), 'r', x(:,4),x(:,2), 'b')

Answers (0)

Community Treasure Hunt

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

Start Hunting!