title: Use variables in symbolic expressions - matlab
body: <p>How can I use variables inside a symbolic variable in Matlab?</p>
  
  <p>For example, I have the following code:</p>
  
  <pre><code>function f = constr_fourier(vec);
  dim=prod(size(vec));
  n=(dim-1)/2;
  a=@(k) vec(k+1);
  b=@(k) vec(n+k-1);
  
  f = @(x) subs(a,{k,0})/2 + symsum(subs(a,{k,i})*cos(i*x) + ...
  subs(b,{k,i})*sin(i*x),i,1,n);
  </code></pre>
  
  <p>In which I want to recover the Fourier series given a vector of coefficients <code>vec</code>. I want to replace the actual values from vec into the symbolic expression of the function. I tried that with <code>subs</code> but it doesn't work, or I didn't use it right.</p>
  
  <p>What is the right way to do this?</p>
  
  <p>[edit] I have tried </p>
  
  <pre><code>f = @(x) subs(a,k,0)/2 + symsum(subs(a,k,i)*cos(i*x) + ...
  subs(b,{k,i})*sin(i*x),i,1,n);
  </code></pre>
  
  <p>but the result is with <code>subs(...)</code> and not with the numerical value of <code>a(k)</code>.</p>
  
  <p>I have tried also a different variant, which gives the a result but in a wrong way...</p>
  
  <pre><code>function f = constr_fourier(w);
  syms x k n u c t vector;
  
  evalin(symengine,'assume(k,Type::Integer)');
  
  dim=prod(size(w));
  m=(dim-1)/2;
  
  a0=w(1);
  a= w(2:m+1);
  b= w(m+2:2*m+1);
  
  u=@(k,vector) vector(k);
  
  fs = @(x,n,c) c/2 + symsum(subs(u,{k,vector},{t,a})*cos(t*x) + subs(u,{k,vector},          {t,b})*sin(t*x),t,1,n);
  
  f= fs(x,m,a0);
  </code></pre>
  
  <p>I tried to use the function <code>u=@(k,vec) vec(k)</code> instead of the initial one. When I use <code>subs(u,{k,vector},{t,a})</code> separately in the terminal, it works ok, but here it doesn't...</p>
  
  <p>I get the result as a vector of two function instead of a function.</p>