Error : A and B must be floating-point scalars - matlab

I am trying to integrate a function between two points but I can't do it as I get the following error:
Error using integral (line 85)
A and B must be floating-point scalars.
Error in integrantK_sup (line 6)
I=integral(F1,double(ALPHA*R_int_ind),double(ALPHA*R_ext_ind)); % équation (70), p.2834
Error in Script_A_MH_L (line 228)
integ=integrantK_sup(ALPHA);
The code used is :
nb_pt_v=200; pas_v=Ep_plaque/nb_pt_v;
nb_pt_h=200; pas_h=3*R_ext_ind/nb_pt_h;
ik=1:nb_pt_h+1;
il=1:nb_pt_v+1;
r= 0 + (ik-1).*pas_h;
z= 0 - (il-1).*pas_v; [r,z]=meshgrid(r,z);
A=zeros(nb_pt_v+1,nb_pt_h+1);
pas_alpha=10;
nb_pas_al=1500;
for ALPHA=0.1:pas_alpha:nb_pas_al
integ=integrantK_sup(ALPHA);
A=integ.*pas_alpha+A;
end
The integration function integratingK_sup is defined as follows:
function integ=integrantK_sup(ALPHA)
% équation (74), p.2835
global r z R_int_ind R_ext_ind Ep_plaque Ep_ind Airgap w mur sigma_plaque
F1=#(x)x.*besselj(1,x);
I=integral(F1,double(ALPHA*R_int_ind),double(ALPHA*R_ext_ind)); % équation (70), p.2834
Alpha_1=sqrt(ALPHA^2+1j*w*mur*sigma_plaque);
aa=I.*besselj(1,ALPHA.*r).*(exp(-ALPHA*Airgap)-exp(-ALPHA*(Airgap+Ep_ind)));
bb=(ALPHA*Alpha_1*exp(2*Alpha_1*Ep_plaque).*exp(Alpha_1.*z));
cc=ALPHA*Alpha_1.*exp(-Alpha_1.*z);
dd=((ALPHA-Alpha_1)*Alpha_1+Alpha_1*(ALPHA+Alpha_1)*exp(2*Alpha_1*Ep_plaque));
integ=((1/(ALPHA^3)).*aa).*((bb+cc)/dd);
end
To execute, you must first initialise the parameters:
Ep_plaque = 5e-3;
R_plaque = 20e-2;
R_int_ind = 5e-2;
R_ext_ind = 15e-2;
Ep_ind = 2e-3;
Airgap = 2e-3;
R_air = 5*R_plaque;
H_air = 100*Ep_plaque;
mu0=4*pi*1e-7; %% Perméabilité magnétique du vide
nu0=1/mu0; %% Réluctivité magnétique du vide
mur=1e5; %% Perméabilité magnétique relative
nur=1.0/mur; %% Perméabilité magnétique du circuit magnétique
Js=5e6; %% Densité de courant source
Is = Js *((R_ext_ind-R_int_ind)*Ep_ind); %% courant source
ADirichlet=0; %% Valeurs Dirichlet du potentiel vecteur magnétique
sigma_plaque=1e6; %% Conductivité électrique du circuit magnétique
fr=50000; %% Fréquence de travail
w = 2*pi*fr ; %% Pulsation
The problem is in the integration of the function F1 between the two points ALPHA * R_int_ind and ALPHA * R_ext_ind.

Based on our conversation in the comments, I was able to reproduce the error you saw by not making R_int_ind and R_ext_ind global at their point of initialization. Here's a simplified version of your code with this error fixed:
% Script_A_MH_L
global R_int_ind R_ext_ind % <-- Global declaration added here.
R_int_ind = 5e-2;
R_ext_ind = 15e-2;
pas_alpha=10;
nb_pas_al=1500;
I=[];
for ALPHA=0.1:pas_alpha:nb_pas_al;
I(end+1)=integrantK_sup(ALPHA);
end
% integrantK_sup
function I = integrantK_sup(ALPHA)
global R_int_ind R_ext_ind
F1=#(x)x.*besselj(1,x);
I=integral(F1,double(ALPHA*R_int_ind),double(ALPHA*R_ext_ind)); % équation (70), p.2834
end
Global variables are notorious for introducing confusion. You might be better served by packaging your constants up in a struct and passing them to the functions that need them:
% Script_A_MH_L
constants.R_int_ind = 5e-2;
constants.R_ext_ind = 15e-2;
pas_alpha=10;
nb_pas_al=1500;
I=[];
for ALPHA=0.1:pas_alpha:nb_pas_al;
I(end+1)=integrantK_sup(ALPHA,constants);
end
% integrantK_sup
function I = integrantK_sup(ALPHA,constants)
F1=#(x)x.*besselj(1,x);
I=integral(F1,double(ALPHA*constants.R_int_ind),double(ALPHA*constants.R_ext_ind)); % équation (70), p.2834
end

Related

How to optimize new test problem using PlatEMO?

I tried to follow the main tutorial of PlatEMO but I failed to compile it. I tried to modify an already existed function but I've got too many errors.
This is the code I already tried:
classdef counster < PROBLEM
%HELP COUNSTER
methods
%% Initialization
function obj = counster()
obj.Global.M = 2;
if isempty(obj.Global.D)
obj.Global.D = 2;
end
obj.Global.lower =[0,zeros(1,obj.Global.D-1)-2];
obj.Global.upper = [1,zeros(1,obj.Global.D-1)+2];
obj.Global.encoding = 'real';
end
%% Calculate objective values
function PopObj = CalObj(obj,X)
PopObj(:,1) = X(1);
PopObj(:,2) = (1 + X(2))/X(1);
end
%% Calculate constraint violations
function PopCon = CalCon(obj,X)
PopCon(:,1)=-9*X(1)-X(2)+6;
PopCon(:,2)= -9*X(1)-X(2)+1;
end
end
end
and this is an example of built-in function which is correctly working:
classdef CF4 < PROBLEM
% <problem> <CF>
% Constrained benchmark MOP
%------------------------------- Reference --------------------------------
% Q. Zhang, A. Zhou, S. Zhao, P. N. Suganthan, W. Liu, and S. Tiwari,
% Multiobjective optimization test instances for the CEC 2009 special
% session and competition, School of CS & EE, University of Essex, Working
% Report CES-487, 2009.
%------------------------------- Copyright --------------------------------
% Copyright (c) 2018-2019 BIMK Group. You are free to use the PlatEMO for
% research purposes. All publications which use this platform or any code
% in the platform should acknowledge the use of "PlatEMO" and reference "Ye
% Tian, Ran Cheng, Xingyi Zhang, and Yaochu Jin, PlatEMO: A MATLAB platform
% for evolutionary multi-objective optimization [educational forum], IEEE
% Computational Intelligence Magazine, 2017, 12(4): 73-87".
%--------------------------------------------------------------------------
methods
%% Initialization
function obj = CF4()
obj.Global.M = 2;
if isempty(obj.Global.D)
obj.Global.D = 10;
end
obj.Global.lower = [0,zeros(1,obj.Global.D-1)-2];
obj.Global.upper = [1,zeros(1,obj.Global.D-1)+2];
obj.Global.encoding = 'real';
end
%% Calculate objective values
function PopObj = CalObj(obj,X)
D = size(X,2);
J1 = 3 : 2 : D;
J2 = 2 : 2 : D;
Y = X - sin(6*pi*repmat(X(:,1),1,D)+repmat(1:D,size(X,1),1)*pi/D);
h = Y.^2;
temp = Y(:,2) < 3/2*(1-sqrt(1/2));
h(temp,2) = abs(Y(temp,2));
h(~temp,2) = 0.125 + (Y(~temp,2)-1).^2;
PopObj(:,1) = X(:,1) + sum(h(:,J1),2);
PopObj(:,2) = 1-X(:,1) + sum(h(:,J2),2);
end
%% Calculate constraint violations
function PopCon = CalCon(obj,X)
t = X(:,2) - sin(6*pi*X(:,1)+2*pi/size(X,2)) - 0.5*X(:,1) + 0.25;
PopCon = -t./(1+exp(4*abs(t)));
end
%% Sample reference points on Pareto front
function P = PF(obj,N)
P(:,1) = (0:1/(N-1):1)';
P(:,2) = 1 - P(:,1);
temp1 = 0.5<P(:,1) & P(:,1)<=0.75;
temp2 = 0.75<P(:,1);
P(temp1,2) = -0.5*P(temp1,1)+3/4;
P(temp2,2) = 1 - P(temp2,1)+0.125;
end
end
end
>> main('-algorithm',#MOPSO,'-problem',#counster,'-N',200,'-M',10);
It meant to plot a pareto front of counster problem but I have got this:
Index exceeds matrix dimensions.
Error in INDIVIDUAL (line 79)
obj(i).obj = Objs(i,:);
Error in GLOBAL/Initialization (line 151)
Population = INDIVIDUAL(obj.problem.Init(N));
Error in MOPSO (line 23)
Population = Global.Initialization();
Error in GLOBAL/Start (line 120)
obj.algorithm(obj);
Error in main (line 62)
Global.Start();

Matlab: ode15i solver raises error about dimensions, I fail to see where

CODE
% Why?
tstart = 0;
tfinal = 1;
tspan = [tstart tfinal];
options = odeset('Events',#myEventsFcn);
% Initial conditions
y0 = [0,-0.6,0,0,0,0];
yp0 = [0,0,0,0,0,0];
% decic funtion calculates consistent ICs
[y0,yp0] = decic(#StateI,t0,y0,[0 1 0 0 0 0],yp0,[0 0 0 0 0 0]);
% Arrays for plots
tout = tstart;
sout = y0(1:1);
bout = y0(2:2);
zout = y0(3:3);
svout = y0(4:4);
bvout = y0(5:5);
zvout = y0(6:6);
% ode15i solves system of implicit ODEs
[t,y,te,ye,ie] = ode15i(#StateI,tspan,y0,yp0,options);
% system of implicit ODEs defined as StateI function
function res = StateI(t,y,yp)
% Constants
mS = 3*10^(-4); % [kg]
JS = 5*10^(-9); % [kgm]
mB = 4.5*10^(-3); % [kg]
g = 9.81; % [m/s^2]
JB = 7*10^(-7); % [kgm]
lS = 10^(-2); % [m]
lG = 1.5*10^(-2); % [m]
cp = 5.6*10^(-3); % [N/rad]
res = [(mS+mB)*yp(6) + mB*lS*yp(4) + mB*lG*yp(5) + (mS+mB)*g;
mB*lS*yp(6) + (JS+mB*lS^2)*yp(4) + mB*lS*lG*yp(5) - cp*(y(2)-y(1)) + mB*lS*g;
mB*lG*yp(6) + mB*lS*lG*yp(4) + (JB+mB*lG^2)*yp(5) - cp*(y(1)-y(2)) + mB*lG*g;
y(4)-yp(1);
y(5)-yp(2);
y(6)-yp(3)];
end
% my events defined in myEventsFcn
function [value,isterminal,direction] = myEventsFcn(t,y,yp)
% Constants
mS = 3*10^(-4); % [kg]
mB = 4.5*10^(-3); % [kg]
g = 9.81; % [m/s^2]
rS = 3.1*10^(-3); % [m]
lS = 10^(-2); % [m]
r0 = 2.5*10^(-3); % [m]
hS = 2*10^(-2); % [m]
lG = 1.5*10^(-2); % [m]
lB = 2.01*10^(-2); % [m]
hB = 2*10^(-2); % [m]
cp = 5.6*10^(-3); % [N/rad]
Z2 = -(mS+mB)*g-mB*lG*yp(5);
Z1II = (cp*(y(2)+(rS-r0)/hS)-rS*Z2-mB*lS*lG*yp(5)-mB*lS*g)/hS;
value = [y(1)+(rS-r0)/hS, y(1)-(rS-r0)/hS, Z1II, y(2)-(lS+lG-lB- r0)/hB];
isterminal = [1, 1, 1, 1];
direction = [-1, 1, -1, 1];
end
ERROR MESSAGE
Matrix dimensions must agree.
Error in odezero (line 46)
indzc = find((sign(vL) ~= sign(vR)) & (direction .* (vR - vL) >= 0));
Error in ode15i (line 506)
[te,ye,ie,valt,stop] = odezero(#ntrp15i,#events_aux,events_args,valt,...
Error in silly (line 24)
[t,y,te,ye,ie] = ode15i(#StateI,tspan,y0,yp0,options);
So I ran this code before, no problem, then this error started popping up. I cannot see where this error might occur. I tried transposing my vectors y0 and yp0, which is not sucessful, but it does give another error message, which seems strange because I think Matlab's ode solvers can handle transposed initial conditions.
Best regards
You code runs just fine on my version of MATLAB (2018b). Check your version.

minimum-redundancy maximum-relevancy (MRMR) for feature selection

I am using a filter measure in feature selection called (MRMR) minimum-redundancy maximum-relevancy. After i run the code below
function testselection
addpath('C:\Users\Desktop\mRMR_0.9_compiled\mi_0.9');
FeaturesFile = dlmread('test.txt')
[x,y]=size(FeaturesFile)
Features=FeaturesFile(:,1:y-1)
classLeble=FeaturesFile(:,y)
[fea] = mrmr_mid_d(Features, classLeble, 5)
end
I got the following error
Undefined function or variable 'estpab'.
Error in mutualinfo (line 21)
[p12, p1, p2] = estpab(vec1,vec2);
Error in mrmr_mid_d (line 17)
t(i) = mutualinfo(d(:,i), f);
Error in mrmr (line 9)
[fea] = mrmr_mid_d(Features, classLeble, 5);
The code for mrmr_mid_d as below
function [fea] = mrmr_mid_d(d, f, K)
% function [fea] = mrmr_mid_d(d, f, K)
%
% MID scheme according to MRMR
%
% By Hanchuan Peng
% April 16, 2003
%
bdisp=0;
nd = size(d,2);
nc = size(d,1);
t1=cputime;
for i=1:nd,
t(i) = mutualinfo(d(:,i), f);
end;
fprintf('calculate the marginal dmi costs %5.1fs.\n', cputime-t1);
[tmp, idxs] = sort(-t);
fea_base = idxs(1:K);
fea(1) = idxs(1);
KMAX = min(1000,nd); %500
idxleft = idxs(2:KMAX);
k=1;
if bdisp==1,
fprintf('k=1 cost_time=(N/A) cur_fea=%d #left_cand=%d\n', ...
fea(k), length(idxleft));
end;
for k=2:K,
t1=cputime;
ncand = length(idxleft);
curlastfea = length(fea);
for i=1:ncand,
t_mi(i) = mutualinfo(d(:,idxleft(i)), f);
mi_array(idxleft(i),curlastfea) = getmultimi(d(:,fea(curlastfea)), d(:,idxleft(i)));
c_mi(i) = mean(mi_array(idxleft(i), :));
end;
[tmp, fea(k)] = max(t_mi(1:ncand) - c_mi(1:ncand));
tmpidx = fea(k); fea(k) = idxleft(tmpidx); idxleft(tmpidx) = [];
if bdisp==1,
fprintf('k=%d cost_time=%5.4f cur_fea=%d #left_cand=%d\n', ...
k, cputime-t1, fea(k), length(idxleft));
end;
end;
return;
%=====================================
function c = getmultimi(da, dt)
for i=1:size(da,2),
c(i) = mutualinfo(da(:,i), dt);
end;
And the code for mutualinfo as follows
function h = mutualinfo(vec1,vec2)
%=========================================================
%
%This is a prog in the MutualInfo 0.9 package written by
% Hanchuan Peng.
%
%Disclaimer: The author of program is Hanchuan Peng
% at <penghanchuan#yahoo.com> and <phc#cbmv.jhu.edu>.
%
%The CopyRight is reserved by the author.
%
%Last modification: April/19/2002
%
%========================================================
%
% h = mutualinfo(vec1,vec2)
% calculate the mutual information of two vectors
% By Hanchuan Peng, April/2002
%
[p12, p1, p2] = estpab(vec1,vec2);
h = estmutualinfo(p12,p1,p2);
Could someone know what is the problem?
If you use this Mutual information computation package, you can see, the estpab function compiled only for some platforms (there are estpab.dll for Win32 platform, estpab.mexglx the ELF32 file and estpab.mexmac for Mac).
So, you have to recompile this package for your platform.
First of all you have to configure mex building system. Call mex -setup and select appropriate compiler.
Than you can recompile the Mutual information computation package by calling makeosmex.
Note: If you get some errors during compilation like
d:\MATLAB work\mi\estcondentropy.cpp(65) : error C2668: 'log' : ambiguous call to overloaded function
you have to modify appropriate line (65 in this example) in appropriate file (estcondentropy.cpp in this example) from the muInf /= log(2); to muInf /= log(2.0);
In the path C:\Users\Desktop\ isn't the user missing?
Usually windows paths are something like C:\Users\Username\Desktop\
You are probably adding the wrong path, so the program cannot find the function estpab at all.

Out of memory when restart a code

I made a matlab code including some symbolic functions
First I run this code, it gives me a result.
But trying to run the same code again, Out of memory message comes up.
Does anybody have an idea to solve this problem?
Below is my code.
clc;
clear;
tic;
rng('shuffle');
p=2; % AR order
yr=normrnd(0,1,1000,1);
T=max(size(yr));
for k=25:35;
intnum=k/10;
fore=100;
int=((max(yr)-min(yr))/intnum);
fin=T-fore;
% Initial setting matrix
y_T1=zeros(fore,1);
y_fore_w=zeros(fore,1); y_diff_w=zeros(fore,1);
y_fore_o=zeros(fore,1);y_diff_o=zeros(fore,1);
y_fore_f=zeros(fore,1);y_diff_f=zeros(fore,1);
y_fore_wl=zeros(fore,1);y_diff_wl=zeros(fore,1);
y_fore_m=zeros(fore,1);y_diff_m=zeros(fore,1);
y_gap=zeros(fore,1);
for e=fin:T-1 ;
y_1=yr(p+1:e);x_1=yr(p:e-1);x_2=yr(p-1:e-2);
A=[y_1 x_1 x_2];
st=1;
ye=yr(st:e);
coordinate = zeros(length(y_1), 3); % Giving new indices to A
y_1_diff=max(y_1)-min(y_1);x_1_diff=max(x_1)-min(x_1);x_2_diff=max(x_2)-min(x_2);
y_1_int=y_1_diff/int;x_1_int=x_1_diff/int;x_2_int=x_2_diff/int;
y_1_int_num=round(y_1_int);x_1_int_num=round(x_1_int);x_2_int_num=round(x_2_int);
for i=1:y_1_int_num;
coordinate(A(:,1) >= (i-1)*int+min(A(:,1)) & A(:,1) < i*int+min(A(:,1)), 1) = i ; % Terrific idea for some conditions in ROW
end
for i=1:x_1_int_num;
coordinate(A(:,2) >= (i-1)*int+min(A(:,2)) & A(:,2) < i*int+min(A(:,2)), 2) = i ; % Terrific idea for some conditions in ROW
end
for i=1:x_2_int_num;
coordinate(A(:,3) >= (i-1)*int+min(A(:,3)) & A(:,3) < i*int+min(A(:,3)), 3) = i; % Terrific idea for some conditions in ROW
end
sz=[y_1_int_num x_1_int_num x_2_int_num];
nucu=max(sz)^(p+1);
y_int_sub_num=zeros(nucu,1); % Number of frequency
yfunc=cell(nucu,1); % Cell of functions
c=1:max(sz);
C=combvec(c,c,c)'; % Generate a reference matrix to coordinate
d=sortrows(C);
syms c b1 b2;
for i=1:nucu;
a=ismember(coordinate, d(i,:), 'rows');
s=sum(a);
y_int_sub_num(i)=s;
B=A(a,:);
func=symfun((B(:,1)-c-b1*B(:,2)-b2*B(:,3)).^2, [c,b1,b2]);
yfunc1=sum(func);
yfunc{i}=yfunc1*y_int_sub_num(i);
clear func; clear yfunc1;
end
yfuncs=symfun(sym('f(c,b1,b2)'),[c,b1,b2]);
for j=1:nucu;
yfuncs=yfuncs+yfunc{j};
end
yfuncs= yfuncs-'f(c,b1,b2)';
y_trans = matlabFunction(yfuncs,'Vars',{[c;b1;b2]});
% WLLS
options = optimset('Algorithm','Interior-point',...
'TolFun',1e-8, 'MaxFunEvals',1000000,'MaxIter',1000000); % Set option
[b_w,fval] = fminsearch(y_trans, [0;0;0], options);
% OLS
c=ones(T-p,1);y_c=zeros(T-p,4);
y_c(:,1)=yr(p+1:T);y_c(:,2)=c;y_c(:,3)=yr(2:end-(p-1));y_c(:,4)=yr(1:end-2);
[b_0,bint_0,e_0,rint,stats] = regress(y_c(:,1), y_c(:,2:4));
% WLS
e_0_w=e_0.^2/sum(e_0.^2); %normalization required for Matlab
[bw,se_bw] = lscov(y_c(:,1), y_c(:,2:4), e_0_w);
% FGLS_iterated
[bf, Sigma] = lscov(y_c(:,1), y_c(:,2:4), e_0_w);
% [bf, Sigma]=fglsi(y_c(:,1), y_c(:,3), y_c(:,4), y_c(:,3));
% MLE
[b_m, b_mm, mu, sig] = regress(y_c(:,1), y_c(:,2:4)); % Since error, OL
% Foracast and Differences
n=e-(fin-1);
y_T1(n)=yr(e+1);
y_fore_w(n)=b_w(1)+b_w(2)*ye(end)+b_w(3)*ye(end-1);y_diff_w(n)=y_T1(n)-y_fore_w(n);
y_fore_o(n)=b_0(1)+b_0(2)*ye(end)+b_0(3)*ye(end-1);y_diff_o(n)=y_T1(n)-y_fore_o(n);
y_fore_wl(n)=bw(1)+bw(2)*ye(end)+bw(3)*ye(end-1);;y_diff_wl(n)=y_T1(n)-y_fore_wl(n);
y_fore_f(n)=bf(1)+bf(2)*ye(end)+bf(3)*ye(end-1);;y_diff_f(n)=y_T1(n)-y_fore_f(n);
y_fore_m(n)=b_m(1)+b_m(2)*ye(end)+b_m(3)*ye(end-1);;y_diff_m(n)=y_T1(n)-y_fore_m(n);
y_gap(n)=abs(y_diff_w(n))-abs(y_diff_o(n));ygap(n)=(y_gap(n)<=0);
end
toc
a=1;filename = [ 'comp', num2str(a), '.mat' ];
save(filename);
if mean(y_diff_w.^2) <=mean(y_diff_o.^2)
[int mean(y_diff_w.^2)]
beep;
end
z1=zeros(y_1_int+1,1);
for i=1:y_1_int+1;
z=(yr>=(i-1)*int+min(yr) & yr<i*int+min(yr));
z1(i)=sum(z);
end
z1
end
sort(yr)
[max(yr) min(yr)]

error function aborted matlab

Hi i am running a model in simulink from matlab and i am taking this error
Error using run (line 28) Call to MATLAB function aborted: The first
input argument of the "mpc" command must be one of the following: an
LTI object, an IDMODEL object or a structure of models and offsets.
Block Subsystem2/Control plant (#38) While executing: State During
Action
The embedeed block that i take the erro contains the code
function w0u = MPC(p,x,Sb_bus,Sb,vt,q,theta0,ku,xs,u,vf,KvP,KvI,Kp,Ki,Kd,Droop,DroopAvr,v0,wb,r,t0,umaxnow,time,samplingsTime,Rn,Qn,Rw,Qw,ud,uMinMPC,uMaxMPC,udMaxMPC,wmin,wminw,wmax,wmaxw,tFLR,aFLR,genset3connected,debuggingfile,mpcOn,mpcValSaveTime,mpcValFileName,useUMaxNow,wNLstart)
coder.extrinsic('mpc');
w0u = zeros(2,1);
[w0u] = mpc(p,x,Sb_bus,Sb,vt,q,theta0,ku,xs,u,vf,KvP,KvI,Kp,Ki,Kd,Droop,DroopAvr,v0,wb,r,t0,umaxnow,time,samplingsTime,Rn,Qn,Rw,Qw,ud,uMinMPC,uMaxMPC,udMaxMPC,wmin,wminw,wmax,wmaxw,tFLR,aFLR,genset3connected,debuggingfile,mpcOn,mpcValSaveTime,mpcValFileName,useUMaxNow,wNLstart);
end
And the function mpc is
function [w0u] = mpc(p,Sb_bus,Sb,vt,q,x,theta0,ku,xs,u,umaxnow,vf,KvP,KvI,Kp,Ki,Kd,Droop,DroopAvr,v0,wb,r,t0,time,samplingsTime,Rn,Qn,Rw,Qw,ud,uMinMPC,uMaxMPC,udMaxMPC,wmin,wminw,wmax,wmaxw,tFLR,aFLR,genset3connected,debuggingfile,mpcOn,mpcValSaveTime,mpcValFileName,useUMaxNow,wNLstart)
persistent tupdated w0set
eml.extrinsic('saveVariables');
H = [10; 7];
D = [.02;.025];
if (isempty(tupdated))
% Initialize variables
tupdated = time;
w0set = wNLstart;
elseif (time < t0)
% Wait until t0
elseif (mpcOn == 0)
% If turned off, do nothing.
elseif (time >= tupdated + samplingsTime)
% Update w0
Pbus = sum(p.*Sb)/sum(Sb);
Qbus = sum(q.*Sb)/sum(Sb);
pFLR = aFLR*Sb(end)/Sb_bus*Pbus;
X = [Pbus;Qbus;x];
u0 = u;
udw = ud(1);
%% Calculate the state space equations
[A B B0 Cu Du ~, ud] = makeLinearSystem(H,D,p,Sb_bus,Sb,q,X,vf,vt,theta0,ku,xs,u0,wb,r,KvP,KvI,Kp,Ki,Kd,Droop,DroopAvr,w0set,v0,samplingsTime,ud);
[Aw Bw B0w Cuw Duw ~, udw] = makeLinearWorstCaseSystem(H,D,p,Sb_bus,Sb,q,X,vf,vt,theta0,ku,xs,u0,wb,r,KvP,KvI,Kp,Ki,Kd,Droop,DroopAvr,w0set,v0,samplingsTime,genset3connected,pFLR,udw);
%% Discretize
if(genset3connected)
% Discretize system for normal case
[Ad Bd B0d] = makediscrete(A,B,B0,samplingsTime);
% Discretize system after FLR reacts
[AdFLRp, ~, ~] = makediscrete(Aw,Bw,B0w,samplingsTime-tFLR);
else
% Sets Ad, Bd, B0d and AdFLRp to remove compilation error (matrices
% must have the same size for all cases)
Ad = A;
Bd = B;
B0d = B0;
AdFLRp = Aw;
end
% Discretize system for failure case
[Adw Bdw B0dw] = makediscrete(Aw,Bw,B0w,samplingsTime);
%% Find input optimal
[dw0opt PHI PSI dw] = findInput(X,Ad,Bd,B0d,Cu,Du,Adw,Bdw,B0dw,Cuw,Duw,AdFLRp,pFLR,Rn,Qn,Rw,Qw,ud,udw,u,umaxnow,uMinMPC,uMaxMPC,udMaxMPC,wmin,wmax,wminw,wmaxw,p,Sb,genset3connected,time,debuggingfile,useUMaxNow);
%% Save internal values for plotting
if(mpcValSaveTime >= time && mpcValSaveTime< time + samplingsTime)
saveVariables(mpcValFileName,PHI,PSI,dw)
end
w0set = w0set + dw0opt;
tupdated = time;
end
w0u =w0set;
end
what i have to do?