How can I use variables inside a symbolic variable in Matlab?
For example, I have the following 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);
In which I want to recover the Fourier series given a vector of coefficients vec. I want to replace the actual values from vec into the symbolic expression of the function. I tried that with subs but it doesn't work, or I didn't use it right.
What is the right way to do this?
[edit] I have tried
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);
but the result is with subs(...) and not with the numerical value of a(k).
I have tried also a different variant, which gives the a result but in a wrong way...
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);
I tried to use the function u=@(k,vec) vec(k) instead of the initial one. When I use subs(u,{k,vector},{t,a}) separately in the terminal, it works ok, but here it doesn't...
I get the result as a vector of two function instead of a function.