我刚刚开始学习Matlab(几天前),我有以下功课,我不知道如何编写代码: 编写一个脚本,使用多项式函数的根的位置创建图形:p(z)= z ^ n-1在复杂计划中,用于n自然数的各种值(统一的第n个根)
答案 0 :(得分:0)
所以我假设你使用的函数是p(z) = (z^n) - 1
,其中n
是一个整数值。
您只需插入roots
函数即可找到此等式的根。传递给roots
的数组是输入函数的系数。
示例
f = 5x^2-2x-6 -> Coefficients are [5,-2,-6]
让根进入根([5,-2,-6])。这将找到x
将导致函数等于0的所有点。
所以在你的情况下你会输入funcRoots = roots([1,zeros(1,n-1),-1]);
然后您可以根据需要绘制这些值,但像plot(funcRoots)
这样的简单图可能就足够了。
要循环使用以下内容。请注意,如果有多个相同的根,可能会有一些重叠,因此您可能无法看到某些值。
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)