Back calculating Ki from test data
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
dpb
il 30 Ago 2023
Spostato: dpb
il 30 Ago 2023
You can only have N-1 differences.
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 Commenti
dpb
il 30 Ago 2023
Modificato: dpb
il 30 Ago 2023
- 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.
- Well, yes. See the doc link and/or try a test case to see.
- 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...
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!