Clear Filters
Clear Filters

How would I properly go about creating this function?

1 view (last 30 days)
I am doing a project to practice matlab and thought it would be best to be put into a function. This is my function and the rest of my code, I have variables t_values, height_values, and width_values that need to be used outside of the function, but I do not know how to use them from inside the function. These equations are going to be different for the different beams which is why I decided to create a general function. Thanks!
l = 70*12; %length
maxHeight = 12; %maximum height of beam
maxWidth = 10; %maximum width of beam
minHeight = 3; %minimum height of beam
minWidth = 3; %minimum width of beam
maxDef = 10; %maximum beam deformation
w = 3; %distributed load
p = 500; %point force
a = l/3; %distance to point force
E_alum = 9.9*10^6; %aluminum stiffness
E_steel = 28*10^6; %steel stiffness
E_ti = 16*10^6; %titanium stiffness
den_alum = .098; %aluminum density
den_steel = .286; %steel density
den_ti = .16; %titanium density
E = [E_alum, E_steel, E_ti];
density = [den_alum, den_steel, den_ti];
%For combined load
I_min = (w*(l^4)./(E.*30.*maxDef))...
- ((p*(a^2)*((3*l)-a))./(E.*6.*maxDef));
disp('I-Beam');
I-Beam
A_horizI = width_values.*t_values; %Area of flanges
Unrecognized function or variable 'width_values'.
A_vertI = t_values.*(height_values-(2.*t_values)); %Area of web
I_horizI = width_values.*t_values.^3; %Moment of flanges
I_vertI = (t_values.*(height_values-(2.*t_values)).^3)./12; %Moment of web
I_I_beam = I_vertI + 2.*(I_horizI + A_horizI.*((height_values./2)-(t_values./2)).^2); %I for I beam
weightIbeam = @(t, h, w, l, density) (2*w.*t + t.*(h-2.*t))*l*density; %Weight equation for I beam
beamoptimize(I_min, l, density,I_I_beam,weightIbeam);
function beamoptimize( I_min, l, density, I1, weightFunc)
t = 0.1:0.05:1;
height = minHeight:0.05:maxHeight;
width = minWidth:0.05:maxWidth;
V = {t, height, width};
C = cell(1, numel(V));
[C{:}] = ndgrid(V{:});
C = cellfun(@(X) reshape(X, [], 1), C, 'UniformOutput', false);
C = horzcat(C{:});
t_values = C(:, 1);
height_values = C(:, 2);
width_values = C(:, 3);
for i = 1:length(I_min)
I = I1;
valid_I = I(I >= I_min(i));
valid_t = t_values(I >= I_min(i));
valid_height = height_values(I >= I_min(i));
valid_width = width_values(I >= I_min(i));
% Calculate weight using the provided weight function
weight = weightFunc(valid_t, valid_height, valid_width, l, density(i));
[min_weight, min_index] = min(weight);
optimal_I = valid_I(min_index);
optimal_t = valid_t(min_index);
optimal_height = valid_height(min_index);
optimal_width = valid_width(min_index);
disp(['For I_min = ', num2str(I_min(i))]);
disp(['Minimum weight: ', num2str(min_weight)]);
disp(['Optimal valid_I: ', num2str(optimal_I)]);
disp(['Minimum thickness: ', num2str(optimal_t)]);
disp(['Optimal height: ', num2str(optimal_height)]);
disp(['Optimal width: ', num2str(optimal_width)]);
disp('---------------------------------------');
end
end
  1 Comment
Dyuman Joshi
Dyuman Joshi on 20 Apr 2024
What is the objective here? What are you trying to do?
Which values are the inputs? Which parameters are outputs? Which quantities are constants?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Apr 2024
l = 70*12; %length
maxHeight = 12; %maximum height of beam
maxWidth = 10; %maximum width of beam
minHeight = 3; %minimum height of beam
minWidth = 3; %minimum width of beam
maxDef = 10; %maximum beam deformation
w = 3; %distributed load
p = 500; %point force
a = l/3; %distance to point force
E_alum = 9.9*10^6; %aluminum stiffness
E_steel = 28*10^6; %steel stiffness
E_ti = 16*10^6; %titanium stiffness
den_alum = .098; %aluminum density
den_steel = .286; %steel density
den_ti = .16; %titanium density
E = [E_alum, E_steel, E_ti];
density = [den_alum, den_steel, den_ti];
%For combined load
I_min = (w*(l^4)./(E.*30.*maxDef))...
- ((p*(a^2)*((3*l)-a))./(E.*6.*maxDef));
disp('I-Beam');
I-Beam
[height_values, width_values, t_values] = calculate_height_width(minHeight, maxHeight, minWidth, maxWidth);
A_horizI = width_values.*t_values; %Area of flanges
A_vertI = t_values.*(height_values-(2.*t_values)); %Area of web
I_horizI = width_values.*t_values.^3; %Moment of flanges
I_vertI = (t_values.*(height_values-(2.*t_values)).^3)./12; %Moment of web
I_I_beam = I_vertI + 2.*(I_horizI + A_horizI.*((height_values./2)-(t_values./2)).^2); %I for I beam
weightIbeam = @(t, h, w, l, density) (2*w.*t + t.*(h-2.*t))*l*density; %Weight equation for I beam
beamoptimize(height_values, width_values, t_values, I_min, l, density, I_I_beam, weightIbeam);
For I_min = 355.0754 Minimum weight: 1193.64 Optimal valid_I: 355.2708 Minimum thickness: 0.5 Optimal height: 12 Optimal width: 9 --------------------------------------- For I_min = 125.5445 Minimum weight: 1244.4432 Optimal valid_I: 125.6859 Minimum thickness: 0.2 Optimal height: 12 Optimal width: 7.15 --------------------------------------- For I_min = 219.7029 Minimum weight: 1177.344 Optimal valid_I: 220.2674 Minimum thickness: 0.3 Optimal height: 12 Optimal width: 8.9 ---------------------------------------
function [height_values, width_values, t_values] = calculate_height_width(minHeight, maxHeight, minWidth, maxWidth)
t = 0.1:0.05:1;
height = minHeight:0.05:maxHeight;
width = minWidth:0.05:maxWidth;
V = {t, height, width};
C = cell(1, numel(V));
[C{:}] = ndgrid(V{:});
C = cellfun(@(X) reshape(X, [], 1), C, 'UniformOutput', false);
C = horzcat(C{:});
t_values = C(:, 1);
height_values = C(:, 2);
width_values = C(:, 3);
end
function beamoptimize( height_values, width_values, t_values, I_min, l, density, I1, weightFunc)
for i = 1:length(I_min)
I = I1;
valid_I = I(I >= I_min(i));
valid_t = t_values(I >= I_min(i));
valid_height = height_values(I >= I_min(i));
valid_width = width_values(I >= I_min(i));
% Calculate weight using the provided weight function
weight = weightFunc(valid_t, valid_height, valid_width, l, density(i));
[min_weight, min_index] = min(weight);
optimal_I = valid_I(min_index);
optimal_t = valid_t(min_index);
optimal_height = valid_height(min_index);
optimal_width = valid_width(min_index);
disp(['For I_min = ', num2str(I_min(i))]);
disp(['Minimum weight: ', num2str(min_weight)]);
disp(['Optimal valid_I: ', num2str(optimal_I)]);
disp(['Minimum thickness: ', num2str(optimal_t)]);
disp(['Optimal height: ', num2str(optimal_height)]);
disp(['Optimal width: ', num2str(optimal_width)]);
disp('---------------------------------------');
end
end

More Answers (1)

Image Analyst
Image Analyst on 20 Apr 2024
What is "width_values"? You never set it equal to anything so of course it complains.
  2 Comments
Oliver Ries
Oliver Ries on 20 Apr 2024
Width_ values was set in the function. So that’s what I’m trying to figure out is how to use it with the equations that need it outside the function as well as the other equations that use it within the function.
Image Analyst
Image Analyst on 20 Apr 2024
Edited: Image Analyst on 20 Apr 2024
It was not set before you tried to use it. Maybe you set it later but if you use it it must be assigned first. Tell me the line of code where you actually assigned values to width_values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!