How do i correct my MATLAB Optimisation code

1 view (last 30 days)
James Clarke
James Clarke on 15 Apr 2024
Moved: Torsten on 15 Apr 2024
I have written a optimisation code for MATLAB to refine design parameters for Ri and Ro to calculate the minimum volume of a shaft of Length 1000mm. Torque of 5392Nm is applied to the shaft made of S235 Steel with a density of 7850kg/me, E of 230GPa, Shear stiffness of 95GPa and a shear yield strength of 140MPa and a Poisson ratio of 0.3 are used. RI and Ro are initially set as 70 and 100mm respectively. The length of the shaft and the position of a 30mm diameter rivet hole, placed halfway down the shaft and cuts through the entire shaft, must be kept fixed. The maximum shear stress in the bar must not exceed the shear yield stress and the twist angle should be kept below 1.5 degrees. Wall thickness must not be less than 5mm.
My code is as follows:
function shaft_optimization
% Given data
L = 1000; % Length of the shaft in mm
T = 5392; % Applied torque in Nm
r_hole = 15; % Radius of the rivet hole in mm
shearYieldStrength = 140e6; % Shear yield strength in Pascals
G = 95e9; % Shear modulus in Pascals
maxTwistAngle = 1.5; % degrees
% Objective function: minimize the volume of the shaft
objective = @(x) pi * (x(1)^2 - x(2)^2) * L;
% Constraint functions
constraints = @(x) deal([
% Shear stress constraint
16 * T / (pi * (x(1)^4 - x(2)^4)) - shearYieldStrength,
% Twist angle constraint
(T * L) / (G * (pi/2 * (x(1)^4 - x(2)^4))) - maxTwistAngle * (pi/180),
% Wall thickness constraint ensuring the hole is accounted for
x(1) - x(2) - r_hole,
% Ensuring the volume remains non-negative
pi * (x(1)^2 - x(2)^2) * L - 0
], []);
% Initial guesses for ro and ri
x0 = [100, 70]; % Based on initial dimensions provided
% Setting options for the optimizer
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
% Run the optimization
lower_bounds = [r_hole + 70, r_hole]; % Ensure ro > ri + r_hole
upper_bounds = [150, 95]; % Example upper bounds
[x_opt, fval] = fmincon(objective, x0, [], [], [], [], lower_bounds, upper_bounds, constraints, options);
% Display the results
fprintf('Optimal outer radius ro = %.2f mm\n', x_opt(1));
fprintf('Optimal inner radius ri = %.2f mm\n', x_opt(2));
fprintf('Minimum volume = %.2f cubic mm\n', fval);
end
I seem to get a negative value for minimum volume. Can anyone help me debug this issue and offer recommendations for corrections.
Thanks,
James

Answers (1)

Torsten
Torsten on 15 Apr 2024
Moved: Torsten on 15 Apr 2024
Unit error. Convert all mm in m (or all m in mm).

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!