Question
Consider the function, f(x)=(sin(x))/x. This function has a minimum value for some x
Explanation / Answer
%%%%%%%%%%%%%% % save as bisect30.m clear; % clear memory % Initial interval is [a,b]; tol is tolerance a = input('Enter value of a: '); b = input('Enter value of b: '); tol = input('Enter vlaue of tolerance: '); %a=-1; %b=1; %tol=0.0001; maxIts=20; % maximum number of Iterations % Check that a,b are good if f(a)*f(b) > 0 , disp('f(a)*f(b) > 0; cannot guarantee a zero in [a,b]'); return; % terminate and return to prompt end; % disp(' Left Right f(mid)') % Print column headings % Set up plotting N=100; % x=a:(b-a)/(N-1):b; y=zeros(size(x)); for (i=1:N); y(i)=f(x(i)); end; min=min(y); max=max(y); hold off; plot(x, y,'r-'); hold on; plot([a,b],[0,0],'r-'); xlabel('x'); ylabel('f(x)'); title('Bisection algorithm'); % Start bisection left = a; right = b; fleft = f(left); n = 0; % counter while n maxIts, disp('No convergence to requested accuracy') else disp('Succesfully computed root'); alpha=mid end; hold off; %%%%%%%%%%%%%% % save as f.m function yx= f(x) yx= sin(x) / x; >> bisect30 Enter value of a: pi Enter value of b: 2*pi Enter vlaue of tolerance: 0.005 Left Right f(mid) 3.141593e+000 4.712389e+000 -2.122066e-001 Hit any key 3.141593e+000 3.926991e+000 -1.800633e-001 Hit any key 3.141593e+000 3.534292e+000 -1.082773e-001 Hit any key 3.141593e+000 3.337942e+000 -5.844629e-002 Hit any key 3.141593e+000 3.239767e+000 -3.025438e-002 Hit any key 3.141593e+000 3.190680e+000 -1.537844e-002 Hit any key 3.141593e+000 3.166136e+000 -7.751160e-003 Hit any key 3.141593e+000 3.153864e+000 -3.890953e-003 Hit any key 3.141593e+000 3.147729e+000 -1.949306e-003 Hit any key 3.141593e+000 3.144661e+000 -9.756082e-004 Hit any key Succesfully computed root alpha = 3.1447