Why isn't my code iterating more than once?

2 vues (au cours des 30 derniers jours)
Daniel Pitigala
Daniel Pitigala le 21 Sep 2023
Commenté : Walter Roberson le 23 Sep 2023
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
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.
Daniel Pitigala
Daniel Pitigala le 22 Sep 2023
Hi thanks for the advice
I have implemented a for loop but not sure why it's not working. does anyone else have more suggestions?. Always when i run the code it comes with an error saying that "Index in position 2 exceeds array bounds). I'm preety certain that i have incremented K correctly and all?. any additional help is greatly appreciated. I have my code files referenced in my first initial question
Many thanks
Daniel

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
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
Daniel Pitigala
Daniel Pitigala le 23 Sep 2023
Hi Walter
Thankyou for your reply. I see what the problem is now but i'm unaware on how to fix the issue?. I'm relatively new and still very much a beginner to matlab . Would you be aware on how to not make these values a scalar quantity and make the code count 800 interations sucessfully?
Many Thanks
Daniel
Walter Roberson
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"

Connectez-vous pour commenter.

Catégories

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

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by