calculate coefficient C. while loop nested in for loop

조회 수: 1 (최근 30일)
Tyde Hilderbrandt
Tyde Hilderbrandt 2021년 11월 1일
답변: Walter Roberson 2021년 11월 1일
Hey,
I'm trying to create a program which follows the formula
the vector must have at least two elements. Here is my code so far which I belive has the wrong nested while loop so the program never ends.
clc
clear
v=[0 4 2 6];
c=0;
deltaL=.5;
for i=1:length(v)
while v(i+1)<= length(v)
c=c+((v(i+1)-v(i))/deltaL);
end
end
c

답변 (1개)

Walter Roberson
Walter Roberson 2021년 11월 1일
v = [0 4 2 6];
deltaL = .5;
c = sum(diff(v) ./ deltaL)
c = 12
c_shortcut = (v(end) - v(1)) ./ deltaL
c_shortcut = 12
You should recheck your formula: unless deltaL can change for each item, then mathematically your c has to equal what I calculate in c_shortcut

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by