while loop - [MATLAB]: Fourier Series - Conversion to logical from sym is not possible -


i'm trying calculate/plot fourier series of function in matlab. function

f(t) = 1-cos(t) [-pi,0] , f(t) = t/pi [0,pi].

i need find how many terms in series needed approximate original signal 99% of average energy. i'm trying use while loop it, can't seem comparison condition work. full code below.

clear  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);  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; = 2;  while (sum < 0.9075)     sum = sum + 2*c_n(i).^2;     = i+1; end  display(i) display(sum) 

the 0.9075 99% of e_av.

i kind of understand error means, i'm assuming matlab reads sum symbolic variable reason, , can't compare sym logical. when first defined, sum = c_n(1).^2;, it's value, not sym (c_n(1) cn0, first fourier coefficient). symbolic variable t somehow carrying on c_n after being integrated out? appreciated.

thankyou


Comments