wind speed and wind power forecasting
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
please, I want a code for wind speed forecasting in a wind farm using ANN and Marcov Chain or pso
trainning ANN using Marcov Chain or pso or any method
1 comentario
Respuestas (1)
Aditya
el 14 de Jul. de 2025
Hi Eng,
Below is an example of how you can forecast wind speed in a wind farm using an Artificial Neural Network (ANN) in MATLAB, and train the ANN using Particle Swarm Optimization (PSO).
Following is the sample code for the same:
% Wind Speed Forecasting using ANN trained by PSO
% -------------------------------------------------
% Requirements:
% - MATLAB Neural Network Toolbox
% - PSO function (provided below)
clc; clear; close all;
%% 1. Generate Synthetic Data (Replace with your own data)
N = 500; % Number of samples
t = (1:N)';
wind_speed = 8 + 2*sin(2*pi*t/24) + randn(N,1); % Example: daily pattern + noise
% Prepare input/output pairs for time series forecasting
input_lag = 3; % Number of past values to use
X = [];
Y = [];
for i = input_lag+1:N
X = [X; wind_speed(i-input_lag:i-1)'];
Y = [Y; wind_speed(i)];
end
% Normalize data
[Xn, xPS] = mapminmax(X',0,1); Xn = Xn';
[Yn, yPS] = mapminmax(Y',0,1); Yn = Yn';
% Split into training/testing
train_ratio = 0.8;
idx = round(train_ratio*size(Xn,1));
X_train = Xn(1:idx,:);
Y_train = Yn(1:idx,:);
X_test = Xn(idx+1:end,:);
Y_test = Yn(idx+1:end,:);
%% 2. ANN Architecture
input_size = input_lag;
hidden_size = 10;
output_size = 1;
% ANN weight vector: [IW(:); b1(:); LW(:); b2(:)]
num_weights = hidden_size*input_size + hidden_size + output_size*hidden_size + output_size;
%% 3. PSO Parameters
n_particles = 30;
max_iter = 100;
lb = -2*ones(1,num_weights); % Lower bound
ub = 2*ones(1,num_weights); % Upper bound
%% 4. PSO Optimization
fitnessFcn = @(w) ann_fitness(w, X_train, Y_train, input_size, hidden_size, output_size);
% Run PSO (see function below)
[best_w, best_fitness] = pso(fitnessFcn, num_weights, n_particles, max_iter, lb, ub);
%% 5. Test Trained ANN
Y_pred = ann_predict(best_w, X_test, input_size, hidden_size, output_size);
% Denormalize
Y_pred_dn = mapminmax('reverse', Y_pred', yPS)';
Y_test_dn = mapminmax('reverse', Y_test', yPS)';
% Performance
rmse = sqrt(mean((Y_pred_dn - Y_test_dn).^2));
fprintf('Test RMSE: %.4f\n', rmse);
% Plot
figure;
plot(Y_test_dn,'b','LineWidth',1.5); hold on;
plot(Y_pred_dn,'r--','LineWidth',1.5);
legend('Actual','Predicted');
xlabel('Sample'); ylabel('Wind Speed (m/s)');
title('Wind Speed Forecasting using ANN-PSO');
%% --- FUNCTIONS ---
function mse = ann_fitness(w, X, Y, input_size, hidden_size, output_size)
Y_hat = ann_predict(w, X, input_size, hidden_size, output_size);
mse = mean((Y_hat - Y).^2);
end
function Y_hat = ann_predict(w, X, input_size, hidden_size, output_size)
% Extract weights
idx = 0;
IW = reshape(w(1:hidden_size*input_size), hidden_size, input_size);
idx = idx + hidden_size*input_size;
b1 = reshape(w(idx+1:idx+hidden_size), hidden_size, 1);
idx = idx + hidden_size;
LW = reshape(w(idx+1:idx+output_size*hidden_size), output_size, hidden_size);
idx = idx + output_size*hidden_size;
b2 = reshape(w(idx+1:idx+output_size), output_size, 1);
% Forward pass
H = tansig(X*IW' + repmat(b1', size(X,1),1));
Y_hat = H*LW' + repmat(b2', size(X,1),1);
end
% Simple PSO implementation
function [gbest, gbestval] = pso(fitnessfcn, ndim, npop, maxiter, lb, ub)
w = 0.7; c1 = 1.5; c2 = 1.5;
x = repmat(lb, npop, 1) + rand(npop, ndim) .* (repmat(ub-lb, npop, 1));
v = zeros(npop, ndim);
pbest = x; pbestval = arrayfun(@(i) fitnessfcn(x(i,:)), 1:npop)';
[gbestval, idx] = min(pbestval); gbest = x(idx,:);
for iter = 1:maxiter
for i = 1:npop
v(i,:) = w*v(i,:) + c1*rand(1,ndim).*(pbest(i,:)-x(i,:)) + c2*rand(1,ndim).*(gbest-x(i,:));
x(i,:) = x(i,:) + v(i,:);
x(i,:) = max(min(x(i,:),ub),lb); % Clamp
fval = fitnessfcn(x(i,:));
if fval < pbestval(i)
pbest(i,:) = x(i,:);
pbestval(i) = fval;
if fval < gbestval
gbest = x(i,:);
gbestval = fval;
end
end
end
if mod(iter,10)==0
fprintf('Iter %d, Best Fitness: %.5f\n', iter, gbestval);
end
end
end
0 comentarios
Ver también
Categorías
Más información sobre Deep Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!