Loop for function with summation up to N-1 and N

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

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
yuanb 2017년 11월 26일
In the assignment D = (70/40N) (N/m) and in the fixed part of the given code D = 70.
This was also given Vchain = Vchain + ...;
My code is not working, so something is wrong.

댓글을 달려면 로그인하십시오.

답변 (1개)

Youssef Ben Dhiab
Youssef Ben Dhiab 2017년 11월 30일

0 개 추천

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));

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2017년 11월 26일

답변:

2017년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by