MATLAB: Index exceeds matrix dimensions problem

index exceeds matrix dimensionsMATLABwhile loop

Hi, my task is to calculate some parameters connected with a sattelite. I'm almost done but there is one error I'm not able to solve. Here is a part of my code:
if true
% code
end
u = 1;
Ek(u) = Mk;
while 1
u = u + 1;
Ek(u) = Mk + e*sin(Ek(u-1));
if abs(Ek(u) - Ek(u-1)) < 5e-5
break; end
Ek = Ek(u);
end
When I run my script this info appears in the Command Window: Index exceeds matrix dimensions.
Error in wsp_xyz_satelity (line 41) Ek(u) = Mk + e*sin(Ek(u-1));
Does anyone have an idea why it occures? I have done many while loops before in the same way and never came across a mistake like that. Thank You for your advice in advance đŸ™‚

Best Answer

Ek = Ek(u);
At the line above, you assign a scalar value to Ek which means only Ek(1) will be accessible, and after that you try to reach its 3rd, 4th,.. elements. Please check that.