# Use a command only once

4 views (last 30 days)
saleh shlimet on 19 Sep 2022
Answered: Walter Roberson on 19 Sep 2022
I use a code to search for an accurate rotor position. I used an initial value for the position (theta_b=0) as shown below and then the final position is theta_ij. I want to use the initial position theta_b=0 (line 5) only once after that I use the final position theta_ij as the initial position (theta_b=theta_ij) for the next search (next sample time). How can I do that?
% Inputs are reference voltage Vdr and Vqr,reference current ,Idr and Iqr
% and measured currents (ia, ib)
function theta = fcn(alpha_r, beta_r, d_est, q_est)
% initial rotor position and initial error
theta_b=0;
err_in=beta_r*d_est-alpha_r*q_est;
for i=0:7
theta_delta=(pi/4)*2^-i;
for j=0:7
theta_ij=theta_b+theta_delta*(j-4);
alpha_est=d_est*cos(theta_ij)-q_est*sin(theta_ij);
beta_est=d_est*sin(theta_ij)+q_est*cos(theta_ij);
% the cost function
err=beta_r*alpha_est-alpha_r*beta_est;
if (err < err_in)
theta_b=theta_ij;
err_in=err;
end
end
end
theta=theta_b;
##### 2 CommentsShow NoneHide None
Walter Roberson on 19 Sep 2022
Is the "next sample time" the next call to fcn(), or is it within the same call?
If it is the next call to fcn(), are you willing to add an additional parameter for the first call, to set the initial value, with the additional calls not passing in the parameter? If you are not willing to use that arrangement, then how would you like the code to signal that it wants to start over at 0 (such as for another run of the program) ?
saleh shlimet on 19 Sep 2022
The inputs are measured voltages and currents which are sampled each 50 micro seconds. the funcution is executed within one sample. the initial position theta_b is set to zero only in the first sample, then it takes the final value theta_ij as an initial value for the next samples. No additional parameter is added. the last question is not clear.

Walter Roberson on 19 Sep 2022
% Inputs are reference voltage Vdr and Vqr,reference current ,Idr and Iqr
% and measured currents (ia, ib)
function theta = fcn(alpha_r, beta_r, d_est, q_est)
% initial rotor position and initial error
persistent theta_b
if isempty(theta_b); theta_b=0; end
err_in=beta_r*d_est-alpha_r*q_est;
for i=0:7
theta_delta=(pi/4)*2^-i;
for j=0:7
theta_ij=theta_b+theta_delta*(j-4);
alpha_est=d_est*cos(theta_ij)-q_est*sin(theta_ij);
beta_est=d_est*sin(theta_ij)+q_est*cos(theta_ij);
% the cost function
err=beta_r*alpha_est-alpha_r*beta_est;
if (err < err_in)
theta_b=theta_ij;
err_in=err;
end
end
end
theta=theta_b;
end
This is what you are asking for.
It is not, in practice, going to be what you want.
Suppose you run the program again a second time. You would probably expect that when you restart the program that the function would re-start with theta_b = 0. However, as far as the function is concerned, it has not been told that the program started again, so it is going to use the last theta_b value from the previous run of the code as the initial value.
What would you consider to be acceptable ways to deal with this problem?

### More Answers (1)

KSSV on 19 Sep 2022
Make theta_b also a input variable.
% Inputs are reference voltage Vdr and Vqr,reference current ,Idr and Iqr
% and measured currents (ia, ib)
function theta = fcn(theta_b,alpha_r, beta_r, d_est, q_est)
% initial rotor position and initial error
err_in=beta_r*d_est-alpha_r*q_est;
for i=0:7
theta_delta=(pi/4)*2^-i;
for j=0:7
theta_ij=theta_b+theta_delta*(j-4);
alpha_est=d_est*cos(theta_ij)-q_est*sin(theta_ij);
beta_est=d_est*sin(theta_ij)+q_est*cos(theta_ij);
% the cost function
err=beta_r*alpha_est-alpha_r*beta_est;
if (err < err_in)
theta_b=theta_ij;
err_in=err;
end
end
end
theta=theta_b;

### Categories

Find more on Spectral Measurements in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!