Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Subscript indices must either be real positive integers or logicals.
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to run eulers method for a first order differential equation. However recieve the above error when I try to plot the results.
See my code blow:
x=zeros(400,1);
y=zeros(400,1);
x(1)=2;
y(1)=4;
for n=1:400
x(n+1)=x(n)+0.005;
y(n+1)=x(n)+0.005.*(((y).^2)+(7*y)+2)/(3*x((y).^2)+((1-x)*(3*((y)^2)+7)));
end
plot(x,y)
title('eulers')
grid on
What am I doing wrong?
댓글 수: 0
답변 (1개)
KL
2017년 12월 1일
when you write,
y(n+1)=x(n)+0.005.*(((y).^2)+(7*y)+2)/(3*x((y).^2)+((1-x)*(3*((y)^2)+7)));
^
you're trying to access the zeroth element of x (since y is vector with all zeroes except its first element). You need to rewrite that equation. How does the equation in your book look like?
Anyway, if you want to create a vector with 0.005 increaments use linspace or colon(:) operator.
댓글 수: 2
KL
2017년 12월 1일
if you have access to symbolic math toolbox, it's a lot easier. Go through the example here: https://www.mathworks.com/help/symbolic/solve-a-single-differential-equation.html#f1-7555
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!