Loop for function with summation up to N-1 and N
Show older comments

I have the function.
How do I write this in MATLAB?
I tried the below, but I don't think it is correct...
Vchain = 0;
for i = 1:N
Vchain = Vchain + 0.5*(D*(y(i)-y(i+1))^2 + (z(i)-z(i+1))^2) + g0*(m*z(i));
end
2 Comments
Star Strider
on 26 Nov 2017
What is ‘D’?
In the image, ‘D’ looks like a function, not a constant. If it is a constant (as you have written it), the multiplication could be written outside the summation.
yuanb
on 26 Nov 2017
Answers (1)
Youssef Ben Dhiab
on 30 Nov 2017
0 votes
Try this :
Vchain = 0; for i = 1:N-1 Vchain = Vchain + 0.5*D*((y(i)-y(i+1))^2 + (z(i)-z(i+1))^2) + g0*(m*z(i)); end Vchain = Vchain + g0*(m*z(N));
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!