0

I've just started learning Matlab(a few days ago) and I have the following homework that I just don't know how to code it: Write a script that creates a graphic using the positions of the roots of the polynomial function: p(z)=z^n-1 in the complex plan for various values for n-natural number(nth roots of unity)

  • Break it up into pieces. First figure out how to find the roots of your polynomial for a specific value of `n`. Then figure out how to plot those roots. Then repeat for various values of `n`. – David K Oct 08 '13 at 20:22

1 Answers1

0

so I am assuming the function you are using is p(z) = (z^n) - 1 where n is some integer value.

you can the find the roots of this equation by simply plugging into the roots function. The array passed to roots are the coefficients of the input function.

Example

f = 5x^2-2x-6 -> Coefficients are [5,-2,-6]

To get roots enter roots([5,-2,-6]). This will find all points at which x will cause the function to be equal to 0.

so in your case you would enter funcRoots = roots([1,zeros(1,n-1),-1]);

You can then plot these values however you want, but a simple plot like plot(funcRoots) would likely suffice.

To do in a loop use the following. Mind you, if there are multiple roots that are the same, there may be some overlap so you may not be able to see certain values.

minN = 1;
maxN = 10;
nVals = minN:maxN;
figure; hold on;
colors = hsv(numel(nVals));
legendLabels = cell(1,numel(nVals));
iter = 1;
markers = {'x','o','s','+','d','^','v'};
for n = nVals
    funcRoots = roots([1,zeros(1,n-1),-1]);
    plot(real(funcRoots),imag(funcRoots),...
         markers{mod(iter,numel(markers))+1},...
         'Color',colors(iter,:),'MarkerSize',10,'LineWidth',2)
    legendLabels{iter} = [num2str(n),'-order'];
    iter = iter+1;
end
hold off;
xlabel('Real Value')
ylabel('Imaginary Value')
title('Unity Roots')
axis([-1,1,-1,1])
legend(legendLabels)
MZimmerman6
  • 8,445
  • 10
  • 40
  • 70