Finding maximum values for one 2D matrix in a loop but then finding the minimum value of that remaining vector? Help please!

1 view (last 30 days)
Hello! This is a very simple question but I'm a third year mechanical student trying to optimize the geometry of a crane, varying values of phi and theta. Look at my code so far.
%List of Design Constraints
L = 4; %units in metres
SF = 1; %chosen due to the danger of failure for lifting heavy objects
m = 4000; %units in kg
g = 9.81; %units ms^-2
P = SF*m*g; %total force units in Newtons
rmin = 1.2; %units in metres
rmax = 2.2; %units metres
thetamax = 80; %units in degrees
thetamin = -20; %units in degrees %CHANGE TO RADIANS?
%-------------------------------------------------------------------------
%LOOP PROCESSES TO VARY PHI AND THETA TO FIND MINIMUM RAM FORCE
% Use i and j to set up the following variables as vectors
i = 1;
k = 1;
for phi=0:5:180;
j = 1;
y(i)=20+phi;
c2(i)=((rmax^2)-(rmin^2))/((cosd(y(i)+thetamin))-(cosd(y(i)+thetamax)));
c1(i)=(rmax^2)+c2(i)*(cosd(y(i)+thetamax));
a(i)=(((c1(i)+c2(i))^0.5)+((c1(i)-c2(i))^0.5))/2;
b(i)=((c2(i))/(2*a(i)));
for theta=(thetamin):5:(thetamax);
r(i,j)=sqrt(((a(i))^2)+(b(i)^2)-(2*a(i)*b(i)*cosd(y(i)+theta)));
F(i,j)=(r(i,j)*P*L*cosd(theta))/(b(i)*a(i)*sind(y(i) + theta));
Fmax(k)=max(F(i,j));
j=j+1;
k=k+1;
end
Fminmax=min(Fmax(i))
i=1+i;
end
Essentially, F(i,j) is a large matrix of all possible values of force. I need to find the maximum force values for each value of phi and then the minimum based on the remaining vector? Sounds simple enough but i keep getting tied up!
Thank you! Emily

Answers (1)

Adam
Adam on 28 Oct 2014
This looks identical to a question I have just been answering.
Unless you really need to, don't keep track of maxima within the loop.
Just get the maxima out of F at the end of the loop. Then get the minimum out of that vector using:
doc min
doc max
Note the second output argument from these that will allow you to track the index of the row or column that yielded the min or max.
  2 Comments
Emily
Emily on 29 Oct 2014
I need the maximum values of the horizontal rows in my 37X21 matrix for force as a vector, not sure how to do that outside the loop! then i can find the minimum value for the 1X21 vector
Thanks! Emily
Adam
Adam on 29 Oct 2014
r = rand(37,21);
maxRowValus = max( r, [], 2 );
gives you the maximum values of along each horizontal row (37 of them), but there are 37 rows, 21 columns, so if you need a 1x21 vector I assume you mean you want the maximum of each column:
maxColValues = max( r );

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!