Sign in

Not a member? | Forgot your Password?

Search code

Search tips

Free PDF Downloads

A Quadrature Signals Tutorial: Complex, But Not Complicated

Understanding the 'Phasing Method' of Single Sideband Demodulation

Complex Digital Signal Processing in Telecommunications

Introduction to Sound Processing

C++ Tutorial

FIR Filter Design Software

See Also

Embedded SystemsFPGA

DSP Code Sharing > Computing the Nth Roots of a Number

Computing the Nth Roots of a Number

Language: Matlab

Processor: Not Relevant

Submitted by Rick Lyons on Jan 9 2013

Licensed under a Creative Commons Attribution 3.0 Unported License

Computing the Nth Roots of a Number


In Matlab when we want to compute the square root of a number we use the 'sqrt()' command. For example, if z = 9 we find the square root of z by typing the

           >> sqrt(z)

command giving us the result

           ans =

That result, however, provides only the 'principle' square root of z. As we know the number z = 9 has two square roots, i.e., +3 and -3. Likewise, if we want the cube root of z (also called the "3rd root of z") we type

           >> z^(1/3)

command giving us the principle result

           ans =

Again, the 3rd root of z has three solutions. They are:

           -1.0400 + 1.8014i
           -1.0400 - 1.8014i.

The following Matlab code computes all of the n nth roots of the number(s) Num. If input argument Num has more than one element, then it must be a row vector. The elements in Num can be real-valued of complex-valued. Argument n is the order of the roots. If you want a complex-plane plot of the roots on Num that option, using the string 'Y', is also available. In such a plot the principle root is plotted as a blue square and the remaining roots are plotted with red circles.

For example, using the commands

           Num = 9;
           [nth_Roots] = roots_nth(Num, n, 'Y')

produces the results in Eq. (1) and the plot shown below.

Using the command

           [nth_Roots] = roots_nth(Num, n)

computes the n roots of Num, but no complex-plane plot is generated.

function [nth_Roots] = roots_nth(Num, n, Plot)
% ROOTS_NTH computes the nth roots of Num.
% Inputs:
%   Num is the number(s) for which the nth roots will be computed.
%     Num can be real-valued or complex-valued.
%     If Num has more than one element, then Num must be a row vector.
%   n must be a positive integer.
%   Plot is a string equal to 'Y' if a complex plane plot of
%        the n complex roots of each element in Num is desired.
%        Principle root is plotted as a blue square.  Remaining roots
%        are red dots.  If input Plot string does not exist, then no
%        plot is generated.
% Output:
%   nth_Roots is a column vector (with n rows) giving the nth roots of Num.
% Example:
%     clear, clc               % clear all variables
%     Num = [2+j*11, 3*exp(j*pi)]; % Two numbers
%     n = 3; % find the 3rd roots
%     [nth_Roots] = roots_nth(Num, n, 'Y')
%   Results are:
%       nth_Roots =
%          2.0000 + 1.0000i   0.7211 + 1.2490i
%         -1.8660 + 1.2321i  -1.4422 + 0.0000i
%         -0.1340 - 2.2321i   0.7211 - 1.2490i
%   [R. Lyons, Jan. 2013]
% Input argument check
if (nargin<2),
       disp('Not enough input arguments!!')
    if (nargin==2)
        Plot == 'N'
Num_of_Columns = max(size(Num)); % How many elements are in Num?
for Column_Loop = 1:Num_of_Columns;
    Mag(Column_Loop) = abs(Num(Column_Loop));
    Angle_Degrees(Column_Loop) = angle(Num(Column_Loop))*180/pi;
    for k = 1:n
        Root_Mag = Mag(Column_Loop)^(1/n);
        Root_Angle_Deg(k) = Angle_Degrees(Column_Loop)/n + (k-1)*360/n;
        nth_Roots(k,Column_Loop) = Root_Mag*exp(j*Root_Angle_Deg(k)*pi/180);
    % Plot roots, on complex plane(s), if necessary
    if Plot == 'Y'
            % Plot unit circle
            Num_Pts = 200;     % Number of circle points
            Index = 0 : Num_Pts-1;
            Alpha = 1/Num_Pts;
            Points = Root_Mag*exp(j*2*pi*Alpha*Index);
            plot(Points, ':k','markersize',5)
            grid on
        hold on
        % Plot principle root (square blue dot) and list results in plot
            axis([-1.1*Root_Mag, 1.1*Root_Mag, -1.1*Root_Mag, 1.1*Root_Mag]);
            plot(Root_Mag*cos(Root_Angle_Deg(1)*pi/180),  ...
                Root_Mag*sin(Root_Angle_Deg(1)*pi/180), 'bs');
            text(-Root_Mag/2, 3*Root_Mag/4, ['Magnitudes = ',num2str(Root_Mag)]);
            text(-Root_Mag/2, 3*Root_Mag/4-Root_Mag/10, ...
                ['Principle Angle (deg.) = ',num2str(Root_Angle_Deg(1))]);
        % Plot remaining roots as red dots
            for k = 2:n
                plot(Root_Mag*cos(Root_Angle_Deg(k)*pi/180),  ...
                    Root_Mag*sin(Root_Angle_Deg(k)*pi/180), 'ro');
                text(-Root_Mag/2, 3*Root_Mag/4-k*Root_Mag/10, ...
                    ['             Angle ',num2str(k),' (deg.) = ', ...
            end % End k loop
            xlabel('Real'); ylabel('Imag.');
        hold off
    end % End 'if Plot == 'Y''
end % End Column_Loop
Rate this code snippet:
Rating: 5 | Votes: 2
posted by Rick Lyons
Richard (Rick) Lyons is a consulting Systems Engineer and lecturer with Besser Associates in Mountain View, California. He is the author of "Understanding Digital Signal Processing 3/E" (Prentice-Hall, 2011), and Editor of, and contributor to, "Streamlining Digital Signal Processing, A Tricks of the Trade Guidebook" (IEEE Press/Wiley, 2012). He is a past Associate Editor for the IEEE Signal Processing Magazine.


No comments yet for this code

Add a Comment
You need to login before you can post a comment (best way to prevent spam). ( Not a member? )