How to output the answer to a while loop into an array
조회 수: 30 (최근 30일)
이전 댓글 표시
I have the following code that is supposed to stop once h>1e-13, the problem that I am running into though is that the answer is supposed to be input into an array but I can't figure out how I am supposed to do that because I just keep getting an answer that is a single digit rather than an array with all of the values for h that where calculated this way.
h(1)=1;
hnew=h/2;
while hnew>=1e-13;
h=hnew;
hnew=h/2;
end
The code works like it is supposed to, but I just need to find out how to add each one of the iterations for h into an array
댓글 수: 0
답변 (1개)
Voss
2022년 2월 3일
Maybe this is what you are going for:
h(1) = 1;
hnew = h/2;
while hnew >= 1e-13
h(end+1) = hnew;
hnew = h(end)/2;
end
disp(h);
disp(hnew);
댓글 수: 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!