title: Finding Percent Error of a Fourier Series
body: <p>Find the error as a function of n, where the error is defined as the difference between two the voltage from the Fourier series (vF (t)) and the value from the ideal function (v(t)), normalized to the maximum magnitude (Vm ):</p>
  
  <p>I am given this prompt where Vm = 1 V. Below this line is the code which I have written.</p>
  
  <p>I am trying to write a function to solve this question: Plot the error versus  time for n=3,n=5,n=10, and n=50. (10points). What does it look like I am doing incorrectly?</p>
  
  <hr>
  
  <pre><code>clc;
  close all;
  clear all;
  
  % define the signal parameters
  Vm = 1;
  T = 1;
  w0 = 2*pi/T;
  
  % define the symbolic variables
  syms n t;
  
  % define the signal
  v1 = Vm*sin(4*pi*t/T);
  v2 = 2*Vm*sin(4*pi*t/T);
  
  % evaluate the fourier series integral
  an1 = 2/T*int(v1*cos(n*w0*t),0,T/2) + 2/T*int(v2*cos(n*w0*t),T/2,T);
  bn1 = 2/T*int(v1*sin(n*w0*t),0,T/2) + 2/T*int(v2*sin(n*w0*t),T/2,T);
  a0 = 1/T*int(v1,0,T/2) + 1/T*int(v2,T/2,T);
  
  % obtain C by substituting n in c[n]
  nmax = 100;
  n = 1:nmax;
  a = subs(an1);
  b = subs(bn1);
  
  
  % define the time vector
  ts = 1e-2; % ts is sampling the
  t = 0:ts:3*T-ts;
  
  % directly plot the signal x(t)
  t1 = 0:ts:T-ts;
  v1 = Vm*sin(4*pi*t1/T).*(t1&lt;=T/2);
  v2 = 2*Vm*sin(4*pi*t1/T).*(t1&gt;T/2).*(t1&lt;T);
  v = v1+v2;
  x = repmat(v,1,3);
  
  % Now fourier series reconstruction
  N = [3];
  
  for p = 1:length(N)
  for i = 1:length(t)
  for k = N(p)  
  x(k,i) = a(k)*cos(k*w0*t(i)) + b(k)*sin(k*w0*t(i));
  end
  % y(k,i) = a0+sum(x(:,i)); % Add DC term
  end
  
  end
  
  z = a0 + sum(x);
  
  figure(1);
  plot(t,z);
  
  %Percent error
  function [per_error] = percent_error(measured, actual)
  per_error = abs(( (measured - actual) ./ 1) * 100);
  end
  </code></pre>