MATLAB Answers

How do I ensure the loop is executed for the duration of the counts i.e. 10 inside the loop?

1 view (last 30 days)
Abdul Karim
Abdul Karim on 26 Jul 2021
hi, i am having issues assigning variables inside the for loop for this simple filter. i want the filter loop to execute each equations inside the body over the duration of the loop (ie 10 counts for this filter)?
Thanks for your help!
% below is my code
a = 0.493;
b = 0.739;
g = 0.00234;
xi = 5;
vi = 1.5;
vok = 2;
gi = 1.3;
range = [r_raw]; % assume r_raw is [1 10] vector
for k = 1:1:length(r_raw)
xpk = xi + vi*dt + 0.5*(dt^2*gi);
vpk = vi + dt*gi;
apk = gi;
xsk = xpk + a*(range-xpk);
vsk = vpk + b*(vok-vpk);
ask = apk + g*(range-xpk)/dt^2;
xpred(k) = xsk;
% vpred(k) = vsk;
% apred(k)= ask;
figure; plot(xsk);
i get the following message:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 26 Jul 2021
There is an err in your code:
xpred(k,:) = xsk; % Corrected line
abdul karim
abdul karim on 27 Jul 2021
How can I ensure the entire statements are repeated over the duration of the iterations within the body ie in this example 10 times? To be clear each I want the each statement (equation) to perform iterations over the entire duration, so k=1:1:length(r_raw) = 10? In hindsight, as more iterations are performed, the values should converge to the true value. Assume r_raw is an array of size (1,10) containing vectors.
I understand it may be simple but I can't think of what the issue is therefore help is really appreciated. Thanks again for your help.
I included the code again.
a = 0.493; b = 0.739; dt=0.001; t=(1:fr)*dt; xi = 0; vi = 1.5; vok = 2; range = [r_raw]; for k = 1:1:length(range ) xpk(k,:) = xi + vi*dt ; vpk(k,:) = vi; xsk(k,:) = xpk + a*(range-xpk); vsk(k,:) = vpk + b*(vok-vpk); xpk(:,k) = xi + vi*dt; vpk(:,k) = vi + dt*gi; xsk(:,k) = xpk + a*(range-xpk); vsk(:,k) = vpk + b*(vok-vpk); xpk(:,k)= xi + vi*dt; vpk (:,k)= vi + dt*gi xsk (:,k)= xpk + a*(range-xpk); vsk (:,k) = vpk + b*(vok-vpk); xpred(:,k) = xsk; end figure; plot(xsk);

Sign in to comment.

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 27 Jul 2021
In your code, there are a few crucial errs, i.e.:
(1) no need to introduce a new variable range that creates conflict with matlab's built in fcn range().
(2) why to use the same variable with different index orders: xpk(k,:) vs. xpk(:,k), xsk(k,:) vs. xsk(:,k), etc.
(3) range-xpk does not work because range has 10 elements and xpk has different number of elements. To avoid this problem, you should use: xpk=xi + vi*dt; xsk=r_raw(k)-xpk;
(4) If you want to get xpred only, there is no need to have indexes for other variables.
(5) fr and gi are not specified.



Community Treasure Hunt

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

Start Hunting!

Translated by