Referencing a Empty matrix

4 vues (au cours des 30 derniers jours)
Zack Bayhan
Zack Bayhan le 31 Oct 2014
Commenté : Star Strider le 1 Nov 2014
Not 100% sure this question will make sense however here it goes. I am attempting to write a while loop and when the loop breaks down and gets stuck the reason is because the matrix that its suppose to retrieve information from is empty. See code below but I was wondering if it's possible to send it to the if option by referencing the empty matrix so I can by pass where the code fails? I'm specifically talking about the time=[] criteria. Thanks for the help Zack
while i<50 && i>1
time=find(conb>.6);
times(i)=time(1);
x=1
if conb(i)<.6 && time=[]
i=n+1
x=1
end
end

Réponse acceptée

Star Strider
Star Strider le 31 Oct 2014
I can’t run your code, but if you want to avoid situations where ‘time’ is empty, change your if statement to:
if conb(i)<.6 && ~isempty(time)
  1 commentaire
Star Strider
Star Strider le 1 Nov 2014
I kept working on the problems with your code, and came up with some solutions:
----------------
I sort of solved one problem by changing the ODE solver and substituting a time vector for a time range:
[time,c]=ode15s(dcdt,[0:0.5:10],[1;0]);
because I couldn’t figure out why it was quitting without updating its time vector, leading me to believe it is a stiff problem and ode45 was getting stuck. It’s still having problems integrating your ODE, but it keeps working at it and doesn’t throw any errors (as long as I’ve let it run).
I have no idea what you’re doing so I can’t comment on your ODE’s structure or constants. I leave it to you to experiment with that and troubleshoot it. It might be worthwhile to plot ‘time’ and ‘c’ and see if anything looks strange.
I also have no idea what you’re doing here:
time=find(conb>.6);
times(i)=time(1);
since the assignment to ‘time’ destroys the ‘time’ vector ode15s kindly provides you. It also returns ‘time’ in that assignment as an index (because that’s what the find function returns), not an actual time. I suggest you name ‘time’ something else, either in that statement or in the ode15s output, so you don’t destroy the ode15s ‘time’ vector.
Those are the only other problems I could identify (and do my best to fix), but there could be others. I can say with some confidence that it now runs (as long as I’ve let it run) without errors. I leave you to troubleshoot the rest.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by