title: How to find the complex part of a root using Newton-Raphson method in matlab?
body: <pre><code>my_fun = z^3 - 1;
  my_deriv = 3*(z^2);
  </code></pre>
  
  <p>Those are the functions I used. I can find the real part of my solutions but when I use a function that has an imaginary part to the root such as the function above, I don't know how to find it. My code goes through different values for my initial value and if I find a root I store it in my array and if it finds a root already in the array it ignores it. So I'm basically wondering can I alter my code to find a root with an imaginary part. Thanks</p>
  
  <pre><code>clc;
  close all;
  clear all;
  
  Roots = [];
  Array_slot = 1;
  threshold = 0.00000001;
  % X_initial = 1;
  
  for (j = -10:10)
      X_initial = j;
      if (my_deriv(X_initial) ~= 0)
      counter = 0;
  
      while (abs(my_fun(X_initial)) &gt; threshold &amp;&amp; counter &lt; 100)
          X_initial;
  %             imag(X_initial)
          X_one = X_initial - (my_fun(X_initial)/my_deriv(X_initial));
          X_initial = X_one;
  %             imag(X_one)
          counter = counter + 1;
  
      end
      if counter &lt; 1000
      root = (round(X_initial*1000))/1000;
  
          if ~ismember(root,Roots)
  
              Roots(Array_slot) = root;
              Array_slot = Array_slot + 1;
          end
      end
  end
  end
  
  Roots
  </code></pre>