how PID Tuning for Pitch Angle Control using PSO

Hello,
I am working on a control system that includes a pitch angle control loop using a PID controller, and I want to tune the PID gains using Particle Swarm Optimization (PSO).
I am looking for a MATLAB (or Simulink) code that meets the following requirements:
Uses PSO to optimize the PID parameters (Kp, Ki, Kd)
Supports defining bounds or constraints on the PID gains
If anyone has a working example or template that I could build upon, I would greatly appreciate it.
Thank you in advance!

6 Comments

Since you already have the equations of motion for the pitch angle, it would be helpful if you could demonstrate how basic control theory is applied to design a simple PID controller in MATLAB code. Your PID gains do not need to be optimal, as long as the control system remains stable with your chosen gains.

PSO experts/users in the forum can extend your code by simply incorporating the PSO component to demonstrate how to optimize the PID gains. This approach saves users the time required to search for textbook materials and to write the code for the pitch angle model and the PID controller components.

@Sam Chak Thank you for your valuable contribution.
As you mentioned, since the equations of motion for the pitch angle are available, it's indeed very helpful to demonstrate how basic control theory can be applied to design a simple PID controller.
However, after trying several different files, the simulation unfortunately did not work as expected on my end. The issue might be related to a variable mismatch or a system configuration within the files.
I sincerely appreciate any further guidance or clarification that could help resolve this issue.
@douara ben ouadeh, Most mechanical motions can be modeled as a 2nd-order transfer function or differential equation. How did the variable mismatch occur?
Transfer function
Differential equation
where is the control input.
Thank you for the clarification and for your time.
To better explain what I’m aiming for, please refer to the following link:
@douara ben ouadeh, The user-supplied PSO code shared in the provided link is incomplete, preventing any testing. It is not surprising that you are experiencing issues. Have you tried using MATLAB built-in particleswarm()?
%% call PSO
fun = @costfcn;
nvars = 1;
lb = 0;
ub = 2;
Kbest = particleswarm(fun, nvars, lb, ub)
Optimization ended: relative change in the objective value over the last OPTIONS.MaxStallIterations iterations is less than OPTIONS.FunctionTolerance.
Kbest = 1.0000
%% Cost function
function J = costfcn(K)
J = (K - 1)^2;
end

Sign in to comment.

Answers (0)

Asked:

on 16 May 2025

Commented:

on 30 May 2025

Community Treasure Hunt

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

Start Hunting!