Q:

How to define a vector that its elements are created in a For Loop?

I want to define a vector,X, with five elements:
syms a
X = zeros(1,5)
X(1) = 1;
for k=1:4
    X(k+1)=X(k)+a^2;
end

Actually I need to have the vector X, that its elements should be based on variable a. But I face an error in Matlab, when I write the code above:
The following error occurred converting from sym to double:
Error using symengine (line 58)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.

Error in Code2 (line 5)
X(k+1)=X(k)*a^2;

How to fix this? 

A:

You are mixing between symbolic variables to doubles,
Doubles holds a value, you can use the loop that you wrote only if a is a double and already holds a value (the value can be the input of a function) .
for example :
function ret=testFunc(a)
X = zeros(1,5)
X(1) = 1;
for k=1:4
    X(k+1)=X(k)+a^2;
end
ret=X
end

if you want to work with syms (for other symbolic analysis) you can define x as a symbol too, for example :
syms a x
x(1)=1;
for i=2:5
x(i)=x(i-1)+a.^2;
end

now, x is a function of a, and if you print x you will get : 
[ 1, a^2 + 1, 2*a^2 + 1, 3*a^2 + 1, 4*a^2 + 1]

to evaluate the value of x, you will still need to input a value for a.
Matlab help suggest subs function for replacing a in wanted value as follows:
y = subs(x,a,4)

in that point y is still a symbol and you will need to make it a double by using 
double(y)

