So i have this code in MatLab(below.) The code looks at the linear function y=kx+f, where f=asin(0.376),and k=2*PI, and calculates the smallest positive integer x in which kx+f is within som error to its closest integer. The code does this for error values chosen as 0.1, 0.01, 0.001,… all the way to 10^(-10). The code works almost as it is supposed to, but when the code is at the point where it checks if the two numbers (line 7) differ by less than 10^(-8), it believes they are identical. How would i go about increasing the precision used in these calculations with digits(n) and vpa to make it work? Are there any other errors? π
A=asin(0.376); best=10; itr=1; for t=1:10 %Iterates trough the powers 10^-t while best>10^(-t) if abs(round(A+2*pi*itr)-(A+2*pi*itr))< best best=abs(round(A+2*pi*itr)-(A+2*pi*itr)); end itr=itr+1; end fprintf('It is sufficient with %d iterations to get an error that is less',itr); fprintf(' than 10^(-%d).The error is then=%.32f\n',t,best); if t~=10 itr=1; best=10; end end
PS:There always exist such an x, because the set of numbers on the form sin(N), where N is an integer is dense in [-1,1]