Summation in a for loop of a function

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!

 Accepted 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.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

Erg
on 19 Sep 2017

Commented:

Erg
on 19 Sep 2017

Community Treasure Hunt

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

Start Hunting!