Hello,
In the first code in the line "f=-d2f+df" the variables "-d2f" and "df" are symbolic expressions so the variable "f" that is created is also a symbolic expression. Hence when you are trying to index through "f" in the line "rhs=f(2:end-1)" , "rhs" is being returned as an empty "sym" object and hence causing an error in the line "U(2:end-1,i)=A\rhs".
To correct this you need to substitute the values of "x" in the expression for "f" by using "subs" command in MATLAB. Applying "vpa" function over it will return the numeric array that you require in the calculation. The following code might help you:
syms x;
f=exp(-x.^2).*(1-x.^2);
df=diff(f,x);
d2f=diff(df,x);
N=6;
a=-1;
b=1;
h=(b-a)/(N-1);
x1=(a:h:b)';
X(1:N)=x1;
u_exact=exp(-x1.^2).*(1-x1.^2);
f=-d2f+df;
disp(f)
f1=subs(f,x,x1)
vpa(f1)
rhs=f1(2:end-1)