Derivative of state ‘1’ in block ‘two_area_LFC_PID/Integrator9’ at time 8.820174943555763 is not finite.
I have simulated a system in Simulink MATLAB that I want to optimize with the PID controller.
To find the best controller coefficients, I use WOA optimization method. But when I run it, I get the following error “Derivative of state ‘1’ in block ‘two_area_LFC_PID/Integrator9’ at time 8.820174943555763 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)”. Is there a solution to my problem? I have changed the step size. I also changed the solver to ODE 15s(stiff/NDF). But the problem was not solved. I have posted the desired system image with algorithm codes for friends.
CODE:
function bestSolution = EWOA_Run_PID()
% Step 1: Initialize the input arguments for EWOA
nVar = 6;
ub = [50 50 50 50 50 50];
lb = [0.01 0.01 0.01 0.01 0.01 0.01];
MaxIter = 50;
PopulationSize = 50;
% Step 2: Define the objective function handle using EWAO_Cost_PID_CF_CE
objFcn = @(params) EWOA_Cost_PID(params);
% Step 3: Call the EWOA function
bestSolution = EWOA(nVar, lb, ub, objFcn, MaxIter, PopulationSize);
end
function bestSolution = EWOA(nVar, lb, ub, objFcn, MaxIter, PopulationSize)
% Step 1: Initialize the population
whalesPositions = bsxfun(@plus, lb, bsxfun(@times, rand(PopulationSize, nVar), (ub – lb)));
% Step 2: Evaluate the objective function for each whale
whalesCost = zeros(PopulationSize, 1);
for i = 1:PopulationSize
whalesCost(i) = objFcn(whalesPositions(i, :));
end
% Step 3: Set the initial best position and cost
[globalBestCost, gBestIndex] = min(whalesCost);
globalBestPosition = whalesPositions(gBestIndex, :);
% Step 4: Initialize elite whale
eliteWhalePosition = globalBestPosition;
eliteWhaleCost = globalBestCost;
% Step 5: Set the termination criterion parameters
maxIterWithoutImprovement = 50; % Adjust this value as needed
iterWithoutImprovement = 50;
% Main loop
for iter = 1:MaxIter
a = 2 – iter * ((2) / MaxIter); % Parameter to control the search space reduction
for i = 1:PopulationSize
% Generate random vectors
r1 = rand(1, nVar);
r2 = rand(1, nVar);
% Calculate the A and C coefficients
A = 2 * a * r1 – a;
C = 2 * r2;
% Update the position of the whales using the WOA operators
D = abs(C .* globalBestPosition – whalesPositions(i, :));
newWhalePosition = globalBestPosition – A .* D;
% Clip positions to stay within the bounds
newWhalePosition = max(newWhalePosition, lb);
newWhalePosition = min(newWhalePosition, ub);
% Evaluate the new position using the EWAO_Cost_PID_CF_CE function
newWhaleCost = EWOA_Cost_PID(newWhalePosition);
% Update the whale’s position and cost if a better solution is found
if newWhaleCost < whalesCost(i)
whalesPositions(i, 🙂 = newWhalePosition;
whalesCost(i) = newWhaleCost;
end
% Update the global best position and cost if a better solution is found
if whalesCost(i) < globalBestCost
globalBestCost = whalesCost(i);
globalBestPosition = whalesPositions(i, :);
end
end
% Check for termination criterion
if globalBestCost < eliteWhaleCost
eliteWhalePosition = globalBestPosition;
eliteWhaleCost = globalBestCost;
iterWithoutImprovement = 0;
else
iterWithoutImprovement = iterWithoutImprovement + 1;
end
% Display the best cost at each iteration
disp([‘Iteration ‘, num2str(iter), ‘: Best Cost = ‘, num2str(eliteWhaleCost)]);
% Check if termination criterion is met
if iterWithoutImprovement >= maxIterWithoutImprovement
break;
end
end
% Output the best solution found
bestSolution = eliteWhalePosition;
end
Code
function cost = EWOA_Cost_PID(params)
% Assign parameters to the base workspace using assignin
assignin(‘base’, ‘KP’, params(1));
assignin(‘base’, ‘KI’, params(2));
assignin(‘base’, ‘KD’, params(3));
assignin(‘base’, ‘KPP’, params(4));
assignin(‘base’, ‘KII’, params(5));
assignin(‘base’, ‘KDD’, params(6));
% Run the Simulink model
sim(‘two_area_LFC_PID’);
% Calculate penalties based on maximum absolute values of signals
g1 = max(abs(f1.signals.values)) – 0.25;
g2 = max(abs(f2.signals.values)) – 0.25;
g3 = max(abs(pt.signals.values)) – 0.05;
% Define a large penalty coefficient
lambda = 10^20;
% Compute penalties for constraint violations
Penalty1 = lambda * g1^2 * GetInequality(g1);
Penalty2 = lambda * g2^2 * GetInequality(g2);
Penalty3 = lambda * g3^2 * GetInequality(g3);
% Calculate the total cost
cost = J_obj.signals.values(end) + Penalty1 + Penalty2 + Penalty3;
endI have simulated a system in Simulink MATLAB that I want to optimize with the PID controller.
To find the best controller coefficients, I use WOA optimization method. But when I run it, I get the following error “Derivative of state ‘1’ in block ‘two_area_LFC_PID/Integrator9’ at time 8.820174943555763 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)”. Is there a solution to my problem? I have changed the step size. I also changed the solver to ODE 15s(stiff/NDF). But the problem was not solved. I have posted the desired system image with algorithm codes for friends.
CODE:
function bestSolution = EWOA_Run_PID()
% Step 1: Initialize the input arguments for EWOA
nVar = 6;
ub = [50 50 50 50 50 50];
lb = [0.01 0.01 0.01 0.01 0.01 0.01];
MaxIter = 50;
PopulationSize = 50;
% Step 2: Define the objective function handle using EWAO_Cost_PID_CF_CE
objFcn = @(params) EWOA_Cost_PID(params);
% Step 3: Call the EWOA function
bestSolution = EWOA(nVar, lb, ub, objFcn, MaxIter, PopulationSize);
end
function bestSolution = EWOA(nVar, lb, ub, objFcn, MaxIter, PopulationSize)
% Step 1: Initialize the population
whalesPositions = bsxfun(@plus, lb, bsxfun(@times, rand(PopulationSize, nVar), (ub – lb)));
% Step 2: Evaluate the objective function for each whale
whalesCost = zeros(PopulationSize, 1);
for i = 1:PopulationSize
whalesCost(i) = objFcn(whalesPositions(i, :));
end
% Step 3: Set the initial best position and cost
[globalBestCost, gBestIndex] = min(whalesCost);
globalBestPosition = whalesPositions(gBestIndex, :);
% Step 4: Initialize elite whale
eliteWhalePosition = globalBestPosition;
eliteWhaleCost = globalBestCost;
% Step 5: Set the termination criterion parameters
maxIterWithoutImprovement = 50; % Adjust this value as needed
iterWithoutImprovement = 50;
% Main loop
for iter = 1:MaxIter
a = 2 – iter * ((2) / MaxIter); % Parameter to control the search space reduction
for i = 1:PopulationSize
% Generate random vectors
r1 = rand(1, nVar);
r2 = rand(1, nVar);
% Calculate the A and C coefficients
A = 2 * a * r1 – a;
C = 2 * r2;
% Update the position of the whales using the WOA operators
D = abs(C .* globalBestPosition – whalesPositions(i, :));
newWhalePosition = globalBestPosition – A .* D;
% Clip positions to stay within the bounds
newWhalePosition = max(newWhalePosition, lb);
newWhalePosition = min(newWhalePosition, ub);
% Evaluate the new position using the EWAO_Cost_PID_CF_CE function
newWhaleCost = EWOA_Cost_PID(newWhalePosition);
% Update the whale’s position and cost if a better solution is found
if newWhaleCost < whalesCost(i)
whalesPositions(i, 🙂 = newWhalePosition;
whalesCost(i) = newWhaleCost;
end
% Update the global best position and cost if a better solution is found
if whalesCost(i) < globalBestCost
globalBestCost = whalesCost(i);
globalBestPosition = whalesPositions(i, :);
end
end
% Check for termination criterion
if globalBestCost < eliteWhaleCost
eliteWhalePosition = globalBestPosition;
eliteWhaleCost = globalBestCost;
iterWithoutImprovement = 0;
else
iterWithoutImprovement = iterWithoutImprovement + 1;
end
% Display the best cost at each iteration
disp([‘Iteration ‘, num2str(iter), ‘: Best Cost = ‘, num2str(eliteWhaleCost)]);
% Check if termination criterion is met
if iterWithoutImprovement >= maxIterWithoutImprovement
break;
end
end
% Output the best solution found
bestSolution = eliteWhalePosition;
end
Code
function cost = EWOA_Cost_PID(params)
% Assign parameters to the base workspace using assignin
assignin(‘base’, ‘KP’, params(1));
assignin(‘base’, ‘KI’, params(2));
assignin(‘base’, ‘KD’, params(3));
assignin(‘base’, ‘KPP’, params(4));
assignin(‘base’, ‘KII’, params(5));
assignin(‘base’, ‘KDD’, params(6));
% Run the Simulink model
sim(‘two_area_LFC_PID’);
% Calculate penalties based on maximum absolute values of signals
g1 = max(abs(f1.signals.values)) – 0.25;
g2 = max(abs(f2.signals.values)) – 0.25;
g3 = max(abs(pt.signals.values)) – 0.05;
% Define a large penalty coefficient
lambda = 10^20;
% Compute penalties for constraint violations
Penalty1 = lambda * g1^2 * GetInequality(g1);
Penalty2 = lambda * g2^2 * GetInequality(g2);
Penalty3 = lambda * g3^2 * GetInequality(g3);
% Calculate the total cost
cost = J_obj.signals.values(end) + Penalty1 + Penalty2 + Penalty3;
end I have simulated a system in Simulink MATLAB that I want to optimize with the PID controller.
To find the best controller coefficients, I use WOA optimization method. But when I run it, I get the following error “Derivative of state ‘1’ in block ‘two_area_LFC_PID/Integrator9’ at time 8.820174943555763 is not finite. The simulation will be stopped. There may be a singularity in the solution. If not, try reducing the step size (either by reducing the fixed step size or by tightening the error tolerances)”. Is there a solution to my problem? I have changed the step size. I also changed the solver to ODE 15s(stiff/NDF). But the problem was not solved. I have posted the desired system image with algorithm codes for friends.
CODE:
function bestSolution = EWOA_Run_PID()
% Step 1: Initialize the input arguments for EWOA
nVar = 6;
ub = [50 50 50 50 50 50];
lb = [0.01 0.01 0.01 0.01 0.01 0.01];
MaxIter = 50;
PopulationSize = 50;
% Step 2: Define the objective function handle using EWAO_Cost_PID_CF_CE
objFcn = @(params) EWOA_Cost_PID(params);
% Step 3: Call the EWOA function
bestSolution = EWOA(nVar, lb, ub, objFcn, MaxIter, PopulationSize);
end
function bestSolution = EWOA(nVar, lb, ub, objFcn, MaxIter, PopulationSize)
% Step 1: Initialize the population
whalesPositions = bsxfun(@plus, lb, bsxfun(@times, rand(PopulationSize, nVar), (ub – lb)));
% Step 2: Evaluate the objective function for each whale
whalesCost = zeros(PopulationSize, 1);
for i = 1:PopulationSize
whalesCost(i) = objFcn(whalesPositions(i, :));
end
% Step 3: Set the initial best position and cost
[globalBestCost, gBestIndex] = min(whalesCost);
globalBestPosition = whalesPositions(gBestIndex, :);
% Step 4: Initialize elite whale
eliteWhalePosition = globalBestPosition;
eliteWhaleCost = globalBestCost;
% Step 5: Set the termination criterion parameters
maxIterWithoutImprovement = 50; % Adjust this value as needed
iterWithoutImprovement = 50;
% Main loop
for iter = 1:MaxIter
a = 2 – iter * ((2) / MaxIter); % Parameter to control the search space reduction
for i = 1:PopulationSize
% Generate random vectors
r1 = rand(1, nVar);
r2 = rand(1, nVar);
% Calculate the A and C coefficients
A = 2 * a * r1 – a;
C = 2 * r2;
% Update the position of the whales using the WOA operators
D = abs(C .* globalBestPosition – whalesPositions(i, :));
newWhalePosition = globalBestPosition – A .* D;
% Clip positions to stay within the bounds
newWhalePosition = max(newWhalePosition, lb);
newWhalePosition = min(newWhalePosition, ub);
% Evaluate the new position using the EWAO_Cost_PID_CF_CE function
newWhaleCost = EWOA_Cost_PID(newWhalePosition);
% Update the whale’s position and cost if a better solution is found
if newWhaleCost < whalesCost(i)
whalesPositions(i, 🙂 = newWhalePosition;
whalesCost(i) = newWhaleCost;
end
% Update the global best position and cost if a better solution is found
if whalesCost(i) < globalBestCost
globalBestCost = whalesCost(i);
globalBestPosition = whalesPositions(i, :);
end
end
% Check for termination criterion
if globalBestCost < eliteWhaleCost
eliteWhalePosition = globalBestPosition;
eliteWhaleCost = globalBestCost;
iterWithoutImprovement = 0;
else
iterWithoutImprovement = iterWithoutImprovement + 1;
end
% Display the best cost at each iteration
disp([‘Iteration ‘, num2str(iter), ‘: Best Cost = ‘, num2str(eliteWhaleCost)]);
% Check if termination criterion is met
if iterWithoutImprovement >= maxIterWithoutImprovement
break;
end
end
% Output the best solution found
bestSolution = eliteWhalePosition;
end
Code
function cost = EWOA_Cost_PID(params)
% Assign parameters to the base workspace using assignin
assignin(‘base’, ‘KP’, params(1));
assignin(‘base’, ‘KI’, params(2));
assignin(‘base’, ‘KD’, params(3));
assignin(‘base’, ‘KPP’, params(4));
assignin(‘base’, ‘KII’, params(5));
assignin(‘base’, ‘KDD’, params(6));
% Run the Simulink model
sim(‘two_area_LFC_PID’);
% Calculate penalties based on maximum absolute values of signals
g1 = max(abs(f1.signals.values)) – 0.25;
g2 = max(abs(f2.signals.values)) – 0.25;
g3 = max(abs(pt.signals.values)) – 0.05;
% Define a large penalty coefficient
lambda = 10^20;
% Compute penalties for constraint violations
Penalty1 = lambda * g1^2 * GetInequality(g1);
Penalty2 = lambda * g2^2 * GetInequality(g2);
Penalty3 = lambda * g3^2 * GetInequality(g3);
% Calculate the total cost
cost = J_obj.signals.values(end) + Penalty1 + Penalty2 + Penalty3;
end error, matlab, solve, derivative of state ‘1’ in block MATLAB Answers — New Questions