Bisection method + Differential equation question

5 views (last 30 days)
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.

Accepted Answer

Geoff Hayes
Geoff Hayes on 15 Oct 2014
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!