Cannot get output printed for every iteration of while loop.

I've been assigned a problem where I'm trying to print out a vector for each iteration of this while loop. Unfortunately it seems like it will only print out the vector when n = 13, when I need printed vectors from n=2 to 13.
n=2;
inc=1;
while n < 100
H_mat=hilb(n);
x_vec=rand(n,1);
b_vec=H_mat*x_vec;
x_approx=H_mat\b_vec;
fprintf('Approximate x value: \n');
disp(x_approx);
if (n > 13)
break;
end
n = n + 1; % Increment n for the next iteration
end
Approximate x value:
0.5749 0.0875
Approximate x value:
0.6030 0.5532 0.7677
Approximate x value:
0.8180 0.6329 0.1916 0.0545
Approximate x value:
0.7039 0.9937 0.8548 0.5057 0.2885
Approximate x value:
0.2469 0.9090 0.5089 0.2990 0.5166 0.4045
Approximate x value:
0.3512 0.4562 0.7001 0.2637 0.9788 0.9042 0.2292
Approximate x value:
0.0153 0.7825 0.3902 0.4387 0.3419 0.9813 0.7404 0.4780
Approximate x value:
0.6497 0.2333 0.7243 0.6011 0.7866 0.4191 0.2551 0.0846 0.3893
Approximate x value:
0.9755 0.6017 0.7922 0.8728 0.2308 0.2886 0.3753 0.1568 0.3033 0.5263
Approximate x value:
0.5979 0.8452 0.5459 0.4357 0.4932 0.4491 0.3476 0.7268 0.1130 0.4357 0.4975
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.609829e-17.
Approximate x value:
0.6427 0.5002 0.8743 0.4863 0.5516 0.1474 0.0630 0.3802 0.7376 0.7585 0.5587 0.9272
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.160155e-18.
Approximate x value:
0.6415 0.9714 0.3046 0.7931 0.2466 0.4886 0.8272 -0.0089 1.2808 -0.0887 0.8444 0.7789 0.3083
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 7.292772e-20.
Approximate x value:
0.2344 0.3239 0.0060 1.0356 0.5715 1.0407 0.5281 -10.5407 39.2708 -73.0277 83.9341 -58.2595 23.5180 -3.3400

 Accepted Answer

Anika Peer
Anika Peer on 28 Oct 2023
Edited: Anika Peer on 28 Oct 2023
NEVERMIND: I figured it out, there was nothing wrong with my code, I wasn't looking at the output on the console corrrectly, I only noticed that after I cleared my output.

More Answers (0)

Categories

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

Products

Asked:

on 28 Oct 2023

Edited:

on 28 Oct 2023

Community Treasure Hunt

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

Start Hunting!