To generate ANFIS
data = readmatrix('[Link]');
% Remove rows containing NaN
data = rmmissing(data);
input = data(:,1:3);
output = data(:,4);
opt = genfisOptions("GridPartition");
[Link] = 3;
fis = genfis(input, output, opt);
[trainedFIS, trainError] = anfis([input output], fis, ...
[100 0 0.01 0.9 1.1]);
writefis(trainedFIS,'Ra_prediction_fis');
predictedRa = evalfis(trainedFIS,input);
figure;
plot(output,'b-o','LineWidth',2);
hold on;
plot(predictedRa,'r-s','LineWidth',2);
xlabel('Sample Number');
ylabel('Ra');
legend('Actual','Predicted');
grid on;
rmse = sqrt(mean((output-predictedRa).^2));
fprintf('RMSE = %.4f\n', rmse);
x.1 ANFIS Model Development
This MATLAB code develops an Adaptive Neuro-Fuzzy Inference System
(ANFIS) model to predict surface roughness ( Ra ). First, the dataset is
imported from the Excel file [Link], and rows containing missing
values (NaN) are removed. The first three columns are considered input
variables, while the fourth column is taken as the target surface roughness.
An initial fuzzy inference system is generated using the grid-partitioning
method with three membership functions assigned to each input. This initial
system is then trained using ANFIS for 100 epochs to establish the nonlinear
relationship between the machining parameters and surface roughness. The
trained fuzzy inference system is saved as Ra_prediction_fis. It is
subsequently used to predict Ra for the input dataset, and the actual and
predicted values are plotted for visual comparison. Finally, the model’s
prediction accuracy is evaluated by calculating the root mean square error
(RMSE) between the experimental and predicted surface roughness values.
To find Objective fn
function err = objectiveRa(x, Ra_target)
% Design variables
SS = round(x(1)); % rpm
FR = x(2); % mm/min
DoC = x(3); % mm
% Send variables to Simulink workspace
assignin('base','SS',SS);
assignin('base','FR',FR);
assignin('base','DoC',DoC);
% Run Simulink model
simOut = sim('newa','StopTime','1');
% Get predicted Ra from To Workspace block
Ra_pred = [Link](end);
% Objective function (minimize error)
err = (Ra_pred - Ra_target)^2;
end
x.2 Surface Roughness Objective Function
This MATLAB function defines the objective function used to identify
machining parameters that produce a desired target surface roughness, Ra .
The function receives a candidate solution vector x and the required surface
roughness R a target. The three elements of x represent spindle speed (SS), feed
rate (FR), and depth of cut (DoC), respectively. The spindle speed is rounded
to the nearest integer because it is specified in rpm. These variables are
transferred to the MATLAB base workspace and supplied to the Simulink
model named newa. The model is simulated for one second, and the final
predicted surface roughness value is extracted from the simout output.
Finally, the squared difference between the predicted and target surface
roughness values is calculated. An optimization algorithm can minimize this
error to determine the combination of spindle speed, feed rate, and depth of
cut that gives a predicted Ra closest to the specified target.
Optimization Function
function err = objectiveRa(x, Ra_target)
% Design variables
SS = round(x(1)); % rpm
FR = x(2); % mm/min
DoC = x(3); % mm
% Send variables to Simulink workspace
assignin('base','SS',SS);
assignin('base','FR',FR);
assignin('base','DoC',DoC);
% Run Simulink model
simOut = sim('newa','StopTime','1');
% Get predicted Ra from To Workspace block
Ra_pred = [Link](end);
% Objective function (minimize error)
err = (Ra_pred - Ra_target)^2;
end
x.3 Machining Parameter Optimization
This MATLAB function serves as an objective function for optimizing
machining parameters to achieve a specified target surface roughness ( Ra ). It
accepts a candidate vector x , containing spindle speed (SS), feed rate (FR),
and depth of cut (DoC), along with the desired surface roughness R a target. The
spindle speed is rounded to the nearest integer, while the feed rate and
depth of cut retain their continuous values. These parameters are transferred
to the MATLAB base workspace using assignin, allowing them to be accessed
by the Simulink model newa. The model is then simulated for one second,
and the final predicted surface roughness is obtained from the simout
variable generated by a To Workspace block. The function calculates the
squared difference between the predicted and target Ra values and returns it
as the objective error. During optimization, this error is minimized to
determine the machining-parameter combination that produces a surface
roughness closest to the desired value.