output argument not assigned during call

i get this error while calling a function i defined having a defined output argument. this is the function:
function J=agc(ki)
global A B C
Tp1=20;
Tp2=20;
Kp1=120;
Kp2=120;
T12=0.0866
Tch1=0.3;
Tch2=0.3;
Tg1=0.08;
Tg2=0.08;
R1=2.4;
R2=2.4;
a12=-1;
apf1=0.5;
apf2=0.5;
D1=1/Kp1;
D2=1/Kp2;
B1=D1+(1/R1);
B2=D2+(1/R2);
A=[(-1/Tp1) 0 (-Kp1/Tp1) (Kp1/Tp1) 0 0 0;
0 (-1/Tp2) (-a12*Kp2/Tp2) 0 (Kp2/Tp2) 0 0;
(2*pi*T12) (-2*pi*T12) 0 0 0 0 0;
0 0 0 (-1/Tch1) 0 (1/Tch1) 0;
0 0 0 0 (-1/Tch2) 0 (1/Tch2);
(-1/(R1*Tg1)) 0 0 0 0 (-1/Tg1) 0;
0 (-1/(Tg2*R2)) 0 0 0 0 (-1/Tg2)];
B=[0 0;0 0;0 0;0 0;0 0;(apf1/Tg1) 0;0 (apf2/Tg2)];
C=[(-Kp1/Tp1) 0;0 (-Kp2/Tp2);0 0;0 0;0 0;0 0;0 0];
u1=0;
u2=0;
u=[u1;u2];
g1=0.01;
g2=0;
g=[g1;g2];
dt=0.01;
x=[0;0;0;0;0;0;0];
x0=(x(1,:));
t(1)=0;
k=1;
tw=k*dt;
function xdot=xnew(t,x,flag,g,u)
global A B C
xdot=A*x+B*u+C*g;
while tw~=80
tw=k*dt;
ts=t(k);
tf=ts+dt;
tspan=[ts tf];
[ts,xs]=ode45('xnew',tspan,x0,[],g,u);
n=size(xs,1);
k=k+1;
t(k,1)=ts(n);
x(k,:)=xs(n,:);
x0=(x(k,:));
x1=x(k,1);
x2=x(k,2);
x3=x(k,3);
ACE1=(B1*x1)+x3;
ACE2=(B2*x2)+(a12*x3);
u1=u1-Ki(1)*dt*ACE1;
u2=u2-Ki(2)*dt*ACE2;
u=[u1;u2];
g=[g1;g2];
J=((x1'*x1)+(x2'*x2)+(x3'*x3))*dt
clear ts xs
end

Answers (1)

Stephen23
Stephen23 on 23 Aug 2018
Moved: Sabin on 1 Feb 2023
@Akanksha Verma: the variable J is not defined anywhere inside the function agc, although you do define J inside the function xnew. However it is not clear if you intend xnew to be a local function or a nested function, but given that J is not an output it implies that you want this as a nested function (in which case you need to define J inside the agc workspace too). This is just a guess though.
Note that you call xnew inside xnew because you specify it as the ode45 function argument which itself is called within xnew, which means that your code is recursive. It is unlikely that this is what you want, but as your code has no comments or explanation it is not clear what you are trying to achieve. You should use a function handle for defining the first input to ode45.
It is better to avoid global variables. Just parameterize the function handle or use nested functions:

Categories

Find more on Configure Simulation Conditions in Help Center and File Exchange

Asked:

on 23 Aug 2018

Moved:

on 1 Feb 2023

Community Treasure Hunt

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

Start Hunting!