why is my vector bigger than how I created
    조회 수: 7 (최근 30일)
  
       이전 댓글 표시
    
I tried to do a remain sum using matlab and graph it,
why is my sumvec vector is 101? should the while loop runs from 10 to 100
%equ = sqrt(x+1)
% calculating remain sum of sqrt(x+1) from with 10 to 100 sub interval
sum = 0
sumvec = []
for n = 10:100
    sum = 0
    width = (3 - 0)/n
    for c = 0:n
        base = width * c
        height = sqrt(base+1)
        sum = sum + (width * height)
    end
    sumvec(n+1) = sum
end
figure;
i = 0:101
scatter(i,sumvec)
댓글 수: 1
  Stephen23
      
      
 2020년 11월 22일
				"why is my sumvec vector is 101? should the while loop runs from 10 to 100"
When n==100, which element do you expect this indexing to refer to?:
sumvec(n+1) = ..
Important: do NOT call any variable sum, as this shadows the very important inbuilt sum function.
채택된 답변
  Setsuna Yuuki.
      
 2020년 11월 21일
        
      편집: Setsuna Yuuki.
      
 2020년 11월 22일
  
      Because the values are saved from space 11 onwards.
You must change n+1 in sumvec()
n+1 = 11 -- n-9 = 1
sum = 0
sumvec = []
for n = 10:100 
    sum = 0;
    width = (3 - 0)/n;
    for c = 0:n
        base = width * c;
        height = sqrt(base+1);
        sum = sum + (width * height);
    end
    sumvec(n-9) = sum; %% change n+1 -- n-9
end
figure;
i = 1:length(sumvec) %i vector
scatter(i,sumvec)
댓글 수: 0
추가 답변 (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!