Hello,
I have a simple PI loop that I am tying to back calculate Ki using test data. From the loop, Ki is given by the below equation:
[Current Trim Value - Previous Trim Value] / Error*Kp*Ts. I have test data for the Error and Trim in the form or an m x 1 array. Any idea ow I can do this? The main challenge I am having is how to calculate [Current Trim Value - Previous Trim Value] from the data. I tried using the following for loop to do it, but I get an error saying "Array inddices must be positive integers or logical values"
for j = 1: length(TrimArray)
Ki(j) = [TrimArray(j) - TrimArray(j-1)] / Error(j)*Kp*Ts
end
I see why I get that error I'm getting because at J = 1, you can have index number = 0. Note that TrimArray and Error are test data recorded at 50Hz.

 Akzeptierte Antwort

dpb
dpb am 30 Aug. 2023
Verschoben: dpb am 30 Aug. 2023

0 Stimmen

You can only have N-1 differences.
See diff for "the MATLAB way"...
For a loop, you simply start with j=2 instead of 1; done w/o anything else you'll end up with a zero element as the first difference; you can either delete it after the loop finishes or have a second counter for the storage of the output differences...
k=0; %initialize counter
N=length(TrimArray);
Ki=zeros(N-1,1); % preallocate differences array
for j=2:N
k=k+1; % increment counter
Ki(k)=TrimArray(j)-TrimArray(j-1)/Error(j)*Kp*Ts;
end
or
Ki=diff(TrimArray)./Error(2:end)*Kp*Ts;
Other than remembering to use the "dot" division operator for element-wise division and to match up the length by subscript colon expression, the vector version is far shorter to write and easier to actually see what are doing...

3 Kommentare

Bernard Andoh
Bernard Andoh am 30 Aug. 2023
Thank you so much dpb. Much grateful. Just a few questions to cement my understanding of what you are saying:
  1. It seem you are proposing multiple ways to do this. Using a For loop or using the vector version. Is this correct?
  2. For the vector version, is the Diff funcation actually taking the current TrimArray value and subtracting the previous TrimArray value?
  3. Why the 2:end for the Error?
dpb
dpb am 30 Aug. 2023
Bearbeitet: dpb am 30 Aug. 2023
  1. Yes, just illustrating both; "the MATLAB way" is to use vectorized code where can; it generally is faster and certainly is less effort to write -- one line instead of a half-dozen, roughly. The loop solution was presented simply for pedagogical purposes for newcomer.
  2. Well, yes. See the doc link and/or try a test case to see.
  3. What we discussed before and your initial problem -- there can only by N-1 first differences between the N elements in the initial vector; if you use all N elements of the Error vector, the two lengths wouldn't match and Boom! error. Using 2:end selects the one with the same position as the k_th array element as does your initial code; I just copied it. You could use 1:end-1 to use the first postion instead or you could average each successive pair and end up with N-1 error values, too. Your call as to which way you want to do it...(*)
(*) Or, of course, you can choose to augment the differences vector to make it have N elements--
Ki=[0 diff(TrimArray)]./Error*Kp*Ts;
which is the same thing with an artificial zero at the beginning...
Bernard Andoh
Bernard Andoh am 30 Aug. 2023
Thank you very much. Much graful to you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by