I want to use a Genetic Algorithm to predict the thickness value. Currently, my code runs and produces results where it hits upper bound and lower bound.
Show older comments
clear; clc;
%% 1) Experimental Data
time = [7200 14400 21600];
temp = [1123 1173 1223];
thick_exp = [16.83 24.51 31.09;
26.28 37.98 49.91;
41.56 59.12 69.01];
R = 8.314; % Gas constant (J/mol·K)
%% 2) Convert matrix to vector
[temp_grid,time_grid] = ndgrid(temp,time);
time_vector = time_grid(:);
temp_vector = temp_grid(:);
thick_exp_vector = thick_exp(:);
%% 3) Define Kinetic Model
% thickness = k * sqrt(t)
% k = k0 * exp(-Q/RT)
kineticModel = @(params,t,T) ...
sqrt( t .* params(1) .* exp(-params(2).*1000 ./ (8.314 .* T)) );
%% 4) Define RMSE Fitness Function
fitnessFcn = @(params) ...
sqrt(mean((thick_exp_vector - kineticModel(params,time_vector,temp_vector)).^2));
%% 5) Define Bounds [k0 Q]
lb = [1e2 50000];
ub = [1e8 300000];
%% 6) GA Options
options = optimoptions('ga',...
'PopulationSize',100,...
'MaxGenerations',200,...
'Display','iter');
%% 7) Run Genetic Algorithm
[nParams,fval] = ga(fitnessFcn,2,[],[],[],[],lb,ub,[],options);
thick_pred = kineticModel(nParams,time_vector,temp_vector);
MAPE = mean(abs((thick_exp_vector - thick_pred) ./ thick_exp_vector)) * 100;
%% 8) Display Results
disp(['Q = ',num2str(nParams(2)),' J/mol'])
disp(['k0 = ',num2str(nParams(1))])
disp(['RMSE = ',num2str(fval)])
disp(['MAPE = ',num2str(MAPE),' %'])
Answers (1)
Star Strider
on 13 Feb 2026
Edited: Star Strider
on 13 Feb 2026
The reason is that the optimal values for the 'k0' and 'Q' parameter values are far outside the bounds that you set for them. Unconstrained, 'k0' is a bit less than 80, and 'Q' is a bit less than 400.
I am not certain what you are doing, so I cannot comment on the accuracy of your 'kineticModel' function. However that is where I would look in order to understand the reason the optimal estimated parameters are far outside the bounds that you set for them.
Example --
clear; clc;
%% 1) Experimental Data
time = [7200 14400 21600];
temp = [1123 1173 1223];
thick_exp = [16.83 24.51 31.09;
26.28 37.98 49.91;
41.56 59.12 69.01];
R = 8.314; % Gas constant (J/mol·K)
%% 2) Convert matrix to vector
[temp_grid,time_grid] = ndgrid(temp,time);
time_vector = time_grid(:);
temp_vector = temp_grid(:);
thick_exp_vector = thick_exp(:);
%% 3) Define Kinetic Model
% thickness = k * sqrt(t)
% k = k0 * exp(-Q/RT)
kineticModel = @(params,t,T) ...
sqrt( t .* params(1) .* exp(-params(2).*1000 ./ (8.314 .* T)) );
%% 4) Define RMSE Fitness Function
fitnessFcn = @(params) ...
sqrt(mean((thick_exp_vector - kineticModel(params,time_vector,temp_vector)).^2));
%% 5) Define Bounds [k0 Q]
lb = [1e2 50000];
ub = [1e8 300000];
lb = [];
ub = [];
%% 6) GA Options
options = optimoptions('ga',...
'PopulationSize',100,...
'MaxGenerations',200,...
'Display','iter');
%% 7) Run Genetic Algorithm
[nParams,fval] = ga(fitnessFcn,2,[],[],[],[],lb,ub,[],options);
thick_pred = kineticModel(nParams,time_vector,temp_vector);
MAPE = mean(abs((thick_exp_vector - thick_pred) ./ thick_exp_vector)) * 100;
thick_pred_mtx = reshape(thick_pred,numel(time), numel(temp)).'; % Used in 'surf' Plot
%% 8) Display Results
disp(['Q = ',num2str(nParams(2)),' J/mol'])
disp(['k0 = ',num2str(nParams(1))])
disp(['RMSE = ',num2str(fval)])
disp(['MAPE = ',num2str(MAPE),' %'])
figure
surf(time, temp, thick_exp, DisplayName='Data')
hold on
surf(time_grid, temp_grid, thick_pred_mtx, FaceAlpha=0.5, EdgeColor='r', DisplayName='Function Fit')
hold off
xlabel('Time')
ylabel('Temperature (K)')
zlabel('Thickness')
title('Unconstrained Parameter Fit')
legend(Location='best')
view(40,30)
.
EDIT -- (13 Feb 2026 at 18:25)
The transposed 'thick_pred_mtx' seems to more closely approximate the data than if it is not transposed.
.
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!