i need code for the example1 model equation using pso

i need code using pso ,without data flitering using model equation for ex-1,is it possible to give code for me to generate simulation results.

Antworten (2)

Sam Chak
Sam Chak am 9 Okt. 2026 um 4:30
Properly modeling the fractional-order Wiener–Hammerstein system and the adaptive fuzzy system structure in Example 1 in MATLAB would require additional coding time or tools from MATLAB File Exchange.
If you’d like to see an example of using PSO to identify the parameters of a nonlinear dynamic system, here is a basic code using particleswarm() for identifying parameters {a, b, c} in this system:
% noise-free data
tin = linspace(0, 10, 51)';
xout = tanh(tin); % a known solution for dx/dt = 1 - x(t)² with initial value x(0) = 0
% identification via PSO
fun = @(p) costfcn(p, tin, xout); % find p vector only in J
nvars = 3; % 3 unknown variable in p
lb = [0.9, 0.9, 1.9]; % lower bound (requires user guess)
ub = [1.1, 1.1, 2.1]; % upper bound (requires user guess)
[p, fval, exitflag, output] = particleswarm(fun, nvars, lb, ub)
Optimization ended: relative change in the objective value over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
p = 1×3
1.0003 1.0003 1.9972
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = 2.1429e-04
exitflag = 1
output = struct with fields:
rngstate: [1×1 struct] iterations: 80 funccount: 2430 message: 'Optimization ended: relative change in the objective value ↵over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.' hybridflag: []
% comparison
[t, x] = ode45(@(t, x) odefcn(t, x, p), tin, xout(1));
plot(t, x), hold on
plot(tin, xout, '.'), hold off
grid on
xlabel('Time, t')
ylabel('Amplitude, x(t)')
title('Comparison')
legend('Identified system', 'True solution (data)')
% cost function, J, (to minimize the error between x and xout)
function J = costfcn(p, tin, xout)
tspan = tin;
x0 = 0;
[t, x] = ode45(@(t, x) odefcn(t, x, p), tspan, x0);
J = norm(x - xout);
end
% nonlinear system, dx = f(x(t)), only parameters are unknown, but the structure is known
function dx = odefcn(t, x, p)
% dx = 1 - x^2; % known system
dx = p(1) - p(2)*x^p(3); % unknown system
end
You appear to be an expert in PSO. I used your standalone PSO code, with minor tweaks, from another thread to identify the parameters of the same nonlinear dynamic system. The results are impressive as well.
%% ==========================================
% DATASET
%% =========================================
g = linspace(0, 10, 51)';
x = tanh(g);
%% ==========================================
% PSO PARAMETERS
%% ==========================================
dim = 3;
nParticles = 100;
maxIter = 500;
lb = [0.9, 0.9, 1.9]; % lower bound (requires user guess)
ub = [1.1, 1.1, 2.1]; % upper bound (requires user guess)
%% ==========================================
% 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);
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);
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 = 1.726705e-07 Iteration 2 Best Cost = 1.710162e-07 Iteration 3 Best Cost = 8.333953e-08 Iteration 4 Best Cost = 7.501296e-08 Iteration 5 Best Cost = 7.501296e-08 Iteration 6 Best Cost = 5.437119e-08 Iteration 7 Best Cost = 4.906860e-08 Iteration 8 Best Cost = 4.906860e-08 Iteration 9 Best Cost = 4.440833e-08 Iteration 10 Best Cost = 4.440833e-08 Iteration 11 Best Cost = 3.798649e-08 Iteration 12 Best Cost = 3.786442e-08 Iteration 13 Best Cost = 3.327946e-08 Iteration 14 Best Cost = 2.740116e-08 Iteration 15 Best Cost = 1.256102e-08 Iteration 16 Best Cost = 3.227137e-09 Iteration 17 Best Cost = 1.424000e-09 Iteration 18 Best Cost = 1.424000e-09 Iteration 19 Best Cost = 1.172687e-09 Iteration 20 Best Cost = 1.134270e-09 Iteration 21 Best Cost = 1.134270e-09 Iteration 22 Best Cost = 9.610087e-10 Iteration 23 Best Cost = 9.610087e-10 Iteration 24 Best Cost = 9.610087e-10 Iteration 25 Best Cost = 9.248028e-10 Iteration 26 Best Cost = 9.235203e-10 Iteration 27 Best Cost = 9.235203e-10 Iteration 28 Best Cost = 9.228408e-10 Iteration 29 Best Cost = 9.206767e-10 Iteration 30 Best Cost = 9.073599e-10 Iteration 31 Best Cost = 9.014428e-10 Iteration 32 Best Cost = 8.990494e-10 Iteration 33 Best Cost = 8.974113e-10 Iteration 34 Best Cost = 8.974113e-10 Iteration 35 Best Cost = 8.973885e-10 Iteration 36 Best Cost = 8.973746e-10 Iteration 37 Best Cost = 8.973746e-10 Iteration 38 Best Cost = 8.973655e-10 Iteration 39 Best Cost = 8.973655e-10 Iteration 40 Best Cost = 8.973655e-10 Iteration 41 Best Cost = 8.973655e-10 Iteration 42 Best Cost = 8.973636e-10 Iteration 43 Best Cost = 8.973562e-10 Iteration 44 Best Cost = 8.973511e-10 Iteration 45 Best Cost = 8.973511e-10 Iteration 46 Best Cost = 8.973511e-10 Iteration 47 Best Cost = 8.973454e-10 Iteration 48 Best Cost = 8.973452e-10 Iteration 49 Best Cost = 8.973452e-10 Iteration 50 Best Cost = 8.973445e-10 Iteration 51 Best Cost = 8.973445e-10 Iteration 52 Best Cost = 8.973445e-10 Iteration 53 Best Cost = 8.973441e-10 Iteration 54 Best Cost = 8.973441e-10 Iteration 55 Best Cost = 8.973441e-10 Iteration 56 Best Cost = 8.973441e-10 Iteration 57 Best Cost = 8.973441e-10 Iteration 58 Best Cost = 8.973441e-10 Iteration 59 Best Cost = 8.973441e-10 Iteration 60 Best Cost = 8.973440e-10 Iteration 61 Best Cost = 8.973440e-10 Iteration 62 Best Cost = 8.973440e-10 Iteration 63 Best Cost = 8.973440e-10 Iteration 64 Best Cost = 8.973440e-10 Iteration 65 Best Cost = 8.973440e-10 Iteration 66 Best Cost = 8.973440e-10 Iteration 67 Best Cost = 8.973440e-10 Iteration 68 Best Cost = 8.973440e-10 Iteration 69 Best Cost = 8.973440e-10 Iteration 70 Best Cost = 8.973440e-10 Iteration 71 Best Cost = 8.973440e-10 Iteration 72 Best Cost = 8.973440e-10 Iteration 73 Best Cost = 8.973440e-10 Iteration 74 Best Cost = 8.973440e-10 Iteration 75 Best Cost = 8.973440e-10 Iteration 76 Best Cost = 8.973440e-10 Iteration 77 Best Cost = 8.973440e-10 Iteration 78 Best Cost = 8.973440e-10 Iteration 79 Best Cost = 8.973440e-10 Iteration 80 Best Cost = 8.973440e-10 Iteration 81 Best Cost = 8.973440e-10 Iteration 82 Best Cost = 8.973440e-10 Iteration 83 Best Cost = 8.973440e-10 Iteration 84 Best Cost = 8.973440e-10 Iteration 85 Best Cost = 8.973440e-10 Iteration 86 Best Cost = 8.973440e-10 Iteration 87 Best Cost = 8.973440e-10 Iteration 88 Best Cost = 8.973440e-10 Iteration 89 Best Cost = 8.973440e-10 Iteration 90 Best Cost = 8.973440e-10 Iteration 91 Best Cost = 8.973440e-10 Iteration 92 Best Cost = 8.973440e-10 Iteration 93 Best Cost = 8.973440e-10 Iteration 94 Best Cost = 8.973440e-10 Iteration 95 Best Cost = 8.973440e-10 Iteration 96 Best Cost = 8.973440e-10 Iteration 97 Best Cost = 8.973440e-10 Iteration 98 Best Cost = 8.973440e-10 Iteration 99 Best Cost = 8.973440e-10 Iteration 100 Best Cost = 8.973440e-10 Iteration 101 Best Cost = 8.973440e-10 Iteration 102 Best Cost = 8.973440e-10 Iteration 103 Best Cost = 8.973440e-10 Iteration 104 Best Cost = 8.973440e-10 Iteration 105 Best Cost = 8.973440e-10 Iteration 106 Best Cost = 8.973440e-10 Iteration 107 Best Cost = 8.973440e-10 Iteration 108 Best Cost = 8.973440e-10 Iteration 109 Best Cost = 8.973440e-10 Iteration 110 Best Cost = 8.973440e-10 Iteration 111 Best Cost = 8.973440e-10 Iteration 112 Best Cost = 8.973440e-10 Iteration 113 Best Cost = 8.973440e-10 Iteration 114 Best Cost = 8.973440e-10 Iteration 115 Best Cost = 8.973440e-10 Iteration 116 Best Cost = 8.973440e-10 Iteration 117 Best Cost = 8.973440e-10 Iteration 118 Best Cost = 8.973440e-10 Iteration 119 Best Cost = 8.973440e-10 Iteration 120 Best Cost = 8.973440e-10 Iteration 121 Best Cost = 8.973440e-10 Iteration 122 Best Cost = 8.973440e-10 Iteration 123 Best Cost = 8.973440e-10 Iteration 124 Best Cost = 8.973440e-10 Iteration 125 Best Cost = 8.973440e-10 Iteration 126 Best Cost = 8.973440e-10 Iteration 127 Best Cost = 8.973440e-10 Iteration 128 Best Cost = 8.973440e-10 Iteration 129 Best Cost = 8.973440e-10 Iteration 130 Best Cost = 8.973440e-10 Iteration 131 Best Cost = 8.973440e-10 Iteration 132 Best Cost = 8.973440e-10 Iteration 133 Best Cost = 8.973440e-10 Iteration 134 Best Cost = 8.973440e-10 Iteration 135 Best Cost = 8.973440e-10 Iteration 136 Best Cost = 8.973440e-10 Iteration 137 Best Cost = 8.973440e-10 Iteration 138 Best Cost = 8.973440e-10 Iteration 139 Best Cost = 8.973440e-10 Iteration 140 Best Cost = 8.973440e-10 Iteration 141 Best Cost = 8.973440e-10 Iteration 142 Best Cost = 8.973440e-10 Iteration 143 Best Cost = 8.973440e-10 Iteration 144 Best Cost = 8.973440e-10 Iteration 145 Best Cost = 8.973440e-10 Iteration 146 Best Cost = 8.973440e-10 Iteration 147 Best Cost = 8.973440e-10 Iteration 148 Best Cost = 8.973440e-10 Iteration 149 Best Cost = 8.973440e-10 Iteration 150 Best Cost = 8.973440e-10 Iteration 151 Best Cost = 8.973440e-10 Iteration 152 Best Cost = 8.973440e-10 Iteration 153 Best Cost = 8.973440e-10 Iteration 154 Best Cost = 8.973440e-10 Iteration 155 Best Cost = 8.973440e-10 Iteration 156 Best Cost = 8.973440e-10 Iteration 157 Best Cost = 8.973440e-10 Iteration 158 Best Cost = 8.973440e-10 Iteration 159 Best Cost = 8.973440e-10 Iteration 160 Best Cost = 8.973440e-10 Iteration 161 Best Cost = 8.973440e-10 Iteration 162 Best Cost = 8.973440e-10 Iteration 163 Best Cost = 8.973440e-10 Iteration 164 Best Cost = 8.973440e-10 Iteration 165 Best Cost = 8.973440e-10 Iteration 166 Best Cost = 8.973440e-10 Iteration 167 Best Cost = 8.973440e-10 Iteration 168 Best Cost = 8.973440e-10 Iteration 169 Best Cost = 8.973440e-10 Iteration 170 Best Cost = 8.973440e-10 Iteration 171 Best Cost = 8.973440e-10 Iteration 172 Best Cost = 8.973440e-10 Iteration 173 Best Cost = 8.973440e-10 Iteration 174 Best Cost = 8.973440e-10 Iteration 175 Best Cost = 8.973440e-10 Iteration 176 Best Cost = 8.973440e-10 Iteration 177 Best Cost = 8.973440e-10 Iteration 178 Best Cost = 8.973440e-10 Iteration 179 Best Cost = 8.973440e-10 Iteration 180 Best Cost = 8.973440e-10 Iteration 181 Best Cost = 8.973440e-10 Iteration 182 Best Cost = 8.973440e-10 Iteration 183 Best Cost = 8.973440e-10 Iteration 184 Best Cost = 8.973440e-10 Iteration 185 Best Cost = 8.973440e-10 Iteration 186 Best Cost = 8.973440e-10 Iteration 187 Best Cost = 8.973440e-10 Iteration 188 Best Cost = 8.973440e-10 Iteration 189 Best Cost = 8.973440e-10 Iteration 190 Best Cost = 8.973440e-10 Iteration 191 Best Cost = 8.973440e-10 Iteration 192 Best Cost = 8.973440e-10 Iteration 193 Best Cost = 8.973440e-10 Iteration 194 Best Cost = 8.973440e-10 Iteration 195 Best Cost = 8.973440e-10 Iteration 196 Best Cost = 8.973440e-10 Iteration 197 Best Cost = 8.973440e-10 Iteration 198 Best Cost = 8.973440e-10 Iteration 199 Best Cost = 8.973440e-10 Iteration 200 Best Cost = 8.973440e-10 Iteration 201 Best Cost = 8.973440e-10 Iteration 202 Best Cost = 8.973440e-10 Iteration 203 Best Cost = 8.973440e-10 Iteration 204 Best Cost = 8.973440e-10 Iteration 205 Best Cost = 8.973440e-10 Iteration 206 Best Cost = 8.973440e-10 Iteration 207 Best Cost = 8.973440e-10 Iteration 208 Best Cost = 8.973440e-10 Iteration 209 Best Cost = 8.973440e-10 Iteration 210 Best Cost = 8.973440e-10 Iteration 211 Best Cost = 8.973440e-10 Iteration 212 Best Cost = 8.973440e-10 Iteration 213 Best Cost = 8.973440e-10 Iteration 214 Best Cost = 8.973440e-10 Iteration 215 Best Cost = 8.973440e-10 Iteration 216 Best Cost = 8.973440e-10 Iteration 217 Best Cost = 8.973440e-10 Iteration 218 Best Cost = 8.973440e-10 Iteration 219 Best Cost = 8.973440e-10 Iteration 220 Best Cost = 8.973440e-10 Iteration 221 Best Cost = 8.973440e-10 Iteration 222 Best Cost = 8.973440e-10 Iteration 223 Best Cost = 8.973440e-10 Iteration 224 Best Cost = 8.973440e-10 Iteration 225 Best Cost = 8.973440e-10 Iteration 226 Best Cost = 8.973440e-10 Iteration 227 Best Cost = 8.973440e-10 Iteration 228 Best Cost = 8.973440e-10 Iteration 229 Best Cost = 8.973440e-10 Iteration 230 Best Cost = 8.973440e-10 Iteration 231 Best Cost = 8.973440e-10 Iteration 232 Best Cost = 8.973440e-10 Iteration 233 Best Cost = 8.973440e-10 Iteration 234 Best Cost = 8.973440e-10 Iteration 235 Best Cost = 8.973440e-10 Iteration 236 Best Cost = 8.973440e-10 Iteration 237 Best Cost = 8.973440e-10 Iteration 238 Best Cost = 8.973440e-10 Iteration 239 Best Cost = 8.973440e-10 Iteration 240 Best Cost = 8.973440e-10 Iteration 241 Best Cost = 8.973440e-10 Iteration 242 Best Cost = 8.973440e-10 Iteration 243 Best Cost = 8.973440e-10 Iteration 244 Best Cost = 8.973440e-10 Iteration 245 Best Cost = 8.973440e-10 Iteration 246 Best Cost = 8.973440e-10 Iteration 247 Best Cost = 8.973440e-10 Iteration 248 Best Cost = 8.973440e-10 Iteration 249 Best Cost = 8.973440e-10 Iteration 250 Best Cost = 8.973440e-10 Iteration 251 Best Cost = 8.973440e-10 Iteration 252 Best Cost = 8.973440e-10 Iteration 253 Best Cost = 8.973440e-10 Iteration 254 Best Cost = 8.973440e-10 Iteration 255 Best Cost = 8.973440e-10 Iteration 256 Best Cost = 8.973440e-10 Iteration 257 Best Cost = 8.973440e-10 Iteration 258 Best Cost = 8.973440e-10 Iteration 259 Best Cost = 8.973440e-10 Iteration 260 Best Cost = 8.973440e-10 Iteration 261 Best Cost = 8.973440e-10 Iteration 262 Best Cost = 8.973440e-10 Iteration 263 Best Cost = 8.973440e-10 Iteration 264 Best Cost = 8.973440e-10 Iteration 265 Best Cost = 8.973440e-10 Iteration 266 Best Cost = 8.973440e-10 Iteration 267 Best Cost = 8.973440e-10 Iteration 268 Best Cost = 8.973440e-10 Iteration 269 Best Cost = 8.973440e-10 Iteration 270 Best Cost = 8.973440e-10 Iteration 271 Best Cost = 8.973440e-10 Iteration 272 Best Cost = 8.973440e-10 Iteration 273 Best Cost = 8.973440e-10 Iteration 274 Best Cost = 8.973440e-10 Iteration 275 Best Cost = 8.973440e-10 Iteration 276 Best Cost = 8.973440e-10 Iteration 277 Best Cost = 8.973440e-10 Iteration 278 Best Cost = 8.973440e-10 Iteration 279 Best Cost = 8.973440e-10 Iteration 280 Best Cost = 8.973440e-10 Iteration 281 Best Cost = 8.973440e-10 Iteration 282 Best Cost = 8.973440e-10 Iteration 283 Best Cost = 8.973440e-10 Iteration 284 Best Cost = 8.973440e-10 Iteration 285 Best Cost = 8.973440e-10 Iteration 286 Best Cost = 8.973440e-10 Iteration 287 Best Cost = 8.973440e-10 Iteration 288 Best Cost = 8.973440e-10 Iteration 289 Best Cost = 8.973440e-10 Iteration 290 Best Cost = 8.973440e-10 Iteration 291 Best Cost = 8.973440e-10 Iteration 292 Best Cost = 8.973440e-10 Iteration 293 Best Cost = 8.973440e-10 Iteration 294 Best Cost = 8.973440e-10 Iteration 295 Best Cost = 8.973440e-10 Iteration 296 Best Cost = 8.973440e-10 Iteration 297 Best Cost = 8.973440e-10 Iteration 298 Best Cost = 8.973440e-10 Iteration 299 Best Cost = 8.973440e-10 Iteration 300 Best Cost = 8.973440e-10 Iteration 301 Best Cost = 8.973440e-10 Iteration 302 Best Cost = 8.973440e-10 Iteration 303 Best Cost = 8.973440e-10 Iteration 304 Best Cost = 8.973440e-10 Iteration 305 Best Cost = 8.973440e-10 Iteration 306 Best Cost = 8.973440e-10 Iteration 307 Best Cost = 8.973440e-10 Iteration 308 Best Cost = 8.973440e-10 Iteration 309 Best Cost = 8.973440e-10 Iteration 310 Best Cost = 8.973440e-10 Iteration 311 Best Cost = 8.973440e-10 Iteration 312 Best Cost = 8.973440e-10 Iteration 313 Best Cost = 8.973440e-10 Iteration 314 Best Cost = 8.973440e-10 Iteration 315 Best Cost = 8.973440e-10 Iteration 316 Best Cost = 8.973440e-10 Iteration 317 Best Cost = 8.973440e-10 Iteration 318 Best Cost = 8.973440e-10 Iteration 319 Best Cost = 8.973440e-10 Iteration 320 Best Cost = 8.973440e-10 Iteration 321 Best Cost = 8.973440e-10 Iteration 322 Best Cost = 8.973440e-10 Iteration 323 Best Cost = 8.973440e-10 Iteration 324 Best Cost = 8.973440e-10 Iteration 325 Best Cost = 8.973440e-10 Iteration 326 Best Cost = 8.973440e-10 Iteration 327 Best Cost = 8.973440e-10 Iteration 328 Best Cost = 8.973440e-10 Iteration 329 Best Cost = 8.973440e-10 Iteration 330 Best Cost = 8.973440e-10 Iteration 331 Best Cost = 8.973440e-10 Iteration 332 Best Cost = 8.973440e-10 Iteration 333 Best Cost = 8.973440e-10 Iteration 334 Best Cost = 8.973440e-10 Iteration 335 Best Cost = 8.973440e-10 Iteration 336 Best Cost = 8.973440e-10 Iteration 337 Best Cost = 8.973440e-10 Iteration 338 Best Cost = 8.973440e-10 Iteration 339 Best Cost = 8.973440e-10 Iteration 340 Best Cost = 8.973440e-10 Iteration 341 Best Cost = 8.973440e-10 Iteration 342 Best Cost = 8.973440e-10 Iteration 343 Best Cost = 8.973440e-10 Iteration 344 Best Cost = 8.973440e-10 Iteration 345 Best Cost = 8.973440e-10 Iteration 346 Best Cost = 8.973440e-10 Iteration 347 Best Cost = 8.973440e-10 Iteration 348 Best Cost = 8.973440e-10 Iteration 349 Best Cost = 8.973440e-10 Iteration 350 Best Cost = 8.973440e-10 Iteration 351 Best Cost = 8.973440e-10 Iteration 352 Best Cost = 8.973440e-10 Iteration 353 Best Cost = 8.973440e-10 Iteration 354 Best Cost = 8.973440e-10 Iteration 355 Best Cost = 8.973440e-10 Iteration 356 Best Cost = 8.973440e-10 Iteration 357 Best Cost = 8.973440e-10 Iteration 358 Best Cost = 8.973440e-10 Iteration 359 Best Cost = 8.973440e-10 Iteration 360 Best Cost = 8.973440e-10 Iteration 361 Best Cost = 8.973440e-10 Iteration 362 Best Cost = 8.973440e-10 Iteration 363 Best Cost = 8.973440e-10 Iteration 364 Best Cost = 8.973440e-10 Iteration 365 Best Cost = 8.973440e-10 Iteration 366 Best Cost = 8.973440e-10 Iteration 367 Best Cost = 8.973440e-10 Iteration 368 Best Cost = 8.973440e-10 Iteration 369 Best Cost = 8.973440e-10 Iteration 370 Best Cost = 8.973440e-10 Iteration 371 Best Cost = 8.973440e-10 Iteration 372 Best Cost = 8.973440e-10 Iteration 373 Best Cost = 8.973440e-10 Iteration 374 Best Cost = 8.973440e-10 Iteration 375 Best Cost = 8.973440e-10 Iteration 376 Best Cost = 8.973440e-10 Iteration 377 Best Cost = 8.973440e-10 Iteration 378 Best Cost = 8.973440e-10 Iteration 379 Best Cost = 8.973440e-10 Iteration 380 Best Cost = 8.973440e-10 Iteration 381 Best Cost = 8.973440e-10 Iteration 382 Best Cost = 8.973440e-10 Iteration 383 Best Cost = 8.973440e-10 Iteration 384 Best Cost = 8.973440e-10 Iteration 385 Best Cost = 8.973440e-10 Iteration 386 Best Cost = 8.973440e-10 Iteration 387 Best Cost = 8.973440e-10 Iteration 388 Best Cost = 8.973440e-10 Iteration 389 Best Cost = 8.973440e-10 Iteration 390 Best Cost = 8.973440e-10 Iteration 391 Best Cost = 8.973440e-10 Iteration 392 Best Cost = 8.973440e-10 Iteration 393 Best Cost = 8.973440e-10 Iteration 394 Best Cost = 8.973440e-10 Iteration 395 Best Cost = 8.973440e-10 Iteration 396 Best Cost = 8.973440e-10 Iteration 397 Best Cost = 8.973440e-10 Iteration 398 Best Cost = 8.973440e-10 Iteration 399 Best Cost = 8.973440e-10 Iteration 400 Best Cost = 8.973440e-10 Iteration 401 Best Cost = 8.973440e-10 Iteration 402 Best Cost = 8.973440e-10 Iteration 403 Best Cost = 8.973440e-10 Iteration 404 Best Cost = 8.973440e-10 Iteration 405 Best Cost = 8.973440e-10 Iteration 406 Best Cost = 8.973440e-10 Iteration 407 Best Cost = 8.973440e-10 Iteration 408 Best Cost = 8.973440e-10 Iteration 409 Best Cost = 8.973440e-10 Iteration 410 Best Cost = 8.973440e-10 Iteration 411 Best Cost = 8.973440e-10 Iteration 412 Best Cost = 8.973440e-10 Iteration 413 Best Cost = 8.973440e-10 Iteration 414 Best Cost = 8.973440e-10 Iteration 415 Best Cost = 8.973440e-10 Iteration 416 Best Cost = 8.973440e-10 Iteration 417 Best Cost = 8.973440e-10 Iteration 418 Best Cost = 8.973440e-10 Iteration 419 Best Cost = 8.973440e-10 Iteration 420 Best Cost = 8.973440e-10 Iteration 421 Best Cost = 8.973440e-10 Iteration 422 Best Cost = 8.973440e-10 Iteration 423 Best Cost = 8.973440e-10 Iteration 424 Best Cost = 8.973440e-10 Iteration 425 Best Cost = 8.973440e-10 Iteration 426 Best Cost = 8.973440e-10 Iteration 427 Best Cost = 8.973440e-10 Iteration 428 Best Cost = 8.973440e-10 Iteration 429 Best Cost = 8.973440e-10 Iteration 430 Best Cost = 8.973440e-10 Iteration 431 Best Cost = 8.973440e-10 Iteration 432 Best Cost = 8.973440e-10 Iteration 433 Best Cost = 8.973440e-10 Iteration 434 Best Cost = 8.973440e-10 Iteration 435 Best Cost = 8.973440e-10 Iteration 436 Best Cost = 8.973440e-10 Iteration 437 Best Cost = 8.973440e-10 Iteration 438 Best Cost = 8.973440e-10 Iteration 439 Best Cost = 8.973440e-10 Iteration 440 Best Cost = 8.973440e-10 Iteration 441 Best Cost = 8.973440e-10 Iteration 442 Best Cost = 8.973440e-10 Iteration 443 Best Cost = 8.973440e-10 Iteration 444 Best Cost = 8.973440e-10 Iteration 445 Best Cost = 8.973440e-10 Iteration 446 Best Cost = 8.973440e-10 Iteration 447 Best Cost = 8.973440e-10 Iteration 448 Best Cost = 8.973440e-10 Iteration 449 Best Cost = 8.973440e-10 Iteration 450 Best Cost = 8.973440e-10 Iteration 451 Best Cost = 8.973440e-10 Iteration 452 Best Cost = 8.973440e-10 Iteration 453 Best Cost = 8.973440e-10 Iteration 454 Best Cost = 8.973440e-10 Iteration 455 Best Cost = 8.973440e-10 Iteration 456 Best Cost = 8.973440e-10 Iteration 457 Best Cost = 8.973440e-10 Iteration 458 Best Cost = 8.973440e-10 Iteration 459 Best Cost = 8.973440e-10 Iteration 460 Best Cost = 8.973440e-10 Iteration 461 Best Cost = 8.973440e-10 Iteration 462 Best Cost = 8.973440e-10 Iteration 463 Best Cost = 8.973440e-10 Iteration 464 Best Cost = 8.973440e-10 Iteration 465 Best Cost = 8.973440e-10 Iteration 466 Best Cost = 8.973440e-10 Iteration 467 Best Cost = 8.973440e-10 Iteration 468 Best Cost = 8.973440e-10 Iteration 469 Best Cost = 8.973440e-10 Iteration 470 Best Cost = 8.973440e-10 Iteration 471 Best Cost = 8.973440e-10 Iteration 472 Best Cost = 8.973440e-10 Iteration 473 Best Cost = 8.973440e-10 Iteration 474 Best Cost = 8.973440e-10 Iteration 475 Best Cost = 8.973440e-10 Iteration 476 Best Cost = 8.973440e-10 Iteration 477 Best Cost = 8.973440e-10 Iteration 478 Best Cost = 8.973440e-10 Iteration 479 Best Cost = 8.973440e-10 Iteration 480 Best Cost = 8.973440e-10 Iteration 481 Best Cost = 8.973440e-10 Iteration 482 Best Cost = 8.973440e-10 Iteration 483 Best Cost = 8.973440e-10 Iteration 484 Best Cost = 8.973440e-10 Iteration 485 Best Cost = 8.973440e-10 Iteration 486 Best Cost = 8.973440e-10 Iteration 487 Best Cost = 8.973440e-10 Iteration 488 Best Cost = 8.973440e-10 Iteration 489 Best Cost = 8.973440e-10 Iteration 490 Best Cost = 8.973440e-10 Iteration 491 Best Cost = 8.973440e-10 Iteration 492 Best Cost = 8.973440e-10 Iteration 493 Best Cost = 8.973440e-10 Iteration 494 Best Cost = 8.973440e-10 Iteration 495 Best Cost = 8.973440e-10 Iteration 496 Best Cost = 8.973440e-10 Iteration 497 Best Cost = 8.973440e-10 Iteration 498 Best Cost = 8.973440e-10 Iteration 499 Best Cost = 8.973440e-10 Iteration 500 Best Cost = 8.973440e-10
%% ==========================================
% Estimated Parameters
%% ==========================================
theta = global_best.position
theta = 1×3
1.0003 1.0003 1.9970
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% ==========================================
% Comparison
%% ==========================================
[tsim, xsim] = ode45(@(t, x) odefcn(t, x, theta), g, x(1));
plot(tsim, xsim), hold on
plot(g, x, '.'), hold off
grid on
xlabel('Time, t')
ylabel('Amplitude, x(t)')
title('Comparison')
legend('Identified system', 'True solution (data)')
%% ==========================================
% Cost function called "Fitness"
%% ==========================================
function j = fitness_functionh(xext, x, g)
%% ==========================================
% Column vectors
%% ==========================================
x = x(:);
g = g(:);
%% ==========================================
% Parameters
%% ==========================================
p(1) = xext(1);
p(2) = xext(2);
p(3) = xext(3);
%% ==========================================
% Free Run Simulation
%% ==========================================
tspan = g;
x0 = x(1);
[t, xhat] = ode45(@(t, x) odefcn(t, x, p), tspan, x0);
%% ==========================================
% Mean Square Error
%% ==========================================
j = mean((x - xhat).^2);
if isnan(j) || isinf(j)
j = 1e20;
end
end
%% ==========================================
% Nonlinear system
%% ==========================================
function dx = odefcn(t, x, p)
% dx = 1 - x^2; % known system
dx = p(1) - p(2)*x^p(3); % unknown system
end

Gefragt:

am 8 Okt. 2026 um 16:58

Beantwortet:

am 9 Okt. 2026 um 9:18

Community Treasure Hunt

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

Start Hunting!

Translated by