Problem with for-while loop, simple coding error?

Hi! I Have an assignment at school in which i am supposed to calculate the diameter of a shaft in order to not exceed the stress limit.
Basically i have devided the shaft in to 1000 segments and calculated bending moment, torsion and so on in each segment, so i have vectors describing those things.
Now I want to itterate over each segment to find a diameter that is big enough so it doesn't exceed the stress limit.
for i=1:1000
while ("my stess equation including the diameter, bending moment and so on") > stress limit
D(i)=D(i)+0.0001
end
end
The problem is that the D I get out of the loop is the same as the one I put in (the while loop is fulfilled without increasing the diameter). But when i calculate it without the loop i get values that are in fact larger than the stress limit. Therefore i guess there must be something wrong with my loop, but im not very good at matlab so... Any help is much apreciated!

4 Comments

provide the datas required to run your code (stress limit , D(1) and ("my stess equation including the diameter, bending moment and so on") )?
Adam
Adam on 13 Nov 2018
Edited: Adam on 13 Nov 2018
Clearly if your value of D is the same before and after the while loop then it must never have entered the while loop given that the instruction inside it is very explicit. But since the while condition you give is not syntactically correct there is no further help to be given based on the information you provided!
You took the time to provide a good expplanation around your question then left out the one part that is vital to the question! what is the actual code of the while condition?
In a general case though it helps you to work these things through very logically, as in my first paragraph. You can get to the root problem very quickly if you do that (and have all the information) because you will know immediately that your while condition is never satisfied, so then you look at that condition, find why that is, etc etc and you arrive at the bug/problem.
I cant really give the exact code since my teacher is going to check it for plagiarism but the basic is:
a=[20 40 60 40 20];
D=[5 5 5 5 5]; %just a starting value
for i=1:5
while (a(i)/D(i))>2
D(i)=D(i)+1
end
end
Is the loop written correctly?
The loop you have given certainly changes the D values and terminates after having updated it numerous times.

Answers (0)

This question is closed.

Asked:

on 13 Nov 2018

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!