I am looking to create a table of data calculated within a while loop to get values that satisfy certain requirements.

4 views (last 30 days)
I am in the process of completing a project where I need to collect data that matches certain specifications in regards to solutions over an interval. I am able to properly set up how to calculate each equation and piece of the problem, but I am struggling to find a way to place everything in an array, table, etc. Everything I have seen online in regards to this does not work due to the nature of the problem having varying results, and I am rather lost as to where to go.
  4 Comments
Voss
Voss on 24 Mar 2022
Is the main problem at this point about how to select/record which sets of D, M, R meet the condition? Or is it more about how to keep track of all the D, M, R as the while loop runs? Or both? Or something else?
Maybe you can attach the code?
Christopher Root
Christopher Root on 24 Mar 2022
Edited: Voss on 24 Mar 2022
The main problem is how to record the data in a new row after knowing that the condition was met. The order of the points would not be important as well! The value for D is constant, so it wouldnt be very necessary to record it but I didnt know if you could record just the two columns. The code below is the entire Program that I have so far with a placeholder showing where im looking for input on recording data!
m = 2540 ; % The mass of Orion in Kilograms
Y = 0.01 ; % Amplitude determined by equation of motion y(t)
w = 50*pi ; % Omega, the angular velocity value in Radians/sec
wn = 1 ; % Starting natural Frequency
g = 98 ; % The maximum acceleration experienced in m/s^2
st = 0.0017; % The maximum standard deflection in m
stcalc = st +1 ;
r = 1 ; % frequency ratio start
d = 0.01; % damping ratio start
Xmin = 1;
Xcalc = 1.5 ;
M = 0 ; %Magnitude Ratio
% Value for the Stiffness Coefficient
Row = 0 ;
k = 100000000 ; % Minimum value for Spring Coefficient in N/m
while k < 300000000
count = count + 1
%Numerator = (k^2)+((c*w)^2); % Numerator of Equation
%Denominator = (((k-m*(w^2))^2)+(c*w)^2); %Denominator of Equation
wn = sqrt(k/m) ;
r = w/wn ;
c = d*(2*sqrt(m*k)) ;
Numerator = 1+((2*d*r)^2) ;
Denominator = ((1-(r^2))^2)+((2*d*r)^2) ;
Xcalc = Y * sqrt(Numerator/Denominator);
stcalc = (m*g)/k ;
%Xcalc = Y*sqrt(((k^2)+(c*(w^2))/((k-m*(w^2))+((c*w)^2)))) ;
if stcalc < st
if Xmin > Xcalc
Xmin = Xcalc ;
cmin = c ;
kmin = k ;
end
M = Xcalc/Y ;
%Right here is where I would need to record the values for the variables
% M and r in a table!
end
k = k + 1000 ;
end

Sign in to comment.

Answers (1)

Voss
Voss on 24 Mar 2022
Please see below. all_results is the matrix I put in to store the M and r each time the condition is satisfied. (I also adjusted the initial k value and the while condition in order to get some results with relatively few iterations, for demonstration - you can change that back to how it was.)
m = 2540 ; % The mass of Orion in Kilograms
Y = 0.01 ; % Amplitude determined by equation of motion y(t)
w = 50*pi ; % Omega, the angular velocity value in Radians/sec
wn = 1 ; % Starting natural Frequency
g = 98 ; % The maximum acceleration experienced in m/s^2
st = 0.0017; % The maximum standard deflection in m
stcalc = st +1 ;
r = 1 ; % frequency ratio start
d = 0.01; % damping ratio start
Xmin = 1;
Xcalc = 1.5 ;
M = 0 ; %Magnitude Ratio
% Value for the Stiffness Coefficient
Row = 0 ;
k = 200000000 ; % Minimum value for Spring Coefficient in N/m
count = 0;
% going to store the results in a matrix with 2 columns,
% so initialize it to have zero rows at first:
all_results = zeros(0,2);
while k < 200000000+100*1000%300000000
count = count + 1;
%Numerator = (k^2)+((c*w)^2); % Numerator of Equation
%Denominator = (((k-m*(w^2))^2)+(c*w)^2); %Denominator of Equation
wn = sqrt(k/m) ;
r = w/wn ;
c = d*(2*sqrt(m*k)) ;
Numerator = 1+((2*d*r)^2) ;
Denominator = ((1-(r^2))^2)+((2*d*r)^2) ;
Xcalc = Y * sqrt(Numerator/Denominator) ;
stcalc = (m*g)/k ;
%Xcalc = Y*sqrt(((k^2)+(c*(w^2))/((k-m*(w^2))+((c*w)^2)))) ;
if stcalc < st
if Xmin > Xcalc
Xmin = Xcalc ;
cmin = c ;
kmin = k ;
end
M = Xcalc/Y ;
% now store the current M and r in the all_results matrix
all_results(end+1,:) = [M r];
end
k = k + 1000 ;
end
% make the stored M and r into a table t:
t = array2table(all_results,'VariableNames',{'M','r'})
t = 100×2 table
M r ______ _______ 1.4563 0.55979 1.4563 0.55978 1.4563 0.55978 1.4563 0.55978 1.4563 0.55978 1.4562 0.55978 1.4562 0.55978 1.4562 0.55978 1.4562 0.55977 1.4562 0.55977 1.4562 0.55977 1.4562 0.55977 1.4562 0.55977 1.4562 0.55977 1.4562 0.55977 1.4562 0.55976

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!