For/While/If loop question

Hey there I have a range of values from 0-10 and for different ranges there will be different relations for example lets say x=0:10 and when x<3 than x2=x^2+2x and when 3<=x<7 than x2=x^4-x^8+44x and than another relation for the rest i'm wondering how I'd write the loop for it.
This is what I was thinking x=0:10;
while x<.3 x2=x^2+2x end
while 3<=x<7 x2=x^4-x^8+44x end
fprintf('x2')
although maybe a while loop nested in a for loop could work if that makes sense

1 Comment

@David: It is impolite to remove the text of the question, after somebody has spent the time an effort to post an answer. If you proceed to do this, your chances to get answers for your future questions will shrink.

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 1 Nov 2018
Edited: Image Analyst on 2 Nov 2018
It's sort of close. I made it a little more workable, though it's certainly not how we'd do it. I'm just doing it that way because you seem to want to do it via two while loops, which can be done, but is not how I'd recommend it:
numPoints = 500; % Whatever...
x = linspace(0, 10, numPoints);
k = 1;
while x(k) < 3 && k <= numPoints
x2(k) = x(k)^2 + 2*x(k);
fprintf('x(%d) = %f\n', k, x(k));
k = k + 1;
end
while x(k) >= 3 && k <= numPoints
x2(k) = x(k)^4 - x(k) ^8 + 44 * x(k);
fprintf('x(%d) = %f\n', k, x(k));
k = k + 1;
if k > numPoints
break;
end
end
plot(x, x2, 'b-', 'LineWidth', 2);
grid on;
fprintf('x2')
Here's how I'd probably do it:
numPoints = 500; % Whatever...
x = linspace(0, 10, numPoints);
x2 = zeros(1, numPoints);
for k = 1 : length(x)
if x(k) < 3
x2(k) = x(k) ^ 2 + 2 * x(k);
else
x2(k) = x(k) ^ 4 - x(k) ^ 8 + 44 * x(k);
end
end
plot(x, x2, 'b-', 'LineWidth', 2);
grid on;
fprintf('x2')
in a for loop. There are vectorized ways that are more compact, though sometimes they can be more cryptic:
numPoints = 500; % Whatever...
x = linspace(0, 10, numPoints);
smallX = x < 3;
x2(smallX) = x(smallX) .^ 2 + 2 * x(smallX);
x2(~smallX) = x(~smallX) .^ 4 - x(~smallX) .^ 8 + 44 * x(~smallX);

4 Comments

Wow thanks, so if there was another 3rd relation like 5<x<10 or something than a 3 while loops would be preferred right? If and else seem like only two relations of x would work and with while loop I can do more.
I think the while loop is the most difficult way to do this, although you can do almost anything in whatever way you want: for loop, while loop, or vectorized. I would NOT prefer three while loops to do a piecewise formula. I'd probably do it vectorized first, followed by a for loop, and absolute last resort would be three while loops.
thank you for all the help, i'm just still confused how in a for loop you could have more than 2 options, it just seems pretty binary with If when x<3 than x2=x^2+2x and else x(k) ^ 4 - x(k) ^ 8 + 44 * x(k) how could you include a third relation like like when 7<x<10? It seems like with while loops you would do 3 equations but with if in for loops you can only have two relations because of If and Else.
Just add more elseif's
if x(k) < 3
% Formula #1
elseif x(k) < 7
% Formula #2
elseif x(k) < 10
% Formula #3
elseif x(k) < 34
% Formula #4
else
% More than 34, so use Formula #5
end

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Tags

Community Treasure Hunt

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

Start Hunting!