Error with brackets symsum
3 views (last 30 days)
Show older comments
f2 = figure;
figure(f2);
ic = linspace(0, 100, K);
[t, n] = ode 45(@(t, n) ODEfunc(n, K);
k_bar = 1/N * symsum(i*n(i),i,0,K);
**I want to solve some ODES eventually with k_bar
I keep getting errors with brackets, im not sure If im tired but i cant spot where its referring to!
Thanks in advance
0 Comments
Answers (1)
Alan Stevens
on 22 Dec 2022
Do you mean something more like this?
N = 100; % Total number of particles
one = 1;
alpha = 1;
K = 7; % 7 states
n0 = 4*ones(K,1);
tspan = 0:0.1:5;
[t, n] = ode45(@(t, n) ODEfunc(t, n, K, one, N, alpha), tspan, n0);
plot(t, n),grid
xlabel('time'), ylabel('n')
legend('n1','n2','n3','n4','n5','n6','n7')
function dndt = ODEfunc(~, n, K, one, N, alpha)
S = n(1);
for i=2:K
S = i*n(i) + S;
end
k_bar = S/N;
dndt = zeros(K,1);
dndt(1,:) = alpha*n(1)*k_bar - one*n(1); % Note that this is for state 1.
for i= 2:K-1
dndt(i,:) = alpha*n(i)*(k_bar - i) + one*n(i-1) - one*n(i); % For state 2 to 6
end
dndt(K,:) = alpha*n(K,1)*(k_bar - K) + one*n(K-1,1); % For state 7
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!