Bisection method + Differential equation question
1 次查看(过去 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!