The problem with your code as you wrote it is you put the plot INSIDE the loop, plotting a new point each time.
But that is NOT how plot works.
If you ant to plot multiple values, then either save them all in an array, plotting it only at the end. (That is best.)
OR...
You can plot one point at a time, but use the hold command to force plot to plot on the old figure.
Otherwise, plot just keeps overwriting the old figure.
Why did I say that creating the entire set of data first, and plotting at the end is best? Because plotting is relatively slow. Multiple plots are slower than one call to plot.
Of course, the call to for was also completely incorrect as you wrote it!!!!
for a=1:100:1
What does that do? It creates a loop of length 1. You don't believe me?
1:100:1 ans = 1
The middle number is the stride. My guess is you wanted to write
for a = 1:1:100
That would create a as the integers from 1 to 100, one at a time. There is a difference, as I am sure you know.
r_all = zeros(100,3); a_list = 1:100; for n = 1:numel(a_list) p = [1 1 a_list(n) 1]; r_all(n,:) = roots(p); end
Now, you can plot the entire array. It seems you want to plot only the real part of those roots, or more likely, you want to plot only the real root. Be careful, as some of those roots are complex. Worse, they are not always in the same order.
So, if I list the first few of them, for different values of a, we see that when a was 1, the FIRST root is the one you wanted to see. But after than, it was the third root.
r_all r_all = -1 + 0i 1.5266e-16 + 1i 1.5266e-16 - 1i -0.21508 + 1.3071i -0.21508 - 1.3071i -0.56984 + 0i -0.31945 + 1.6332i -0.31945 - 1.6332i -0.3611 + 0i -0.36864 + 1.9158i -0.36864 - 1.9158i -0.26272 + 0i -0.39661 + 2.163i -0.39661 - 2.163i -0.20678 + 0i -0.41465 + 2.3846i -0.41465 - 2.3846i -0.17069 + 0i -0.42728 + 2.5871i -0.42728 - 2.5871i -0.14544 + 0i -0.43662 + 2.7746i -0.43662 - 2.7746i -0.12675 + 0i -0.44382 + 2.9501i -0.44382 - 2.9501i -0.11236 + 0i -0.44954 + 3.1156i -0.44954 - 3.1156i -0.10092 + 0i -0.4542 + 3.2727i -0.4542 - 3.2727i -0.091602 + 0i -0.45806 + 3.4225i -0.45806 - 3.4225i -0.08387 + 0i -0.46133 + 3.5659i -0.46133 - 3.5659i -0.077348 + 0i ...
So, perhaps you want to plot all three roots, in the complex plane?
plot(real(r_all),imag(r_all),'.')
I might do this instead, assuming you really only wanted to save the real root.
r_all = zeros(100,1); a_list = 1:100; for n = 1:numel(a_list) p = [1 1 a_list(n) 1]; r = roots(p); r(imag(r) ~= 0) = []; r_all(n) = r; end plot(a,r_all)
That code will fail if for some values of a, we have multiple real roots.