Bisection method + Differential equation question
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!