필터 지우기
필터 지우기

Trying to do central difference using for loop, getting error

조회 수: 2 (최근 30일)
Ismail khalil
Ismail khalil 2016년 12월 6일
댓글: Ismail khalil 2016년 12월 6일
m=1;
k=1;
c=1;
x(2)=0;
dx=1;
d2x=-1;
h=0.5;
for i=2:0.1:20
x(i-1)=x(i)-h*dx+(h/2)*d2x;
x(i+1)=(1/(m/h^2)+(c/2*h))*((2*m/h^2-k).*x(i)+(c/2*h-m/h^2).*x(i-1));
end
plot(x)
what is wrong with my code, I am getting an error Subscript indices must either be real positive integers or logicals. As you can see I am trying to get a plot of x(i+1) which depends on x(i) and x(i-1), thanks
appreciate all the help
  댓글 수: 1
Jan
Jan 2016년 12월 6일
I've formatzted your code using the "{} Code" button. You can do this by your own also.

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

채택된 답변

David Barry
David Barry 2016년 12월 6일
You must index into an array/matrix with integer values. In your case you are trying to index into variable x with the variable i but you have asked for i to range from 2 to 20 in 0.1 steps. What are you expecting x(2.1) to give for example?
  댓글 수: 1
Ismail khalil
Ismail khalil 2016년 12월 6일
I have done this and got an a run indeed, but the answer was very different in value than I expected, but I guess you solved my error, thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by