kindly check code i am unable to get actual and estimated it is system identification

clc
clear
close all
%% ==========================================
% READ HEATING SYSTEM DATASET
%% =========================================
data = readmatrix('exchanger.xlsx');
M = 3863;
g = data(1:M,1);
x = data(1:M,2);
%% ==========================================
% PSO PARAMETERS
%% ==========================================
dim = 8;
nParticles = 100;
maxIter = 500;
lb = [-2.0 0.3 0.0 -0.4 -40 30 -30 55];
ub = [-1.0 0.9 0.4 -0.1 -25 50 -20 75];
%% ==========================================
% INITIALIZATION
%% ==========================================
particle = struct;
for i = 1:nParticles
particle(i).position = lb + rand(1,dim).*(ub-lb);
particle(i).velocity = zeros(1,dim);
particle(i).cost = fitness_functionh( ...
particle(i).position,x,g,M);
particle(i).best.position = particle(i).position;
particle(i).best.cost = particle(i).cost;
end
costs = [particle.cost];
[~,idx] = min(costs);
global_best = particle(idx).best;
%% ==========================================
% PSO Constants
%% ==========================================
w = 0.6;
c1 = 1.5;
c2 = 1.5;
BestCost = zeros(maxIter,1);
theta_history = zeros(maxIter,dim);
mse_history = zeros(maxIter,1);
%% ==========================================
% PSO MAIN LOOP
%% ==========================================
for iter = 1:maxIter
for i = 1:nParticles
particle(i).velocity = ...
w*particle(i).velocity ...
+ c1*rand*(particle(i).best.position-particle(i).position) ...
+ c2*rand*(global_best.position-particle(i).position);
particle(i).position = ...
particle(i).position + particle(i).velocity;
particle(i).position = max(particle(i).position,lb);
particle(i).position = min(particle(i).position,ub);
particle(i).cost = fitness_functionh( ...
particle(i).position,x,g,M);
if particle(i).cost < particle(i).best.cost
particle(i).best.position = particle(i).position;
particle(i).best.cost = particle(i).cost;
end
if particle(i).best.cost < global_best.cost
global_best = particle(i).best;
end
end
theta_history(iter,:) = global_best.position;
mse_history(iter) = global_best.cost;
BestCost(iter) = global_best.cost;
fprintf('Iteration %d Best Cost = %e\n', ...
iter,global_best.cost);
end
Iteration 1 Best Cost = 9.556211e+03 Iteration 2 Best Cost = 9.395876e+03 Iteration 3 Best Cost = 9.395876e+03 Iteration 4 Best Cost = 9.395876e+03 Iteration 5 Best Cost = 9.395876e+03 Iteration 6 Best Cost = 9.395876e+03 Iteration 7 Best Cost = 9.395876e+03 Iteration 8 Best Cost = 9.395876e+03 Iteration 9 Best Cost = 9.395876e+03 Iteration 10 Best Cost = 9.395876e+03 Iteration 11 Best Cost = 9.395876e+03 Iteration 12 Best Cost = 9.395876e+03 Iteration 13 Best Cost = 9.395876e+03 Iteration 14 Best Cost = 9.395876e+03 Iteration 15 Best Cost = 9.395876e+03 Iteration 16 Best Cost = 9.395876e+03 Iteration 17 Best Cost = 9.395876e+03 Iteration 18 Best Cost = 9.395876e+03 Iteration 19 Best Cost = 9.395876e+03 Iteration 20 Best Cost = 9.395876e+03 Iteration 21 Best Cost = 9.395876e+03 Iteration 22 Best Cost = 9.395876e+03 Iteration 23 Best Cost = 9.395876e+03 Iteration 24 Best Cost = 9.395876e+03 Iteration 25 Best Cost = 9.395876e+03 Iteration 26 Best Cost = 9.395876e+03 Iteration 27 Best Cost = 9.395876e+03 Iteration 28 Best Cost = 9.395876e+03 Iteration 29 Best Cost = 9.395876e+03 Iteration 30 Best Cost = 9.395876e+03 Iteration 31 Best Cost = 9.395876e+03 Iteration 32 Best Cost = 9.395876e+03 Iteration 33 Best Cost = 9.395876e+03 Iteration 34 Best Cost = 9.395876e+03 Iteration 35 Best Cost = 9.395876e+03 Iteration 36 Best Cost = 9.395876e+03 Iteration 37 Best Cost = 9.395876e+03 Iteration 38 Best Cost = 9.395876e+03 Iteration 39 Best Cost = 9.395876e+03 Iteration 40 Best Cost = 9.395876e+03 Iteration 41 Best Cost = 9.395876e+03 Iteration 42 Best Cost = 9.395876e+03 Iteration 43 Best Cost = 9.395876e+03 Iteration 44 Best Cost = 9.395876e+03 Iteration 45 Best Cost = 9.395876e+03 Iteration 46 Best Cost = 9.395876e+03 Iteration 47 Best Cost = 9.395876e+03 Iteration 48 Best Cost = 9.395876e+03 Iteration 49 Best Cost = 9.395876e+03 Iteration 50 Best Cost = 9.395876e+03 Iteration 51 Best Cost = 9.395876e+03 Iteration 52 Best Cost = 9.395876e+03 Iteration 53 Best Cost = 9.395876e+03 Iteration 54 Best Cost = 9.395876e+03 Iteration 55 Best Cost = 9.395876e+03 Iteration 56 Best Cost = 9.395876e+03 Iteration 57 Best Cost = 9.395876e+03 Iteration 58 Best Cost = 9.395876e+03 Iteration 59 Best Cost = 9.395876e+03 Iteration 60 Best Cost = 9.395876e+03 Iteration 61 Best Cost = 9.395876e+03 Iteration 62 Best Cost = 9.395876e+03 Iteration 63 Best Cost = 9.395876e+03 Iteration 64 Best Cost = 9.395876e+03 Iteration 65 Best Cost = 9.395876e+03 Iteration 66 Best Cost = 9.395876e+03 Iteration 67 Best Cost = 9.395876e+03 Iteration 68 Best Cost = 9.395876e+03 Iteration 69 Best Cost = 9.395876e+03 Iteration 70 Best Cost = 9.395876e+03 Iteration 71 Best Cost = 9.395876e+03 Iteration 72 Best Cost = 9.395876e+03 Iteration 73 Best Cost = 9.395876e+03 Iteration 74 Best Cost = 9.395876e+03 Iteration 75 Best Cost = 9.395876e+03 Iteration 76 Best Cost = 9.395876e+03 Iteration 77 Best Cost = 9.395876e+03 Iteration 78 Best Cost = 9.395876e+03 Iteration 79 Best Cost = 9.395876e+03 Iteration 80 Best Cost = 9.395876e+03 Iteration 81 Best Cost = 9.395876e+03 Iteration 82 Best Cost = 9.395876e+03 Iteration 83 Best Cost = 9.395876e+03 Iteration 84 Best Cost = 9.395876e+03 Iteration 85 Best Cost = 9.395876e+03 Iteration 86 Best Cost = 9.395876e+03 Iteration 87 Best Cost = 9.395876e+03 Iteration 88 Best Cost = 9.395876e+03 Iteration 89 Best Cost = 9.395876e+03 Iteration 90 Best Cost = 9.395876e+03 Iteration 91 Best Cost = 9.395876e+03 Iteration 92 Best Cost = 9.395876e+03 Iteration 93 Best Cost = 9.395876e+03 Iteration 94 Best Cost = 9.395876e+03 Iteration 95 Best Cost = 9.395876e+03 Iteration 96 Best Cost = 9.395876e+03 Iteration 97 Best Cost = 9.395876e+03 Iteration 98 Best Cost = 9.395876e+03 Iteration 99 Best Cost = 9.395876e+03 Iteration 100 Best Cost = 9.395876e+03 Iteration 101 Best Cost = 9.395876e+03 Iteration 102 Best Cost = 9.395876e+03 Iteration 103 Best Cost = 9.395876e+03 Iteration 104 Best Cost = 9.395876e+03 Iteration 105 Best Cost = 9.395876e+03 Iteration 106 Best Cost = 9.395876e+03 Iteration 107 Best Cost = 9.395876e+03 Iteration 108 Best Cost = 9.395876e+03 Iteration 109 Best Cost = 9.395876e+03 Iteration 110 Best Cost = 9.395876e+03 Iteration 111 Best Cost = 9.395876e+03 Iteration 112 Best Cost = 9.395876e+03 Iteration 113 Best Cost = 9.395876e+03 Iteration 114 Best Cost = 9.395876e+03 Iteration 115 Best Cost = 9.395876e+03 Iteration 116 Best Cost = 9.395876e+03 Iteration 117 Best Cost = 9.395876e+03 Iteration 118 Best Cost = 9.395876e+03 Iteration 119 Best Cost = 9.395876e+03 Iteration 120 Best Cost = 9.395876e+03 Iteration 121 Best Cost = 9.395876e+03 Iteration 122 Best Cost = 9.395876e+03 Iteration 123 Best Cost = 9.395876e+03 Iteration 124 Best Cost = 9.395876e+03 Iteration 125 Best Cost = 9.395876e+03 Iteration 126 Best Cost = 9.395876e+03 Iteration 127 Best Cost = 9.395876e+03 Iteration 128 Best Cost = 9.395876e+03 Iteration 129 Best Cost = 9.395876e+03 Iteration 130 Best Cost = 9.395876e+03 Iteration 131 Best Cost = 9.395876e+03 Iteration 132 Best Cost = 9.395876e+03 Iteration 133 Best Cost = 9.395876e+03 Iteration 134 Best Cost = 9.395876e+03 Iteration 135 Best Cost = 9.395876e+03 Iteration 136 Best Cost = 9.395876e+03 Iteration 137 Best Cost = 9.395876e+03 Iteration 138 Best Cost = 9.395876e+03 Iteration 139 Best Cost = 9.395876e+03 Iteration 140 Best Cost = 9.395876e+03 Iteration 141 Best Cost = 9.395876e+03 Iteration 142 Best Cost = 9.395876e+03 Iteration 143 Best Cost = 9.395876e+03 Iteration 144 Best Cost = 9.395876e+03 Iteration 145 Best Cost = 9.395876e+03 Iteration 146 Best Cost = 9.395876e+03 Iteration 147 Best Cost = 9.395876e+03 Iteration 148 Best Cost = 9.395876e+03 Iteration 149 Best Cost = 9.395876e+03 Iteration 150 Best Cost = 9.395876e+03 Iteration 151 Best Cost = 9.395876e+03 Iteration 152 Best Cost = 9.395876e+03 Iteration 153 Best Cost = 9.395876e+03 Iteration 154 Best Cost = 9.395876e+03 Iteration 155 Best Cost = 9.395876e+03 Iteration 156 Best Cost = 9.395876e+03 Iteration 157 Best Cost = 9.395876e+03 Iteration 158 Best Cost = 9.395876e+03 Iteration 159 Best Cost = 9.395876e+03 Iteration 160 Best Cost = 9.395876e+03 Iteration 161 Best Cost = 9.395876e+03 Iteration 162 Best Cost = 9.395876e+03 Iteration 163 Best Cost = 9.395876e+03 Iteration 164 Best Cost = 9.395876e+03 Iteration 165 Best Cost = 9.395876e+03 Iteration 166 Best Cost = 9.395876e+03 Iteration 167 Best Cost = 9.395876e+03 Iteration 168 Best Cost = 9.395876e+03 Iteration 169 Best Cost = 9.395876e+03 Iteration 170 Best Cost = 9.395876e+03 Iteration 171 Best Cost = 9.395876e+03 Iteration 172 Best Cost = 9.395876e+03 Iteration 173 Best Cost = 9.395876e+03 Iteration 174 Best Cost = 9.395876e+03 Iteration 175 Best Cost = 9.395876e+03 Iteration 176 Best Cost = 9.395876e+03 Iteration 177 Best Cost = 9.395876e+03 Iteration 178 Best Cost = 9.395876e+03 Iteration 179 Best Cost = 9.395876e+03 Iteration 180 Best Cost = 9.395876e+03 Iteration 181 Best Cost = 9.395876e+03 Iteration 182 Best Cost = 9.395876e+03 Iteration 183 Best Cost = 9.395876e+03 Iteration 184 Best Cost = 9.395876e+03 Iteration 185 Best Cost = 9.395876e+03 Iteration 186 Best Cost = 9.395876e+03 Iteration 187 Best Cost = 9.395876e+03 Iteration 188 Best Cost = 9.395876e+03 Iteration 189 Best Cost = 9.395876e+03 Iteration 190 Best Cost = 9.395876e+03 Iteration 191 Best Cost = 9.395876e+03 Iteration 192 Best Cost = 9.395876e+03 Iteration 193 Best Cost = 9.395876e+03 Iteration 194 Best Cost = 9.395876e+03 Iteration 195 Best Cost = 9.395876e+03 Iteration 196 Best Cost = 9.395876e+03 Iteration 197 Best Cost = 9.395876e+03 Iteration 198 Best Cost = 9.395876e+03 Iteration 199 Best Cost = 9.395876e+03 Iteration 200 Best Cost = 9.395876e+03 Iteration 201 Best Cost = 9.395876e+03 Iteration 202 Best Cost = 9.395876e+03 Iteration 203 Best Cost = 9.395876e+03 Iteration 204 Best Cost = 9.395876e+03 Iteration 205 Best Cost = 9.395876e+03 Iteration 206 Best Cost = 9.395876e+03 Iteration 207 Best Cost = 9.395876e+03 Iteration 208 Best Cost = 9.395876e+03 Iteration 209 Best Cost = 9.395876e+03 Iteration 210 Best Cost = 9.395876e+03 Iteration 211 Best Cost = 9.395876e+03 Iteration 212 Best Cost = 9.395876e+03 Iteration 213 Best Cost = 9.395876e+03 Iteration 214 Best Cost = 9.395876e+03 Iteration 215 Best Cost = 9.395876e+03 Iteration 216 Best Cost = 9.395876e+03 Iteration 217 Best Cost = 9.395876e+03 Iteration 218 Best Cost = 9.395876e+03 Iteration 219 Best Cost = 9.395876e+03 Iteration 220 Best Cost = 9.395876e+03 Iteration 221 Best Cost = 9.395876e+03 Iteration 222 Best Cost = 9.395876e+03 Iteration 223 Best Cost = 9.395876e+03 Iteration 224 Best Cost = 9.395876e+03 Iteration 225 Best Cost = 9.395876e+03 Iteration 226 Best Cost = 9.395876e+03 Iteration 227 Best Cost = 9.395876e+03 Iteration 228 Best Cost = 9.395876e+03 Iteration 229 Best Cost = 9.395876e+03 Iteration 230 Best Cost = 9.395876e+03 Iteration 231 Best Cost = 9.395876e+03 Iteration 232 Best Cost = 9.395876e+03 Iteration 233 Best Cost = 9.395876e+03 Iteration 234 Best Cost = 9.395876e+03 Iteration 235 Best Cost = 9.395876e+03 Iteration 236 Best Cost = 9.395876e+03 Iteration 237 Best Cost = 9.395876e+03 Iteration 238 Best Cost = 9.395876e+03 Iteration 239 Best Cost = 9.395876e+03 Iteration 240 Best Cost = 9.395876e+03 Iteration 241 Best Cost = 9.395876e+03 Iteration 242 Best Cost = 9.395876e+03 Iteration 243 Best Cost = 9.395876e+03 Iteration 244 Best Cost = 9.395876e+03 Iteration 245 Best Cost = 9.395876e+03 Iteration 246 Best Cost = 9.395876e+03 Iteration 247 Best Cost = 9.395876e+03 Iteration 248 Best Cost = 9.395876e+03 Iteration 249 Best Cost = 9.395876e+03 Iteration 250 Best Cost = 9.395876e+03 Iteration 251 Best Cost = 9.395876e+03 Iteration 252 Best Cost = 9.395876e+03 Iteration 253 Best Cost = 9.395876e+03 Iteration 254 Best Cost = 9.395876e+03 Iteration 255 Best Cost = 9.395876e+03 Iteration 256 Best Cost = 9.395876e+03 Iteration 257 Best Cost = 9.395876e+03 Iteration 258 Best Cost = 9.395876e+03 Iteration 259 Best Cost = 9.395876e+03 Iteration 260 Best Cost = 9.395876e+03 Iteration 261 Best Cost = 9.395876e+03 Iteration 262 Best Cost = 9.395876e+03 Iteration 263 Best Cost = 9.395876e+03 Iteration 264 Best Cost = 9.395876e+03 Iteration 265 Best Cost = 9.395876e+03 Iteration 266 Best Cost = 9.395876e+03 Iteration 267 Best Cost = 9.395876e+03 Iteration 268 Best Cost = 9.395876e+03 Iteration 269 Best Cost = 9.395876e+03 Iteration 270 Best Cost = 9.395876e+03 Iteration 271 Best Cost = 9.395876e+03 Iteration 272 Best Cost = 9.395876e+03 Iteration 273 Best Cost = 9.395876e+03 Iteration 274 Best Cost = 9.395876e+03 Iteration 275 Best Cost = 9.395876e+03 Iteration 276 Best Cost = 9.395876e+03 Iteration 277 Best Cost = 9.395876e+03 Iteration 278 Best Cost = 9.395876e+03 Iteration 279 Best Cost = 9.395876e+03 Iteration 280 Best Cost = 9.395876e+03 Iteration 281 Best Cost = 9.395876e+03 Iteration 282 Best Cost = 9.395876e+03 Iteration 283 Best Cost = 9.395876e+03 Iteration 284 Best Cost = 9.395876e+03 Iteration 285 Best Cost = 9.395876e+03 Iteration 286 Best Cost = 9.395876e+03 Iteration 287 Best Cost = 9.395876e+03 Iteration 288 Best Cost = 9.395876e+03 Iteration 289 Best Cost = 9.395876e+03 Iteration 290 Best Cost = 9.395876e+03 Iteration 291 Best Cost = 9.395876e+03 Iteration 292 Best Cost = 9.395876e+03 Iteration 293 Best Cost = 9.395876e+03 Iteration 294 Best Cost = 9.395876e+03 Iteration 295 Best Cost = 9.395876e+03 Iteration 296 Best Cost = 9.395876e+03 Iteration 297 Best Cost = 9.395876e+03 Iteration 298 Best Cost = 9.395876e+03 Iteration 299 Best Cost = 9.395876e+03 Iteration 300 Best Cost = 9.395876e+03 Iteration 301 Best Cost = 9.395876e+03 Iteration 302 Best Cost = 9.395876e+03 Iteration 303 Best Cost = 9.395876e+03 Iteration 304 Best Cost = 9.395876e+03 Iteration 305 Best Cost = 9.395876e+03 Iteration 306 Best Cost = 9.395876e+03 Iteration 307 Best Cost = 9.395876e+03 Iteration 308 Best Cost = 9.395876e+03 Iteration 309 Best Cost = 9.395876e+03 Iteration 310 Best Cost = 9.395876e+03 Iteration 311 Best Cost = 9.395876e+03 Iteration 312 Best Cost = 9.395876e+03 Iteration 313 Best Cost = 9.395876e+03 Iteration 314 Best Cost = 9.395876e+03 Iteration 315 Best Cost = 9.395876e+03 Iteration 316 Best Cost = 9.395876e+03 Iteration 317 Best Cost = 9.395876e+03 Iteration 318 Best Cost = 9.395876e+03 Iteration 319 Best Cost = 9.395876e+03 Iteration 320 Best Cost = 9.395876e+03 Iteration 321 Best Cost = 9.395876e+03 Iteration 322 Best Cost = 9.395876e+03 Iteration 323 Best Cost = 9.395876e+03 Iteration 324 Best Cost = 9.395876e+03 Iteration 325 Best Cost = 9.395876e+03 Iteration 326 Best Cost = 9.395876e+03 Iteration 327 Best Cost = 9.395876e+03 Iteration 328 Best Cost = 9.395876e+03 Iteration 329 Best Cost = 9.395876e+03 Iteration 330 Best Cost = 9.395876e+03 Iteration 331 Best Cost = 9.395876e+03 Iteration 332 Best Cost = 9.395876e+03 Iteration 333 Best Cost = 9.395876e+03 Iteration 334 Best Cost = 9.395876e+03 Iteration 335 Best Cost = 9.395876e+03 Iteration 336 Best Cost = 9.395876e+03 Iteration 337 Best Cost = 9.395876e+03 Iteration 338 Best Cost = 9.395876e+03 Iteration 339 Best Cost = 9.395876e+03 Iteration 340 Best Cost = 9.395876e+03 Iteration 341 Best Cost = 9.395876e+03 Iteration 342 Best Cost = 9.395876e+03 Iteration 343 Best Cost = 9.395876e+03 Iteration 344 Best Cost = 9.395876e+03 Iteration 345 Best Cost = 9.395876e+03 Iteration 346 Best Cost = 9.395876e+03 Iteration 347 Best Cost = 9.395876e+03 Iteration 348 Best Cost = 9.395876e+03 Iteration 349 Best Cost = 9.395876e+03 Iteration 350 Best Cost = 9.395876e+03 Iteration 351 Best Cost = 9.395876e+03 Iteration 352 Best Cost = 9.395876e+03 Iteration 353 Best Cost = 9.395876e+03 Iteration 354 Best Cost = 9.395876e+03 Iteration 355 Best Cost = 9.395876e+03 Iteration 356 Best Cost = 9.395876e+03 Iteration 357 Best Cost = 9.395876e+03 Iteration 358 Best Cost = 9.395876e+03 Iteration 359 Best Cost = 9.395876e+03 Iteration 360 Best Cost = 9.395876e+03 Iteration 361 Best Cost = 9.395876e+03 Iteration 362 Best Cost = 9.395876e+03 Iteration 363 Best Cost = 9.395876e+03 Iteration 364 Best Cost = 9.395876e+03 Iteration 365 Best Cost = 9.395876e+03 Iteration 366 Best Cost = 9.395876e+03 Iteration 367 Best Cost = 9.395876e+03 Iteration 368 Best Cost = 9.395876e+03 Iteration 369 Best Cost = 9.395876e+03 Iteration 370 Best Cost = 9.395876e+03 Iteration 371 Best Cost = 9.395876e+03 Iteration 372 Best Cost = 9.395876e+03 Iteration 373 Best Cost = 9.395876e+03 Iteration 374 Best Cost = 9.395876e+03 Iteration 375 Best Cost = 9.395876e+03 Iteration 376 Best Cost = 9.395876e+03 Iteration 377 Best Cost = 9.395876e+03 Iteration 378 Best Cost = 9.395876e+03 Iteration 379 Best Cost = 9.395876e+03 Iteration 380 Best Cost = 9.395876e+03 Iteration 381 Best Cost = 9.395876e+03 Iteration 382 Best Cost = 9.395876e+03 Iteration 383 Best Cost = 9.395876e+03 Iteration 384 Best Cost = 9.395876e+03 Iteration 385 Best Cost = 9.395876e+03 Iteration 386 Best Cost = 9.395876e+03 Iteration 387 Best Cost = 9.395876e+03 Iteration 388 Best Cost = 9.395876e+03 Iteration 389 Best Cost = 9.395876e+03 Iteration 390 Best Cost = 9.395876e+03 Iteration 391 Best Cost = 9.395876e+03 Iteration 392 Best Cost = 9.395876e+03 Iteration 393 Best Cost = 9.395876e+03 Iteration 394 Best Cost = 9.395876e+03 Iteration 395 Best Cost = 9.395876e+03 Iteration 396 Best Cost = 9.395876e+03 Iteration 397 Best Cost = 9.395876e+03 Iteration 398 Best Cost = 9.395876e+03 Iteration 399 Best Cost = 9.395876e+03 Iteration 400 Best Cost = 9.395876e+03 Iteration 401 Best Cost = 9.395876e+03 Iteration 402 Best Cost = 9.395876e+03 Iteration 403 Best Cost = 9.395876e+03 Iteration 404 Best Cost = 9.395876e+03 Iteration 405 Best Cost = 9.395876e+03 Iteration 406 Best Cost = 9.395876e+03 Iteration 407 Best Cost = 9.395876e+03 Iteration 408 Best Cost = 9.395876e+03 Iteration 409 Best Cost = 9.395876e+03 Iteration 410 Best Cost = 9.395876e+03 Iteration 411 Best Cost = 9.395876e+03 Iteration 412 Best Cost = 9.395876e+03 Iteration 413 Best Cost = 9.395876e+03 Iteration 414 Best Cost = 9.395876e+03 Iteration 415 Best Cost = 9.395876e+03 Iteration 416 Best Cost = 9.395876e+03 Iteration 417 Best Cost = 9.395876e+03 Iteration 418 Best Cost = 9.395876e+03 Iteration 419 Best Cost = 9.395876e+03 Iteration 420 Best Cost = 9.395876e+03 Iteration 421 Best Cost = 9.395876e+03 Iteration 422 Best Cost = 9.395876e+03 Iteration 423 Best Cost = 9.395876e+03 Iteration 424 Best Cost = 9.395876e+03 Iteration 425 Best Cost = 9.395876e+03 Iteration 426 Best Cost = 9.395876e+03 Iteration 427 Best Cost = 9.395876e+03 Iteration 428 Best Cost = 9.395876e+03 Iteration 429 Best Cost = 9.395876e+03 Iteration 430 Best Cost = 9.395876e+03 Iteration 431 Best Cost = 9.395876e+03 Iteration 432 Best Cost = 9.395876e+03 Iteration 433 Best Cost = 9.395876e+03 Iteration 434 Best Cost = 9.395876e+03 Iteration 435 Best Cost = 9.395876e+03 Iteration 436 Best Cost = 9.395876e+03 Iteration 437 Best Cost = 9.395876e+03 Iteration 438 Best Cost = 9.395876e+03 Iteration 439 Best Cost = 9.395876e+03 Iteration 440 Best Cost = 9.395876e+03 Iteration 441 Best Cost = 9.395876e+03 Iteration 442 Best Cost = 9.395876e+03 Iteration 443 Best Cost = 9.395876e+03 Iteration 444 Best Cost = 9.395876e+03 Iteration 445 Best Cost = 9.395876e+03 Iteration 446 Best Cost = 9.395876e+03 Iteration 447 Best Cost = 9.395876e+03 Iteration 448 Best Cost = 9.395876e+03 Iteration 449 Best Cost = 9.395876e+03 Iteration 450 Best Cost = 9.395876e+03 Iteration 451 Best Cost = 9.395876e+03 Iteration 452 Best Cost = 9.395876e+03 Iteration 453 Best Cost = 9.395876e+03 Iteration 454 Best Cost = 9.395876e+03 Iteration 455 Best Cost = 9.395876e+03 Iteration 456 Best Cost = 9.395876e+03 Iteration 457 Best Cost = 9.395876e+03 Iteration 458 Best Cost = 9.395876e+03 Iteration 459 Best Cost = 9.395876e+03 Iteration 460 Best Cost = 9.395876e+03 Iteration 461 Best Cost = 9.395876e+03 Iteration 462 Best Cost = 9.395876e+03 Iteration 463 Best Cost = 9.395876e+03 Iteration 464 Best Cost = 9.395876e+03 Iteration 465 Best Cost = 9.395876e+03 Iteration 466 Best Cost = 9.395876e+03 Iteration 467 Best Cost = 9.395876e+03 Iteration 468 Best Cost = 9.395876e+03 Iteration 469 Best Cost = 9.395876e+03 Iteration 470 Best Cost = 9.395876e+03 Iteration 471 Best Cost = 9.395876e+03 Iteration 472 Best Cost = 9.395876e+03 Iteration 473 Best Cost = 9.395876e+03 Iteration 474 Best Cost = 9.395876e+03 Iteration 475 Best Cost = 9.395876e+03 Iteration 476 Best Cost = 9.395876e+03 Iteration 477 Best Cost = 9.395876e+03 Iteration 478 Best Cost = 9.395876e+03 Iteration 479 Best Cost = 9.395876e+03 Iteration 480 Best Cost = 9.395876e+03 Iteration 481 Best Cost = 9.395876e+03 Iteration 482 Best Cost = 9.395876e+03 Iteration 483 Best Cost = 9.395876e+03 Iteration 484 Best Cost = 9.395876e+03 Iteration 485 Best Cost = 9.395876e+03 Iteration 486 Best Cost = 9.395876e+03 Iteration 487 Best Cost = 9.395876e+03 Iteration 488 Best Cost = 9.395876e+03 Iteration 489 Best Cost = 9.395876e+03 Iteration 490 Best Cost = 9.395876e+03 Iteration 491 Best Cost = 9.395876e+03 Iteration 492 Best Cost = 9.395876e+03 Iteration 493 Best Cost = 9.395876e+03 Iteration 494 Best Cost = 9.395876e+03 Iteration 495 Best Cost = 9.395876e+03 Iteration 496 Best Cost = 9.395876e+03 Iteration 497 Best Cost = 9.395876e+03 Iteration 498 Best Cost = 9.395876e+03 Iteration 499 Best Cost = 9.395876e+03 Iteration 500 Best Cost = 9.395876e+03
%% ==========================================
% Estimated Parameters
%% ==========================================
theta = global_best.position;
disp('Estimated Parameters')
Estimated Parameters
disp(theta)
-1.0000 0.9000 0 -0.4000 -40.0000 50.0000 -30.0000 75.0000
sigma1 = theta(1);
sigma2 = theta(2);
delta1 = theta(3);
delta2 = theta(4);
l1 = theta(5);
l2 = theta(6);
l3 = theta(7);
l4 = theta(8);
%% ==========================================
%% ==========================================
% Nonlinear Block
%% ==========================================
khat = l1*g ...
+ l2*g.^2 ...
+ l3*g.^3 ...
+ l4*g.^4;
%% ==========================================
% GL Coefficients
%% ==========================================
alpha = 0.3;
c = zeros(M,1);
c(1) = 1;
for k = 2:M
c(k) = ((k-2)-alpha)/(k-1)*c(k-1);
end
%% ==========================================
% Free Run Simulation
%% ==========================================
xhat = zeros(M,1);
% Initial conditions
xhat(1:2) = x(1:2);
for t = 3:M
% Fractional memory of output
Sx1 = 0;
Sx2 = 0;
% Fractional memory of nonlinear block
Sk1 = 0;
Sk2 = 0;
% Δ^α xhat(t-1)
for j = 1:t-1
Sx1 = Sx1 + c(j)*xhat(t-j);
Sk1 = Sk1 + c(j)*khat(t-j);
end
% Δ^α xhat(t-2)
for j = 1:t-2
Sx2 = Sx2 + c(j)*xhat(t-1-j);
Sk2 = Sk2 + c(j)*khat(t-1-j);
end
xhat(t) = ...
sigma1*Sx1 ...
+ sigma2*Sx2 ...
+ delta1*Sk1 ...
+ delta2*Sk2;
end
%% ==========================================
% Estimated Parameters
%% ==========================================
theta = global_best.position;
disp('======================================');
======================================
disp('Estimated Parameters');
Estimated Parameters
disp('======================================');
======================================
fprintf('sigma1 = %12.6f\n',theta(1));
sigma1 = -1.000000
fprintf('sigma2 = %12.6f\n',theta(2));
sigma2 = 0.900000
fprintf('delta1 = %12.6f\n',theta(3));
delta1 = 0.000000
fprintf('delta2 = %12.6f\n',theta(4));
delta2 = -0.400000
fprintf('l1 = %12.6f\n',theta(5));
l1 = -40.000000
fprintf('l2 = %12.6f\n',theta(6));
l2 = 50.000000
fprintf('l3 = %12.6f\n',theta(7));
l3 = -30.000000
fprintf('l4 = %12.6f\n',theta(8));
l4 = 75.000000
disp(' ');
disp('Parameter Vector')
Parameter Vector
disp(theta)
-1.0000 0.9000 0 -0.4000 -40.0000 50.0000 -30.0000 75.0000
%% ==========================================
% Generate Estimated Output
%% ==========================================
sigma1 = theta(1);
sigma2 = theta(2);
delta1 = theta(3);
delta2 = theta(4);
l1 = theta(5);
l2 = theta(6);
l3 = theta(7);
l4 = theta(8);
khat = l1*g ...
+ l2*g.^2 ...
+ l3*g.^3 ...
+ l4*g.^4;
xhat = zeros(M,1);
xhat(1:5) = x(1:5);
for t = 6:M
Dx1 = ...
x(t-1) ...
-0.3000*x(t-2) ...
-0.1050*x(t-3) ...
-0.0595*x(t-4) ...
-0.0402*x(t-5);
Dx2 = ...
x(t-2) ...
-0.3000*x(t-3) ...
-0.1050*x(t-4) ...
-0.0595*x(t-5);
Dk1 = ...
khat(t-1) ...
-0.3000*khat(t-2) ...
-0.1050*khat(t-3) ...
-0.0595*khat(t-4) ...
-0.0402*khat(t-5);
Dk2 = ...
khat(t-2) ...
-0.3000*khat(t-3) ...
-0.1050*khat(t-4) ...
-0.0595*khat(t-5);
xhat(t) = ...
sigma1*Dx1 ...
+ sigma2*Dx2 ...
+ delta1*Dk1 ...
+ delta2*Dk2;
end
%% ==========================================
% Final MSE
%% ==========================================
FinalMSE = mean((x-xhat).^2);
fprintf('\nFinal MSE = %e\n',FinalMSE);
Final MSE = 9.395876e+03
%% ==========================================
% Save Estimated Parameters
%% ==========================================
ParameterTable = array2table(theta);
ParameterTable.Properties.VariableNames = ...
{'sigma1','sigma2','delta1','delta2','l1','l2','l3','l4'};
writetable(ParameterTable,'Estimated_Parameters.xlsx');
%% ==========================================
% Save Estimated Output
%% ==========================================
Result = table((1:M)',x,xhat,...
'VariableNames',{'Sample','Actual','Estimated'});
writetable(Result,'Estimated_Output.xlsx');
%% ==========================================
% Actual vs Estimated Plot
%% ==========================================
figure
plot(1:M,x,'b','LineWidth',2)
hold on
plot(1:M,xhat,'r--','LineWidth',2)
grid on
xlabel('Sample Number')
ylabel('Output')
legend('Actual Output','Estimated Output','Location','best')
title('Actual vs Estimated Output')
%% ==========================================
% Convergence Curve
%% ==========================================
figure
semilogy(BestCost,'LineWidth',2)
grid on
xlabel('Iteration')
ylabel('Best Cost (MSE)')
title('PSO Convergence')
Warning: Graphics acceleration hardware is unavailable. Graphics quality and performance might be diminished. See MATLAB System Requirements.
function j = fitness_functionh(xext,x,g,M)
%% ==========================================
% Column vectors
%% ==========================================
x = x(:);
g = g(:);
%% ==========================================
% Parameters
%% ==========================================
sigma1 = xext(1);
sigma2 = xext(2);
delta1 = xext(3);
delta2 = xext(4);
l1 = xext(5);
l2 = xext(6);
l3 = xext(7);
l4 = xext(8);
%% ==========================================
% Polynomial Nonlinear Block
%% ==========================================
khat = l1*g ...
+ l2*g.^2 ...
+ l3*g.^3 ...
+ l4*g.^4;
%% ==========================================
% Free Run Simulation
%% ==========================================
xhat = zeros(M,1);
% Initial conditions
xhat(1:5) = x(1:5);
for t = 6:M
Dx1 = ...
x(t-1) ...
-0.3000*x(t-2) ...
-0.1050*x(t-3) ...
-0.0595*x(t-4) ...
-0.0402*x(t-5);
Dx2 = ...
x(t-2) ...
-0.3000*x(t-3) ...
-0.1050*x(t-4) ...
-0.0595*x(t-5);
Dk1 = ...
khat(t-1) ...
-0.3000*khat(t-2) ...
-0.1050*khat(t-3) ...
-0.0595*khat(t-4) ...
-0.0402*khat(t-5);
Dk2 = ...
khat(t-2) ...
-0.3000*khat(t-3) ...
-0.1050*khat(t-4) ...
-0.0595*khat(t-5);
xhat(t) = ...
sigma1*Dx1 ...
+ sigma2*Dx2 ...
+ delta1*Dk1 ...
+ delta2*Dk2;
end
%% ==========================================
% Mean Square Error
%% ==========================================
j = mean((x-xhat).^2);
if isnan(j) || isinf(j)
j = 1e20;
end
end

Respuestas (1)

The main issue with the code you have shared is that "sigma1" is constrained negative, but the model used "+ sigma1*Dx1", which pushes the estimated output to the wrong sign.
To resolve the issue, change the model to use "-sigma1*Dx1" consistently in both fitness and final output.
Hope this resolves the issue.
Regards,
Matt

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Jul. de 2026

Respondida:

el 23 de Jul. de 2026

Community Treasure Hunt

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

Start Hunting!

Translated by