Array Indices Error in for loop
Ältere Kommentare anzeigen
I have the following for loop and when the I run the code I get an error stating "Array indices must be positive integers or logical values" for the line v=(ve/m).... Why am I getting the error and what does it mean?
dt = 0.1;
ho = 0;%initial altitude
vo = 0; %initial speed
ro = R; %initial radius
rhoo = 1.225; %initial density
end_time = 1000;
G = 0;
t = 0:dt:end_time;
v = zeros(1, length(t)); %speed
h = zeros(1, length(t)); %altitude
m = zeros(1,length(t)); %mass
for i = 1:length(t)
if t(i) == 1
m(i) = mo;
v(i) = v;
h(i) = h;
rho = rhoo;
else
m = m(i)- m(i)*dt;
v = (ve/m)*dm*dt + v(i-dt);
h = h(i-dt) + dt*0.5*(v(i)+v(i-dt));
rho = rhoo*10^(-3*h/50000);
end
end
Antworten (1)
the cyclist
am 12 Apr. 2020
The problem is in this expression:
v(i-dt);
The variable i is equal to 1, and the variable dt is equal to 0.1. Therefore, you are trying to access the "0.9th" element of the vector v.
3 Kommentare
Taurai Madziva
am 12 Apr. 2020
the cyclist
am 12 Apr. 2020
So, this is going to be a somewhat harsh comment.
In multiple ways, your code shows that you are very much a MATLAB beginner. There's nothing wrong with that. We all started out as beginners.
Your code has many mistakes in it. It's difficult to help someone via this forum when even a short piece of code shows many basic aspects of MATLAB are not understood.
There are some where I think I can guess what you are thinking. For example, you wrote
if t(i) == 1
...
end
Now, I think what you meant there is that if it is the first iteration of the loop, then initialize the variables instead using the incremental formula. But that's not the right idea. I think you should use
if i==1
...
end
there.
Also, you write
v(i) = v;
which is trying to write an entire vector into one element of itself. I'm guessing you meant
v(i) = v0;
Same problem with h.
Also, your code does not have all the values needed to run it (e.g. dm).
Also, there's the problem I already mentioned in my answer -- which it seems you really have no idea how to solve yourself, and you kind of just guessed at translating your physics equation into MATLAB syntax.
All together, this adds up to a lot of work trying to help you with many basic aspects of MATLAB. That's OK, but often it means someone like me ends up kinda writing your entire solution from scratch, and you don't learn anything.
So, my recommendations would be:
- Take a look (or another look) at basic MATLAB tutorials such as the MATLAB Onramp.
- If possible, find someone closer to you that maybe you can have some real-time interaction with (maybe via Zoom?), because the back-and-forth you would need here could take a long time
- Tell your instructor you need help with the basics
- Try to solve a much simpler problem first, and do some testing of your code. For example, perhaps write a simple formula where v just increases linearly in time, to see if you can get that to work.
Good luck.
Taurai Madziva
am 12 Apr. 2020
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!