% Sample session with MATLAB % (1) GETTING STARTED: % First log into a unity machine with your id and password. % Start an xterm. % Type "add matlab7" at the prompt to ensure that you % will use MATLAB 7 hereafter. This has only to be done once. % Type "matlab" at the prompt % A MATLAB desktop should now appear. % Download the programs "bisect.m" and "myfunc.m" % from the course webpage into your home directory. % (2) A SAMPLE SESSION WITH BISECTION method in MATLAB % I assume you now have the MATLAB desktop running % You should type the following lines % in your MATLAB desktop in the following order: % I have interspaced my comments in the discussion . % These comments appear on a line that starts with a % sign. % You need not type these comment lines in your MATLAB desktop. % Kartik's MATLAB session: 17th August 2007 % Consider Example 1 in Chapter 2 on pages 48-49 of Burden and Faires % We will solve the nonlinear equation f(x) = x^3+4x^2-10=0 % using the bisection method % The file 'myfunc.m' contains the function f(x) % The intial interval is [a,b] = [1,2] since f(1) =-5 and f(2) = 14 help bisect % function [a, b, x, ithist, iflag] = bisect( f, a, b, tol, maxit ) % % use the bisection method to compute an approximate root of f. % Compare with Algorithm 2.1 on page 47 of Burden and Faires % % Input parameters: % func name of a matlab file containing a function % for example % f = myfunc(x) % f = x^3 + 4*x^2 -10 % a, b real numbers satisfying f(a)*f(b) < 0 % tol stopping tolerance (optional. Default tolx = 1e-7) % the bisection method stops if b-a < tol % maxit maximum number of iterations (optional. Default maxit = 100) % % % Output parameters: % a, b real numbers satisfying f(a)*f(b) < 0; a root of f is % located between a and b % x approximation of the solution. x = a + 0.5*(b-a) % ithist array with the iteration history % The i-th row of ithist contains [it, a, b, c, fc] % ifag return flag % iflag = -1 error in input data, f(a)*f(b) > 0 % iflag = 0 |b-a| < tol and |x-x*| < 0.5*tol, where x* % is a root of f % iflag = 1 iteration terminated because maximum number of % iterations was reached. |b-a| >= tol % % % Kartik Sivaramakrishnan % Department of Mathematics % North Carolina State University % August 17, 2007 [a,b,x,ithist,flag] = bisect(@myfunc,1,2,1e-7,100); % x % % x = % % 1.3652 format long x % x = % % 1.36522999405861 ithist % ithist = % % 0 1.00000000000000 2.00000000000000 1.50000000000000 2.37500000000000 % 1.00000000000000 1.00000000000000 1.50000000000000 1.25000000000000 -1.79687500000000 % 2.00000000000000 1.25000000000000 1.50000000000000 1.37500000000000 0.16210937500000 % 3.00000000000000 1.25000000000000 1.37500000000000 1.31250000000000 -0.84838867187500 % 4.00000000000000 1.31250000000000 1.37500000000000 1.34375000000000 -0.35098266601562 % 5.00000000000000 1.34375000000000 1.37500000000000 1.35937500000000 -0.09640884399414 % 6.00000000000000 1.35937500000000 1.37500000000000 1.36718750000000 0.03235578536987 % 7.00000000000000 1.35937500000000 1.36718750000000 1.36328125000000 -0.03214997053146 % 8.00000000000000 1.36328125000000 1.36718750000000 1.36523437500000 0.00007202476263 % 9.00000000000000 1.36328125000000 1.36523437500000 1.36425781250000 -0.01604669075459 % 10.00000000000000 1.36425781250000 1.36523437500000 1.36474609375000 -0.00798926281277 % 11.00000000000000 1.36474609375000 1.36523437500000 1.36499023437500 -0.00395910152292 % 12.00000000000000 1.36499023437500 1.36523437500000 1.36511230468750 -0.00194365901007 % 13.00000000000000 1.36511230468750 1.36523437500000 1.36517333984375 -0.00093584728188 % 14.00000000000000 1.36517333984375 1.36523437500000 1.36520385742188 -0.00043191879925 % 15.00000000000000 1.36520385742188 1.36523437500000 1.36521911621094 -0.00017994890323 % 16.00000000000000 1.36521911621094 1.36523437500000 1.36522674560547 -0.00005396254153 % 17.00000000000000 1.36522674560547 1.36523437500000 1.36523056030273 0.00000903099274 % 18.00000000000000 1.36522674560547 1.36523056030273 1.36522865295410 -0.00002246580384 % 19.00000000000000 1.36522865295410 1.36523056030273 1.36522960662842 -0.00000671741291 % 20.00000000000000 1.36522960662842 1.36523056030273 1.36523008346558 0.00000115678807 % 21.00000000000000 1.36522960662842 1.36523008346558 1.36522984504700 -0.00000278031288 % 22.00000000000000 1.36522984504700 1.36523008346558 1.36522996425629 -0.00000081176252 % 23.00000000000000 1.36522996425629 1.36523008346558 1.36523002386093 0.00000017251275 flag % flag = % % 0 a % a = % % 1.36522996425629 b % b = % % 1.36523002386093 exit