フィルターのクリア

de Moivres Nth Root Function on returns 2 out 5 roots? Spot the fault?

3 ビュー (過去 30 日間)
FortuitousMonkey
FortuitousMonkey 2018 年 3 月 15 日
コメント済み: FortuitousMonkey 2018 年 3 月 15 日
Hi,
I wonder if anyone could help me find the fault in the code? When I call the function using the below it only returns 2 values, the first and last root, but misses 3 in the middle, subsequently my plot is also incorrect (it should return 5 equally spaced points). I can't find any error in the code.
>> complex_root1(2+3j,5)
D =
1.2675 + 0.2524i 0.6317 - 1.1275i
My function is
function res=complex_root(z,N)
%z is the complex number
%n is the number of roots, must be positive and larger than o
k=[0,N-1];
%Logic statement if N above 0 proceed with calculation
if N>0
a=complex(z);
magnitude=abs(a);
r=(abs(a));
t=angle(a);
%de Moivre's forumla
D=r^(1/N)*((cos((t+2*pi*k)/N))+((1i*sin((t+2*pi*k)/N))))
polarplot (D)
%Logical statement, if N below zero return error message
elseif N<=0;
disp 'N must be a positive integer above 0'
end
end

採用された回答

David Goodmanson
David Goodmanson 2018 年 3 月 15 日
編集済み: David Goodmanson 2018 年 3 月 15 日
Hi Owen, try
k = 0:N-1;
which gives all five values for k.
  1 件のコメント
FortuitousMonkey
FortuitousMonkey 2018 年 3 月 15 日
Nice spot, it had me puzzled.
Obviously, it has been a long night. Thanks David.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMarine and Underwater Vehicles についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by