Loop until Matrix value exceeds value

12 views (last 30 days)
Buster
Buster on 13 Sep 2021
Answered: dpb on 13 Sep 2021
I have a script for truss analysis. The calculation is set up Ax=b. A is the govenring equations from the truss anaylsis while x is m. Thus b is the stresses in the members depending on m. m ranges from values 1-7. i need to make a loop that ends the calulations after a certain value of matrix b is met and then displays the previus iteration as "max allowable mass". my issue is designating that "a value inside b" > given value. and having the loop re-run for the acceptable tolerances.
  3 Comments
Buster
Buster on 13 Sep 2021
Essentially i only have A=9x1 and x=1:7. B is the resulting values of A*x and they are given as 9 different values which is what i need. Basically i need a loop to run through the caluclation of A*x=B until a single value of the 9 given by B is over 60. once any of said values hits 60 i need the loop to stop and display all 9 B values that are accepted and the x value that gives these B values that are less than the tolerance of 60. does this help?
Jan
Jan on 13 Sep 2021
If A ia [9 x 1] vector and x is [1 x 7], A * x is a [9 x 7]. How can this be given as 9 different values? Over which calculation do you want to run a loop?

Sign in to comment.

Answers (1)

dpb
dpb on 13 Sep 2021
% pseudo code, salt to suit...
A=[...]; % initialize A here
maxB=60; % the test value
for x=1:7
B=A*x; % compute B
if all(B)<=maxB % everything AOK so far...
fprintf(['X=%d, B[]=' repmat(numel(B),'%f.2 ') newline],X,B);
end
end
The above prints every time the condition is met rather than waiting unitl it fails and then backing up one level -- that's doable, takes a little more logic to save the previous values as well so I took the easy way out here.
The key MATLAB point here is the use of any and/or all to make the logic test -- read the documentation for them to see why.
I don't know the problem your'e trying to solve, of course, but it seems to me that rather than use the multiples of 1:N it would make more sense to solve the system to find the critical mass value instead.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!