필터 지우기
필터 지우기

Why isn't my code iterating more than once?

조회 수: 1 (최근 30일)
Daniel Pitigala
Daniel Pitigala 2023년 9월 21일
댓글: Walter Roberson 2023년 9월 23일
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
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.
Daniel Pitigala
Daniel Pitigala 2023년 9월 22일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
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
Daniel Pitigala
Daniel Pitigala 2023년 9월 23일
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 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 CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by