MATLAB: Determination of e^x using taylor polynomial representation

#while_end_loopMATLAB

I want to write a program to find e^x using Taylor series representation.The limit of the number of passes is 30.When the absolute value of last added term is smaller than 0.0001 adding should be stopped.This should be handled using a while loop.And if in the 30 th pass the value of added term is not smaller than 0.001 a message should be diaplayed. I've tried it myself.But it doesn't seem to be working.Following is my code.
function exponen(x)
clc
eX=1; %when x=0;
y(n+1)=x^(n+1)/factorial(n+1);
while abs(y(n+1))>0.0001
for n=0:30
eX=eX+sum(y);
if abs(y(31))<0.0001
fprintf('The answer is %.5f',eX);
else
fprintf('More than 30 terms are needed');
end
end
end
end

Best Answer

Roughly speaking,
for n = 1 : 30
....
if abs(y(n)) < 0.0001
break
end
end