I am trying to create a loop to solve a system of differential equations, where x' = -y and y' = x. The initial conditions given are x(0) = r and y(0) = 0, but r can be set as any arbitrary number and it won't change the solution too much. I am getting this error that says "Array indices must be positive integers or logical values", but I am confused because I am nonly trying to set initial conditions. ANY help is greatly appreciated!!!

 채택된 답변

Torsten
Torsten 2022년 4월 28일
편집: Torsten 2022년 4월 28일

0 개 추천

Array indices in MATLAB start at 1, not at 0.
Thus you will have to replace
x(0)=1
y(0)=0
by
x(1)=1
y(1)=0
And you solve the wrong equations. What you solve is
x' = -t
y' = y
Look at how you call dxdt and dydt.
In both cases, the call must be
x(n+1) = x(n) + h*dxdt(x(n),y(n))
y(n+1) = y(n) + h*dydt(x(n),y(n))
Further, by the line
t(n+1) = a+n*h
you overwrite the t-vector you've already created by
t=a:h:b
So you should delete this line.

댓글 수: 4

ISABELLA CORONADO
ISABELLA CORONADO 2022년 4월 28일
Hi, thank you SO much for your response. I actually ended up creating a code that runs, but if you see this and can give me some feedback that would be amazing. The graph doesn't really make sense when I compare it to the solution that I found using the eigenvalue method for my system (x' = -y, y' = x), but I think that's because the forward euler method just isn't a good approximation for this system. I'm just wondering if everything looks correct on the coding end of things, and if not, if this could be the reason that my graph looks nothing like the numerical solution I calculated.
First, I created a function with a loop for the forward euler method, then called it in my live script with a plotting function to visualize my results. I'm wondering, did I plot things correctly? Is my indexing okay? And finally, is there a reason one of my plots is just the constant zero line?
If you see this, thank you so much for your help. You are a great person!!!!!!!
Torsten
Torsten 2022년 4월 28일
편집: Torsten 2022년 4월 28일
As already noted, you solve
x' = -t
y' = y
instead of
x' = -y
y' = x
The solution to your system is
x(t) = -1/2*t^2 + 1, y(t) = 0.
The solution to the correct system is
x(t) = cos(x), y(t) = sin(x).
The reason is that you did not read my response properly.
ISABELLA CORONADO
ISABELLA CORONADO 2022년 5월 3일
I see. I was confused by your answer involving t, as manually solving the differential equation does not involve t. I ended up using the code attached, and it worked perfectly. I am not familiar enough with matlab to understand if this is equivalent to your suggestion, so I am not sure if what you suggested would have provided the correct results, but I appreciate your help in my process.
Torsten
Torsten 2022년 5월 3일
편집: Torsten 2022년 5월 3일
As I wrote in my first response, the call to dydt must be
x(n+1) = x(n) + h*dxdt(x(n),y(n))
y(n+1) = y(n) + h*dydt(x(n),y(n))
instead of
x(n+1) = x(n) + h*dxdt(x(n),t(n))
y(n+1) = y(n) + h*dydt(y(n),t(n))
as you first did, and that's what you finally found out by yourself. That's optimal.

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

추가 답변 (1개)

KSSV
KSSV 2022년 4월 28일

0 개 추천

The loop should be taken as:
for i = 1:round(N)
In your present case, it is a fraction and you are getting non integers as the indices and error is pooped.
% Demo
N = 10.2;
for i = 1:N
i
A(i) = rand ; % error, index cannot be fraction
end

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 4월 28일

편집:

2022년 5월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by