title: [MATLAB]: Fourier Series - Conversion to logical from sym is not possible
body: <p>I'm trying to calculate/plot the Fourier Series of a function in MATLAB. The function is </p>
  
  <p><code>f(t) = 1-cos(t) for [-pi,0] and f(t) = t/pi for [0,pi].</code></p>
  
  <p>I need to find how many terms in the series are needed to approximate the original signal with 99% of the average energy. I'm trying to use a while loop to do it, but can't seem to get the comparison condition to work. The full code is below. </p>
  
  <pre><code>clear all
  
  n = 0;
  syms t;
  
  f1 = 1-cos(t);
  f2 = t/pi;
  
  e1 = int((abs(f1))^2,-pi,0);
  e2 = int((abs(f2))^2, 0, pi);
  
  e_av = (1/(2*pi)) * (e1 + e2);
  
  for k = 1 : 11
  c_n(k) = subs((1/(2*pi)) * (int((f1 * exp(-1j*n*2*pi*t)),-pi,0) + int((f2 * exp(-1j*n*2*pi*t)), 0, pi)));
  n=n+1;
  end
  
  sum = c_n(1).^2;
  i = 2;
  
  while (sum &lt; 0.9075)
      sum = sum + 2*c_n(i).^2;
      i = i+1;
  end
  
  display(i)
  display(sum)
  </code></pre>
  
  <p>The 0.9075 is 99% of e_av.</p>
  
  <p>I kind of understand what the error means, I'm assuming MATLAB reads sum as a symbolic variable for some reason, and you can't compare a sym to a logical. But when first defined, where <code>sum = c_n(1).^2;</code>, it's a value, not a sym (c_n(1) is cn0, the first Fourier Coefficient). Is the symbolic variable t somehow carrying over into c_n even after being integrated out? Any help would be much appreciated. </p>
  
  <p>Thankyou</p>