필터 지우기
필터 지우기

Storing Values From a Loop

조회 수: 4 (최근 30일)
Kyle Fertig
Kyle Fertig 2017년 11월 5일
댓글: Kaushik Lakshminarasimhan 2017년 11월 5일
I'm doing a project involving an implementation of Euler's method for class, and am using a "for" loop to increment my values. The code below runs, but whatever new y value I get is overwritten as the loop goes through. I need to be able to save those y values after each time through the loop and put them into a matrix so that they can later be graphed. I'm not sure how to do this, so any help is appreciated!
a=1250
q=500
c=160
n=0.5
y=1.1
for t=0:n:2
s=3*(q/a)*sin(t)^2-(c*(1+y)^1.5)/a
y=y+s
end
Thanks,
-Kyle

채택된 답변

Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2017년 11월 5일
Replace
s=3*(q/a)*sin(t)^2-(c*(1+y)^1.5)/a
y=y+s
with
s=3*(q/a)*sin(t)^2-(c*(1+y(end))^1.5)/a;
y=[y y(end)+s];
  댓글 수: 2
Kyle Fertig
Kyle Fertig 2017년 11월 5일
That's awesome, thanks! How does the (end) thing work? I assume it's a sort of function since the "s=" equation is still starting y at 1. 1
-Kyle
Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan 2017년 11월 5일
end is a keyword, not a function. It simply picks out the last element of an array. For example, if x = [1 2 3 4 5], then x(end) = 5

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by