# I'm doing predictive torque control in simulink, while running I'm getting error as variable x_opt is not fully defined on some execution paths. How to correct the program? Can any one send the code for it?

3 views (last 30 days)
ABHIJITH M on 19 Jun 2020
Answered: Gautam on 20 Jun 2020
Function[sa, sb, sc, Te] =pred(sref, Tref, isa, isb, rfa, rfb, wr) states=[0, 0,0,-1,-1,-1,0,1,1,1,0,-1,-1,-1,-1,-1,0,1,1; 0,-1,0,0,0,-1,-1,-1,0,1,1,1,1,1,0,-1,-1,-1,-1; 0,-1,-1,-1,0,0,0,-1,-1,-1,-1,-1,0,1,1,1,1,1,0];
Ts=25e-6; Rs=1.405; Rr=1.395; Ls=0.005839; Lr=0.005839; Lm=0.1722; f=50; Vdc=400; xs=2*pi*f*Ls; xr=2*pi*f*Lr; xm=2*pi*f*Lm; D=(xs*xr)-(xm*xm); ts=(xr*D/((Rs*xr^2)+(Rr*xm^2)); tr=xr/Rr; I=[1,0;0,1]; F=(-1/ts)*I; G1=(xm/D)*[1/tr,wr;-wr,1/tr); K=[1,-1/2,-1/2;0,sqrt(3)/2,-sqrt(3)/2; G2=(Vdc/3)*(xr/D)*K; is=[isa;isb]; rotf=[rfa;rfb]; A=F*Ts B1=G1*Ts; B2=G2*Ts; gopt=10e3; for(i=1:19) v=[states(1,i);states(2,i);states(3,i)]; ispread=((A*is) +(B1*rotf) +(B2*v)) ; sf=(B2*(D/xr) *v) - ((Rs*I*Ts) *rotf) ; sfm=sqrt((sf(1)^2)+sf(2)^2); Te=abs(sf(1)*ispread(2)-sf(2)*spread(1)); g=((Te-Tref)^2)+(sref-sfm)^2; if(g<gopt) gopt=g; x_opt=i; end end %output switching states sa=states(1,x_opt); sb=states(2,x_opt); sc=states(3,x_opt);

Gautam on 20 Jun 2020
The error would indicate that when you create your function and if you have implemented if-elses or multiple ifs and in one of those ifs, the output variable has not been defined.
In your code, you have written:
if(g<gopt) gopt=g; x_opt=i; end end
If this condition was not true, then what should be the value of x_opt for the next few lines of code? Fix this and the error should be resolved(either by inserting an else branch or another if branch for g>=gopt)
Going forward, please use proper formatting while pasting code here as the community might find it hard to parse through the provided code.

### Categories

Find more on Simulink Environment Customization in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!