MATLAB: Summation in a for loop of a function

for loopsummation

Hi everyone! I need to write a function for ode45 solver, but the results I'm getting are wrong. What i need to get is a system of ODEs. To do so i prepared something like this:
function dndt = fun(t,n)
global v
%v is defined in another file
dndt = zeros(numel(v),1);
for i = 1 : numel(v) - 1
dndt(i) = sum(n(i : numel(v))) * v(i+1) - v(i) * n(i);
end
end
Now, my question is: sum(n(i : numel(v))) should be the summation of n(k) with k going from i to number of intervals. Is it correct to write it like that? Or should I introduce another for loop to solve that summation? Thanks!

Best Answer

The code does exactly, what you want:
sum(n(i : numel(v)))
summation of n(k) with k going from i to number of intervals
You can simply try it and check the result manually. Set a breakpoint in this line and use the debugger.