How would I properly go about creating this function?
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’);
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(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
endI 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’);
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(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 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’);
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(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 help, functions, matlab MATLAB Answers — New Questions