Loop for function with summation up to N-1 and N
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Star Strider
2017-11-26
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.
回答(1 个)
Youssef Ben Dhiab
2017-11-30
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));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!