Use a command only once
이전 댓글 표시
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
Walter Roberson
2022년 9월 19일
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
2022년 9월 19일
채택된 답변
추가 답변 (1개)
KSSV
2022년 9월 19일
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;
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!