Output is -INF

7 visualizzazioni (ultimi 30 giorni)
Marios Christofides
Marios Christofides il 5 Lug 2020
Risposto: dpb il 5 Lug 2020
I added a while loop to my code so that it would output the time with the data every .5 seconds as well as a mod funciton to only output every 100th result. I added this to my code and the output is only -INF for everything. Does anyone know how to fix it?
while t <= 12
t = t + .5;
for c = 1:1:nt
%Forward Euler Method
x1 = xi + dt*(a*xi*(1 - xi/20) - b*xi*yi - c*xi*zi);
y1 = yi + dt*(yi*(1-yi/25) - a*xi*yi - d*yi*zi);
z1 = zi + dt*(b*zi*(1-zi/30) - xi*zi - yi*zi);
xi = x1;
yi = y1;
zi = z1;
data = [t,xi, yi, zi];
if mod(c,100)==0
fprintf(1, '\t%d\t%06f\t%12.5E\t%.2f\n\n', data);
end
end
end
Time = toc

Risposte (1)

dpb
dpb il 5 Lug 2020
You had
...
x1 = xi + dt*(a*xi*(1 - xi/20) - b*xi*yi - c*xi*zi);
y1 = yi + dt*(yi*(1-yi/25) - a*xi*yi - d*yi*zi);
before with both c and d undefined in the posted code (as well as were a, b and other things...so those had to have been set in workplace before.
Now you've introduced and reused variable c for some other purpose -- that undoubtedly is at least part of the problem.
I recommend strongly pull all your code together in one script m-file; and make sure it's working again completely independently -- putting a function keyword at beginning would make it such that anything not defined in the function would be flagged...
Then, once you've got that working (again), then you can introduce some new counter variable...

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by