Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

why is while loop used here?

Asked by Jack on 11 Sep 2013

hello, I'm trying to understand why are we using while in this situation? what is it originally asking for? I don't understand he we got the i to be 3 and a to be 46 in the final answer

x = [1:4];
y = x.^2;
z = x'*y;
a = 1;
while a<15
for i=1:3
a = a*x(i) + y(i);
if a<4
a = a + 2;
end
end
a = a + 1;
end
a = 46
i= 3

0 Comments

Jack

Tags

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 11 Sep 2013
Accepted answer

"while" is being used because someone wanted to write obscure code, possibly for teaching purposes.

2 Comments

Jack on 11 Sep 2013

can you please comment on how we got the i and a?

Walter Roberson on 11 Sep 2013

It looks to me like it is intended to be an exercise. Follow the logic through step by step, working on paper if you must. (I followed the logic through in my head and confirmed that the code will produce those answers.)

You could also learn to use the debugger, and let it execute the program step by step and watch the variables change.

Walter Roberson

Contact us