Error with brackets symsum
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Alan Stevens
am 22 Dez. 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
2 Kommentare
Alan Stevens
am 22 Dez. 2022
If N is constant I would expect the sum of dndt over all states to be zero, but it doesn't seem to be.
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!