user3062845
user3062845

Reputation: 9

Extract and display result from table MATLAB

I am trying to extract the lowest value for V from the table produced for each force (F) in order to establish the optimum result, e.g. for F=5000, V=0.06575 is the optimum result.
I want to be able to display this for each instance of F. I'm not quite sure where I am going wrong.

%===========================================  
%This Script calculates 
%===========================================  
%Parameter Values  
%F:force (N)  
%L1: Length of beam 1 (m)  
%L2: Length of beam 2 (m)  
%H: Height of beam 1 (m)  
%h: Height of beam 2 (m)  
%b: Cross section width (m)  
%S: Yield strength (Pa)  
%N: Factor of safety  
%V:Volume of the beam (m^2)  
%=========================================

clc %clear command window 
clear all %clear all variables previously generated and stored in Matlab 
format short g 
L1=2; 
L2=3;
b=0.05; 
S=248; 
K=1.42; 
results=[]; 
optimalresults=[]; 
nd=3.6; %desired factor of safety calculated by group number

for F=5000:100:6000
    for h=0.2:0.005:0.3 %loop of h
        for H=0.2:0.005:0.3 % loop of H
            M1=F*(L1+L2);
            M2=F*L2;
            I1=b*H^3/12;
            I2=b*h^3/12;
            Y1=H/2;
            Y2=h/2;
            Max_stress_1=M1*Y1/I1;
            Max_stress_2=K*(M2*Y2/I2);
            Max_total_stress=max(Max_stress_1,Max_stress_2);
            Max_total_stress=Max_total_stress/1e6;
            N=0.577*S/Max_total_stress;
            %if N is greater than desired factor of safety then display results
            if N >= nd 
                V=(b*L1*H)+(b*L2*h);
                results=[results; F,V,N];sort(F);
            end
        end
    end
    results_sorted=sortrows(results,1); %sorts factor of safety from min to max
    disp('         F         V              N');
    disp(results_sorted);
    optimum=results_sorted(1,:)   
end

What do I have to change to display the minimal value of V for each F?

Upvotes: 0

Views: 246

Answers (2)

Moreira
Moreira

Reputation: 586

I think your code needs two changes before you can get the minimal value of V for each F:

Where you use sortrows, you should sort the table using the second column (V), so that the lowest V gets placed in the top row. results_sorted=sortrows(results,2);

You should sort only the results for the current F, so either you clear the variable "results" for each F iteration results=[]; or you use a new variable. Be sure to check if "results" is not empty before sorting the rows.

The variable "optimum" has now the lowest V for each F iteration.

Upvotes: 0

am304
am304

Reputation: 13886

My guess is that you probably want this bit outside of the 3 for loops:

results_sorted=sortrows(results,1); %sorts factor of safety from min to max
disp('         F         V              N');
disp(results_sorted);
optimum=results_sorted(1,:)

Also, I am not sure what you achieve with sort(F); inside the loop.

Upvotes: 0

Related Questions