MATLAB: I cannot get the error.

errorMATLAB

syms f(x) a(x)
f(x)=(cos(x)-(2*x)+3);
a(x)=(1/2)*(cos(x)+3);
da=diff(a,x);
b=pi/2.5;
s='a f(x)';
disp(s);
for i=(1:100)
if abs(vpa(da(b)))<1
if vpa(f(a(b)))~=0
a(b)= vpa(a(b))
f(b)=vpa(f(b))
A=[num2str(a(b)),' ',num2str(f(b))];
disp(A);
a(b)=vpa(a(a(b)));
i=i+1;
elseif vpa(f(b))==0;
a(b)= vpa(a(b))
f(b)=vpa(f(b))
A=[num2str(a(b)),' ',num2str(f(b))];
disp(A);
end
else
a(b)=vpa(a(a(b)));
i=i+1;
end
end
a f(x)
Error using sym/subsasgn (line 837)
Invalid indexing or function definition. When defining a function, ensure that
the arguments are symbolic variables and the body of the function is a SYM
expression. When indexing, the input must be numeric, logical, or ':'.
MATLAB gave the error at the bottom for the code above it. Where am i doing wrong? Anyone help?

Best Answer

a(x)=(1/2)*(cos(x)+3);
That defines a as being a symbolic function
a(b)= vpa(a(b))
That attempts to define the value of the function evaluated at b, but MATLAB does not permit you to define a general function and then assign values to the function at specific locations.
Below is the code rewritten in a way that is acceptable to MATLAB:
syms f(x) a(x)
f(x)=(cos(x)-(2*x)+3);
a(x)=(1/2)*(cos(x)+3);
da=diff(a,x);
b=pi/2.5;
s='a f(x)';
disp(s);
for i=(1:100)
if abs(vpa(da(b)))<1
if vpa(f(a(b)))~=0
a(x) = piecewise(x==b, vpa(a(b)), a(x));
f(x) = piecewise(x == b, vpa(f(b)), f(x));
A=[num2str(double(a(b))),' ',num2str(double(f(b)))];
disp(A);
a(x)= piecewise(x==b, vpa(a(a(b))), a(x));
i=i+1;
elseif vpa(f(b))==0
a(x)= piecewise(x==b, vpa(a(b)), a(x));
f(x) = piecewise(x == b, vpa(f(b)), f(x));
A=[num2str(doubble(a(b))),' ',num2str(double(f(b)))];
disp(A);
end
else
a(x) = piecewise(x==b, vpa(a(a(b))), a(x));
i=i+1;
end
end
I made no attempt at all to correct the algorithm: I just corrected the syntax to make it do exactly what you coded.