Clear Filters
Clear Filters

How can I get multiple values from a formula where the inputs are based on an array?

5 views (last 30 days)
I am fairly new to Matlab. I thought this should be simple, but it is proving much more difficult than I expected.
I am trying to create a code that takes a range of values for the inside diameter of a tube, runs them through a series of engineering formulas, and then outputs multiple factors of safety based on the inside diameter of the tube. I thought it would be effective to scatter plot the results.
What I am getting is a plot where it shows all the values for the inputs, but only plots them against the lowest value of the outputs. I have tried taking out the plot and just returning values, but I cannot get that to work either. Maybe there is something obvious I am missing or maybe this is the wrong approach all together. I have removed the input array and checked the code for individual values and it works just fine.
Any suggestions are appreciated. Here is the code.
% create arrays to explore design options
clc;clear all;close all;
% Possible ID Sizes
ID = [0.014 0.015 0.016 0.017 0.018 0.019];
%Define the given variables; units are kg-meters-seconds
Sut = 350000000;
Suc = 1000000000;
Ltube = 0.1;
Larm = 0.4;
Fapp = 50;
ODtube = 0.02;
%Solve for applied stresses
Tarm = Fapp*Larm;
Marm = 5;
Atube = (pi/4).*(ODtube.^2-ID.^2);
Itube = (pi/64).*(ODtube^4-ID.^4);
Jtube = 2.*Itube;
BendStress = (Marm.*(ODtube/2))./Itube;
TransShear = (2*Fapp)./Atube;
TorsionalShear = (Tarm*(ODtube/2))./Jtube;
%Find Principal Stresses at Point A
Tmax = ((BendStress./2).^2+TorsionalShear.^2).^0.5;
Sigma1 = (BendStress/2)+Tmax;
Sigma2 = 0;
Sigma3 = (BendStress/2)-Tmax;
Tau13 = (Sigma1-Sigma3)/2;
%Apply MMES; We know sigma2 = 0
C1 = 0.5*(abs(Sigma1-Sigma2)+((-Suc+2*Sut)/(-Suc))*(Sigma1+Sigma2));
C2 = 0.5*(abs(Sigma2-Sigma3)+((-Suc+2*Sut)/(-Suc))*(Sigma2+Sigma3));
C3 = 0.5*(abs(Sigma3-Sigma1)+((-Suc+2*Sut)/(-Suc))*(Sigma3+Sigma1));
MMES = [C1 C2 C3 Sigma1 Sigma2 Sigma3];
SigmaFOS = max(MMES);
FOS = Sut/SigmaFOS;
%use scatter instead of plot to avoid plot line issues
scatter(FOS,ID)
xlabel('Factor of Safety')
ylabel('Inside Diameter of Shaft (m)')

Accepted Answer

Voss
Voss on 30 Dec 2023
Edited: Voss on 30 Dec 2023
The problem is that MMES is a row vector, so SigmaFOS = max(MMES) is a scalar. See below for changes necessary to make SigmaFOS have one element per element of ID.
% create arrays to explore design options
clc;clear all;close all;
% Possible ID Sizes
ID = [0.014 0.015 0.016 0.017 0.018 0.019];
%Define the given variables; units are kg-meters-seconds
Sut = 350000000;
Suc = 1000000000;
Ltube = 0.1;
Larm = 0.4;
Fapp = 50;
ODtube = 0.02;
%Solve for applied stresses
Tarm = Fapp*Larm;
Marm = 5;
Atube = (pi/4).*(ODtube.^2-ID.^2);
Itube = (pi/64).*(ODtube^4-ID.^4);
Jtube = 2.*Itube;
BendStress = (Marm.*(ODtube/2))./Itube;
TransShear = (2*Fapp)./Atube;
TorsionalShear = (Tarm*(ODtube/2))./Jtube;
%Find Principal Stresses at Point A
Tmax = ((BendStress./2).^2+TorsionalShear.^2).^0.5;
Sigma1 = (BendStress/2)+Tmax;
Sigma2 = zeros(size(ID));
Sigma3 = (BendStress/2)-Tmax;
Tau13 = (Sigma1-Sigma3)/2;
%Apply MMES; We know sigma2 = 0
C1 = 0.5*(abs(Sigma1-Sigma2)+((-Suc+2*Sut)/(-Suc))*(Sigma1+Sigma2));
C2 = 0.5*(abs(Sigma2-Sigma3)+((-Suc+2*Sut)/(-Suc))*(Sigma2+Sigma3));
C3 = 0.5*(abs(Sigma3-Sigma1)+((-Suc+2*Sut)/(-Suc))*(Sigma3+Sigma1));
MMES = [C1; C2; C3; Sigma1; Sigma2; Sigma3];
SigmaFOS = max(MMES,[],1);
FOS = Sut./SigmaFOS;
%use scatter instead of plot to avoid plot line issues
scatter(FOS,ID)
xlabel('Factor of Safety')
ylabel('Inside Diameter of Shaft (m)')
  1 Comment
David
David on 30 Dec 2023
Thank you so much for your help! Based on the online searches I did prior to posting the question, I thought it might have something to do with vector and scalar quantities. But I couldn't find anything specific enough to solve the issue. Thanks again!

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 30 Dec 2023
I really wonder why my submitted asnswer disappeared. The system has some failures. This is the answer that I submiited yesterday:
% create arrays to explore design options
clc;clearvars;close all;
% Possible ID Sizes
ID = [0.014 0.015 0.016 0.017 0.018 0.019];
%Define the given variables; units are kg-meters-seconds
Sut = 350000000;
Suc = 1000000000;
Ltube = 0.1;
Larm = 0.4;
Fapp = 50;
ODtube = 0.02;
%Solve for applied stresses
Tarm = Fapp*Larm;
Marm = 5;
Atube = (pi/4).*(ODtube.^2-ID.^2);
Itube = (pi/64).*(ODtube^4-ID.^4);
Jtube = 2.*Itube;
BendStress = (Marm.*(ODtube/2))./Itube;
TransShear = (2*Fapp)./Atube;
TorsionalShear = (Tarm*(ODtube/2))./Jtube;
%Find Principal Stresses at Point A
Tmax = ((BendStress./2).^2+TorsionalShear.^2).^0.5;
Sigma1 = (BendStress/2)+Tmax;
Sigma2 = zeros(size(Sigma1));
Sigma3 = (BendStress/2)-Tmax;
Tau13 = (Sigma1-Sigma3)/2;
%Apply MMES; We know sigma2 = 0
C1 = 0.5*(abs(Sigma1-Sigma2)+((-Suc+2*Sut)/(-Suc))*(Sigma1+Sigma2));
C2 = 0.5*(abs(Sigma2-Sigma3)+((-Suc+2*Sut)/(-Suc))*(Sigma2+Sigma3));
C3 = 0.5*(abs(Sigma3-Sigma1)+((-Suc+2*Sut)/(-Suc))*(Sigma3+Sigma1));
MMES = [C1; C2; C3; Sigma1; Sigma2; Sigma3];
SigmaFOS = max(MMES);
FOS = Sut./SigmaFOS;
%use scatter instead of plot to avoid plot line issues
C = linspace(1, 6, numel(FOS));
BSize = 55;
scatter(FOS,ID, BSize, C, 'filled', 'MarkerEdgeColor', [0.5 0.5 0.5])
xlabel('Factor of Safety')
ylabel('Inside Diameter of Shaft (m)')
grid on
ylim([min(ID)-.0005 max(ID)+.0005])

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!