Drones flying between city buildings, around offshore turbines, or over ridges run into sudden wind gusts and wake turbulence that linear controllers handle poorly. A tuned PID or LQR loop works fine in calm air, then starts oscillating or losing altitude as soon as airflow turns nonlinear. On the other hand, pure black-box neural networks need huge amounts of flight data and still break down when they see a gust stronger than anything in their training logs.
Physics-Informed Neural Networks (PINNs) offer a practical middle ground in MATLAB and Simulink. Instead of fitting a neural network to sensor logs alone, you embed Newton-Euler rigid-body dynamics directly into the training loss. That lets the network estimate unknown wind forces from sparse IMU and altitude data without a wind tunnel.
Why black-box neural networks struggle with UAV aerodynamics
Training a standard multilayer perceptron directly on flight-log input-output pairs usually hits three walls in aerospace and mechanical work. First, a network trained in 4 m/s wind has no physical guardrails when it encounters 11 m/s wind shear, so its predictions can diverge. Second, collecting clean 6-DOF aerodynamic force labels requires motion-capture rooms or wind-tunnel runs that most university labs cannot afford for every airframe change. Third, an unconstrained network can predict non-physical drag or negative damping, which makes a closed-loop flight controller unstable.
Embedding UAV dynamics into the PINN loss function
Take the vertical and translational motion of a multi-rotor UAV of mass m under gravity g, total rotor thrust uthrust(t), linear drag coefficient cd, and an unknown wind-gust force Fwind(t):
rphysics(t) = m · z̈θ(t) − [ uthrust(t) − m·g − cd · żθ(t) + F̂wind,θ(t) ]
With trainable network weights θ, the PINN minimizes a combined loss computed via automatic differentiation (dlgradient in MATLAB):
Ldata(θ) = (1 / Nd) · Σ | zθ(ti) − zmeas(ti) |2
Lphysics(θ) = (1 / Nc) · Σ | rphysics(tj) |2
In this setup, Nd is a small set of noisy altitude measurements, while Nc is a grid of collocation points across the flight window where the differential equation residual rphysics(t) is driven toward zero. Because the network outputs both the smoothed altitude zθ(t) and the latent wind force F̂wind,θ(t) at the same time, it works as a physics-constrained disturbance observer.
Closed-loop wind-gust rejection
Once the PINN estimates the gust force, you feed that estimate into a standard PD controller to cancel the wind before it pushes the drone off target:
Canceling Fwind(t) in the feedforward path means the feedback loop does not need high integral gains, which helps prevent motor saturation and frame vibration.
Comparing EKF, black-box neural networks, and PINNs
| Criteria | Classical EKF observer | Black-box neural net | PINN-augmented control |
|---|---|---|---|
| Training data needed | None (model-based) | High (dense flight logs) | Low (sparse logs + ODE residual) |
| Unseen wind-gust extrapolation | Moderate (linearized around trim) | Weak outside training range | Strong (guided by rigid-body physics) |
| Nonlinear drag modeling | Manual Jacobian updates | Learned from data | Learned with physical constraints |
| MATLAB workflow | extendedKalmanFilter |
trainnet |
dlfeval + dlgradient |
How the MATLAB simulation works
The MATLAB script below uses Deep Learning Toolbox (dlarray, dlfeval, dlgradient, and adamupdate) to run a complete hover test. First, it simulates a 1.8 kg quadrotor holding 5 m while hit by a combined step-and-sinusoidal wind gust between 2 s and 7 s. Next, it trains a 3-layer tanh network on 60 noisy altitude samples and 120 physics collocation points. Finally, it injects the estimated wind force into the PD loop and plots both altitude tracking RMSE and the recovered wind profile against the baseline controller.
Ideas for thesis and lab extensions
If you are adapting this for a capstone or PhD project, you can replace the single-axis residual with full 6-DOF rotational Euler equations and blade-flapping moments in Simulink. You can also export the trained network weights with MATLAB Coder or Simulink Coder for Hardware-in-the-Loop (HIL) testing on a Pixhawk controller, or apply the same residual formulation to estimate lift and drag shifts during eVTOL hover-to-cruise transitions.
Need help with UAV PINNs or Simulink HIL models?
If you are working on physics-informed flight control, debugging dlgradient residuals, or building a 6-DOF Simulink and PX4 model, our engineers at MatlabSolutions.com can help with custom code and simulation walkthroughs. Request custom MATLAB & UAV simulation help.
Executable MATLAB Script & Model Setup
%% Physics-Informed Neural Network (PINN) for UAV Wind-Gust Rejection
% Requirements: MATLAB + Deep Learning Toolbox (dlarray, dlgradient, adamupdate)
clear; clc; close all;
rng(42);
%% 1. UAV Physical Parameters & Wind-Gust Profile
m = 1.8; % Quadrotor mass [kg]
g = 9.81; % Gravity [m/s^2]
cd = 0.25; % Linear aerodynamic drag coeff [N*s/m]
z_ref = 5.0; % Target hover altitude [m]
% Time grid
dt = 0.02;
t_vec = (0:dt:10)';
N = length(t_vec);
% True unknown wind gust force [N]: sudden shear + turbulence
F_wind_true = @(t) (t >= 2.0 & t Outputs: [z_hat, F_wind_hat])
layers = [1, 32, 32, 32, 2];
params = struct();
for i = 1:length(layers)-1
in_sz = layers(i); out_sz = layers(i+1);
scale = sqrt(2 / (in_sz + out_sz));
params.("W"+i) = dlarray(scale * randn(out_sz, in_sz));
params.("b"+i) = dlarray(zeros(out_sz, 1));
end
%% 5. Train PINN Using Adam + Automatic Differentiation (dlgradient)
numEpochs = 1500;
lr = 0.008;
trailingAvg = []; trailingAvgSq = [];
lossHist = zeros(numEpochs, 1);
for epoch = 1:numEpochs
[loss, grads] = dlfeval(@pinnLoss, params, dl_t_data, dl_z_data, ...
dl_t_phys, dl_u_phys, m, g, cd);
[params, trailingAvg, trailingAvgSq] = adamupdate(params, grads, ...
trailingAvg, trailingAvgSq, epoch, lr);
lossHist(epoch) = extractdata(loss);
if mod(epoch, 300) == 0
fprintf('Epoch %4d | Composite Physics+Data Loss: %.5f\n', epoch, lossHist(epoch));
end
end
%% 6. Evaluate PINN Wind Estimate & Run PINN-Compensated Controller
dl_t_all = dlarray(t_vec', 'CB');
out_all = forwardPINN(params, dl_t_all);
F_wind_est = extractdata(out_all(2, :))';
z_pinn = zeros(N,1); vz_pinn = zeros(N,1);
z_pinn(1) = z_ref;
for k = 1:N-1
err = z_ref - z_pinn(k);
derr = 0 - vz_pinn(k);
% Feedforward rejection using PINN wind-gust observer
u_cmd = Kp*err + Kd*derr + m*g - F_wind_est(k);
u_cmd = max(0, min(40, u_cmd));
az = (u_cmd - m*g - cd*vz_pinn(k) + F_wind_true(t_vec(k))) / m;
vz_pinn(k+1) = vz_pinn(k) + az*dt;
z_pinn(k+1) = z_pinn(k) + vz_pinn(k+1)*dt;
end
%% 7. Quantitative Benchmark & Visualization
rmse_pd = sqrt(mean((z_pd - z_ref).^2));
rmse_pinn = sqrt(mean((z_pinn - z_ref).^2));
fprintf('\n--- Benchmark Results ---\n');
fprintf('Baseline PD Altitude RMSE : %.4f m\n', rmse_pd);
fprintf('PINN-Augmented Altitude RMSE: %.4f m (%.1f%% reduction)\n', ...
rmse_pinn, 100*(rmse_pd - rmse_pinn)/rmse_pd);
figure('Color','w','Position',[100 100 950 620]);
subplot(2,1,1);
plot(t_vec, z_pd, 'r--', 'LineWidth', 1.8); hold on;
plot(t_vec, z_pinn, 'b-', 'LineWidth', 2.0);
yline(z_ref, 'k:', 'LineWidth', 1.2);
grid on; ylabel('Altitude z [m]');
title('UAV Hover Altitude Under Sudden Wind Gust (2s - 7s)');
legend('Baseline PD', 'PINN-Compensated Controller', 'Reference (5 m)', 'Location', 'best');
subplot(2,1,2);
plot(t_vec, F_wind_true(t_vec), 'k-', 'LineWidth', 2.0); hold on;
plot(t_vec, F_wind_est, 'g--', 'LineWidth', 1.8);
grid on; xlabel('Time [s]'); ylabel('Wind Force [N]');
title('PINN Aerodynamic Wind-Gust Disturbance Estimation');
legend('True Wind Gust F_{wind}(t)', 'PINN Estimated F_{wind}', 'Location', 'best');
%% --- Helper Functions ---
function out = forwardPINN(params, t)
% Normalize time input to [-1, 1] for stable tanh gradients
x = (t / 5.0) - 1.0;
numLayers = numel(fieldnames(params)) / 2;
for i = 1:numLayers-1
x = tanh(params.("W"+i) * x + params.("b"+i));
end
out = params.("W"+numLayers) * x + params.("b"+numLayers);
end
function [loss, grads] = pinnLoss(params, t_d, z_d, t_p, u_p, m, g, cd)
% 1. Data loss on sparse sensor measurements
pred_d = forwardPINN(params, t_d);
loss_data = mean((pred_d(1,:) - z_d).^2);
% 2. Physics residual loss via automatic differentiation (dz/dt, d2z/dt2)
pred_p = forwardPINN(params, t_p);
z_p = pred_p(1,:);
F_est = pred_p(2,:);
dz_dt = dlgradient(sum(z_p), t_p, 'EnableHigherDerivatives', true);
d2z_dt2 = dlgradient(sum(dz_dt), t_p, 'EnableHigherDerivatives', true);
residual = m * d2z_dt2 - (u_p - m*g - cd*dz_dt + F_est);
loss_phys = mean(residual.^2);
% 3. Smoothness regularization on latent wind force
dF_dt = dlgradient(sum(F_est), t_p, 'EnableHigherDerivatives', false);
loss_reg = 0.01 * mean(dF_dt.^2);
loss = loss_data + 0.05 * loss_phys + loss_reg;
grads = dlgradient(loss, params);
end
Related Verified MATLAB & Simulink Projects
Need pre-built, debugged Simulink models with complete parameter initialization scripts and documentation? Explore top related solutions:
Common Engineering Troubleshooting & Q&A
Frequently encountered bugs, solver convergence issues, and implementation questions answered by our engineering mentors:
Recommended Engineering Articles
Need Custom MATLAB / Simulink Implementation?
Our team of PhD engineers build custom simulation plants, train machine learning agents, tune PID/MPC controllers, and deliver complete, executable code with Turnitin reports.