필터 지우기
필터 지우기

Bisection method + Differential equation question

조회 수: 2 (최근 30일)
JK
JK 2014년 10월 15일
답변: Geoff Hayes 2014년 10월 15일
I need to calculated 100 values using differential equation and bisection method, but it is giving me only 1 value. I have 100 a1 and a2 values initially, and the time needed when m>1 should be calculated. this is where I got stuck..
tmax=10;
tmin=0;
error=tmax-tmin;
for i=1:100
dmdt=@(t,m)[-m(1).*a1(i);m(1).*a1(i)-m(2).*a2(i)];
while error>10^-5
treal=(tmax+tmin)/2;
[t,m]=ode45(dmdt,[0 treal(i)],[1;0]);
if m(end,2)>1
tmax=treal(i);
else tmin=treal(i);
end
error=tmax-tmin;
end
end
treal
I have it as treal and it is giving me only 1 value instead of 100.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 10월 15일
Are you not observing some sort of Index exceeds matrix dimensions error? Look at the following lines from your while loop
while error>10^-5
treal=(tmax+tmin)/2;
[t,m]=ode45(dmdt,[0 treal(i)],[1;0]);
% etc.
end
On each iteration of the while loop, treal is set to a scalar value (since tmax and tmin are scalar values). The following line tries to access treal(i) which, for i>1 should throw the above error message.
It may be that you want to do the following instead
while error>10^-5
treal(i)=(tmax+tmin)/2;
[t,m]=ode45(dmdt,[0 treal(i)],[1;0]);
% etc.
end
so that as long as we are in the while loop for the ith iteration of the for loop, we continue to update treal.
I suggest replacing i with k (as your index value) since i (and j) is also used as the representation of the imaginary number.

추가 답변 (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