0% found this document useful (0 votes)
61 views

Surface Generation MATLAB Code

Uploaded by

kishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Surface Generation MATLAB Code

Uploaded by

kishan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

2D Surface Generation

close all;
clear;
clc;
% Define parameters
length = 10; % Length in mm
Ra_target = 0.6; % Target Ra value in microns
Rq_target = 0.76; % Target Rq (RMS) value in microns
Rsk_target = 0.05; % Target Rsk (skewness) value in microns
Rku_target = 3.45; % Target Rku (kurtosis) value in microns

% Convert length to meters


length_m = length ; % Length in meters

% Generate random surface profile


rng('default'); % Reset the random number generator
x = linspace(0, length_m, 1000); % x-coordinates along the length
y = randn(1, 1000); % Random surface profile with mean 0 and standard deviation 1

% Scale the surface to achieve the desired roughness parameters


Ra_current = mean(abs(y));
Rq_current = sqrt(mean(y.^2));
Rsk_current = skewness(y);
Rku_current = kurtosis(y);

y = y * (Ra_target / Ra_current);
y = y * (Rq_target / Rq_current);
y = y + (Rsk_target - Rsk_current);
y = y + (Rku_target - Rku_current);

% Calculate roughness parameters of the generated surface


Ra = mean(abs(y)); % Arithmetic mean roughness
Rq = sqrt(mean(y.^2)); % Root mean square roughness
Rsk = skewness(y); % Skewness
Rku = kurtosis(y); % Kurtosis

% Display the achieved roughness parameters


fprintf('Achieved Roughness Parameters:\n');
fprintf('Ra: %.2f microns\n', Ra);
fprintf('Rq: %.2f microns\n', Rq);
fprintf('Rsk: %.2f microns\n', Rsk);
fprintf('Rku: %.2f microns\n', Rku);

% Plot the surface profile


figure;
plot(x, y);
xlabel('Length (mm)');
ylabel('Amplitude (um)');
title('genrated Surface Profile');
grid on;

3D Surface Generation
close all;
clear;
clc;
% Set the random seed for reproducibility
rng(1234);

% Parameters
width = 100; % Width of the surface
height = 100; % Height of the surface
scale = 0.1; % Scale of the surface
octaves = 3; % Number of octaves
persistence = 0.5; % Persistence of the noise
defectX = 50; % X-coordinate of the defect
defectY = 50; % Y-coordinate of the defect
defectRadius = 1; % Radius of the defective area
defectAmplitude = 1; % Amplitude of the defect
smoothingRadius = 2; % Radius of the smoothing filter

% Generate Perlin noise


noise = zeros(height, width);
for octave = 1:octaves
frequency = 2^(octave - 1);
amplitude = persistence^(octave - 1);

[X, Y] = meshgrid(1:width, 1:height);


X = X * frequency * scale;
Y = Y * frequency * scale;
noise = noise + amplitude * interp2(randn(height, width), X, Y, 'linear', 0);
end

% Normalize the noise to [0, 1]


noise = (noise - min(noise(:))) / (max(noise(:)) - min(noise(:)));

% Create meshgrid
[X, Y] = meshgrid(1:width, 1:height);

% Generate Z coordinates for the surface


Z = noise;

% Create a defective area around the coordinates


defectMask = (X - defectX).^2 + (Y - defectY).^2 <= defectRadius^2;
Z(defectMask) = Z(defectMask) + defectAmplitude;

% Apply smoothing filter


h = fspecial('average', smoothingRadius);
Z = imfilter(Z, h, 'replicate');

% Display the 3D profile


figure;
surf(X, Y, Z);
colormap('jet');
title('3D Surface Profile with Defective Asperity and Smooth Surface');
xlabel('X');
ylabel('Y');
zlabel('Z');

You might also like