Plot graph of roots versus coefficient of polynomial

조회 수: 1 (최근 30일)
Himanshu SINGLA
Himanshu SINGLA 2018년 4월 21일
편집: Walter Roberson 2018년 4월 21일
i was trying for a program. Suppose i am given any cubic equation and its one coefficient is changing. With change in that one coefficient, there is change in all three roots of that cubic equation.So i want to plot 3 different graph with coefficient and roots(r1,r2,r3).
I tried as follow
for a=1:100:1;
p=[1 1 a 1];
r=roots(p);
r1=r(1);
r2=r(2);
r3=r(3);
r11=real(sqrt(r1));
r22=real(sqrt(r2));
r33=real(sqrt(r3));
plot(a,r11);
end
So my problems are as follow
1. For loop is showing result only for last iteration. But i need all values of r11,r22,r33 corresponding to a.
2. If i try to define p(a) instead of p,
then matlab say ''In an assignment A(:) = B, the number of elements in A and B must be the same. And variable p change size in every loop iteration. Consider pre allocation''.
I have spent a lot of time searching on google, I find similar doubts and i tried them but can not run my program, Help me with this.

채택된 답변

John D'Errico
John D'Errico 2018년 4월 21일
편집: John D'Errico 2018년 4월 21일

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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by