Why isn't my code iterating more than once?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello Everyone
I'm developing a code for a kalman filter in a mobile robot. The idea of this code is that updates the robot position x,y, theta every 0.05seconds up to 40 seconds. There should be 800 iterations but not sure why my code only iterates once?. I have put it in a while loop so it should work?. can someone help me why the code isn't working?. i'll send my code and mat files down below . any help would be really appreciated
Many Thanks
Daniel
5 commentaires
Fifteen12
le 21 Sep 2023
I'm not sure why it's not running 800 times, but I would recommend using a for loop instead of a while loop. It's more robust.
for K = 1:800
% run your loop here
end
That way, you don't have to increment K each time.
Réponses (1)
Walter Roberson
le 22 Sep 2023
r1_real=me(1,K);
r2_real=me(2,K);
b1_real=me(3,K);
b2_real=me(4,K);
Those are all scalars.
x=(r1_real(1,K).^2-r2_real(1,K).^2)/2;
y= (16+sqrt(4-8*(14-r1_real(1,K).^2-r2_real(1,K).^2)))/4;
But there you try to index r1_real at location K in 3 spots, and r2_real at location K as well. Which is a problem because they are scalars.
4 commentaires
Walter Roberson
le 23 Sep 2023
Sometimes it helps to first write the code in terms of "current" values. You might have matrices, and you might be looping over them, but it can help to extract the particular values you are interested in right now into scalars, and perform calculations on the scalars, getting a "current" result... which you then might store at the appropriate location in some output array.
Doing this will not necessarily result in the fastest possible code -- but what it does do is make it easier to debug the code. And many professional developers recommend that the highest priority is in first coding correct code, and only then consider optimizing that code... while keeping the correct implementation around for side-by-side testing to be sure that the "optimized" version of the code is producing reasonable answers.
Getting the code right first (instead of getting the code fast first) can make it a lot easier to figure out the "corner-cases". And oddly enough, when you have correctly functioning code up to a point and you start working through all of the "corner-cases", then it is common to realize that the "corner-cases" follow a pattern that can lead to more compact code. Whereas when you are working on making the code fast first, then you are often too busy trying to do mental gymnatistics worrying about whether the "fast" version of the code is correct, making it difficult to figure out efficient ways to handle the "corner-cases"
Voir également
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!