Evaluate symbolic expression in MATLAB

337 Views Asked by At

I am trying to evaluate the following infinite series in MATLAB :

$$\hat{c_{k}}(x) = \sum_{m=0}^{\infty} a^{m} cos(2^{m+1}k \pi x)$$

For this I have written the following code:

function func = cosfun_hat(a,k)
    syms m x;

    assume(m,'integer');
    assumeAlso(m > 0);

    sum(x) = symsum(a^m*cos(k*sym(pi)*x*2^m+1),m,0,Inf);
    func(x) = sum(x);  
end

Now to evaluate this returned function at some input say x = 2, I write in the command window

%In the command window
func = cosfun_hat(0.5,2); 
func(2)

which returns the following symbolic expression

(2^(1/2)*3^(1/2)*sum((1/2)^m*(exp(- pi*exp(m*log(2))*4*k - k)/2 + exp(pi*exp(m*log(2))*4*k + k)/2), m == 0..Inf))/2

I tried using subs to evaluate the expression,

%In the command window
syms y;
w(y) = func(y);
y = 2;
subs(w);

But it returns the same symbolic expression.

I want to be able to get the numerical value of the expression st some input x.

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

I figured out a way to do it without symbolic MATLAB.

function func = cosfun_hat(a,i,x)
    %syms m;
%     assume(m,'integer');
%     assumeAlso(m > 0);
%     
    m = 0;
    sum = zeros(1,length(x));
    sum2 = Inf(1,length(x));
    while max(sum2-sum) > 1e-16
        disp(m);
        sum2 = sum;
        sum = sum + sqrt(1-a^2)*sqrt(2)*a^m*cos(i*pi*x*2^(m+1));
        m = m+1;

    end
    func = sum;
end

The sum converges in under 100 iterations.

Now in command window I write,

x_in = -2:0.001:2;
f = cosfun_hat(0.6,2,x_in);
plot(x_in,f);

I get the following plot: Infinite Cosine Sum

which possesses the nowhere differentiable properties.