the line
Error = Error -(100*abs((exact - sum)/exact));
is wrong.
Replace it with
Error = 100*abs((exact - sum)/exact);
also you initialize i to 1, should be 0.
In the end :
function ApproxEulers(PercError,exact)
i=0;
sum=0;
Error = 100;
while Error >= PercError ;
i=i+1;
sum = sum + (1/(i^2));
Error = 100*abs((exact - sum)/exact);
end
fprintf('\n\tThe number of terms: %d \n', i)
fprintf('\tThe approxamite value: %.8f \n', sum)
fprintf('\tThe exact value: %.8f \n\n', exact)
end
which will give you
>> ApproxEulers(.001,pi^2/6)
The number of terms: 60793
The approxamite value: 1.64491762
The exact value: 1.64493407
One remark : sum and error are the names of basic matlab functions. try to not use them when you code. Just pick other names to define your variables, otherwise, one day you're gonne struggle to debug a code because of that kind of mistake.
Best Answer