Why isn't my code iterating more than once?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Fifteen12
2023 年 9 月 21 日
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.
回答 (1 件)
Walter Roberson
2023 年 9 月 22 日
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 件のコメント
Walter Roberson
2023 年 9 月 23 日
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"
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!