Matlab Programs

14 downloads 119 Views 39KB Size Report
Matlab programs are called m-files because their file names always have ... The following script is a simple program to apply functional iteration to solve the ...
Matlab Programs Matlab programs are called m-files because their file names always have the extension .m. There are two types of m-file, scripts and functions. The m-file shown below is a script, which is the simpler type. To make an m-file, just use your favorite editor to create a file called whatever you want with the file extension .m. The names xyz.m, cosfunk.m, and ralph.m are all valid m-file names. If you make a script called, say, joe.m, then you can invoke that script by typing joe at the MATLAB command line. This causes the commands in the script to be executed in order. The following script is a simple program to apply functional iteration to solve the equation x = 12 cos x. I stored these commands in a file called itdem.m. % Sample functional iteration program % Anything following a percent sign is a comment. x = zeros(2,1); % % % % x(1) = xguess; %

Approximate solutions will be stored in vector x. Initially it is set to be a 2-by-1 vector of zeros. This cheap trick makes x a column vector, not a row vector. Initial guess goes in x(1).

for n = 1:itmax % beginning of loop x(n+1) = .5*cos(x(n)); diff = abs(x(n+1)-x(n)); % using absolute error for simplicity. if diff < tol break % jump out of loop end % end of if clause end % end of loop if n < itmax % beginning of if clause disp(’Iterations converged to prescribed tolerance.’) else disp(’Iterations failed to converge to prescribed tolerance.’) end % end of if clause x

% displays the vector x.

% end of sample program 1

This simple program illustrates the syntax for a loop, simple if statements, and some other useful features. The following MATLAB session illustrates the use of this m-file. It begins by setting a tolerance tol for the convergence test, a maximum number of iterations itmax, and an initial guess xguess, all of which itdem.m needs in order to function. >> tol = eps

% tolerance for the convergence test. MATLAB % initializes the variable eps to the machine % epsilon, which is twice the unit roundoff u.

tol = 2.2204e-16 >> itmax = 100

% Give up after 100 iterations.

itmax = 100 >> xguess = .5

% Initial guess

xguess = 0.5000 >> format long e % Show a lot of digits. >> itdem

% Run the m-file itdem.m

Iterations converged to prescribed tolerance. x = 5.000000000000000e-01 4.387912809451864e-01 4.526329216602096e-01 4.496493762136596e-01 4.502997781314499e-01 4.501583343696809e-01 4.501891105361431e-01 2

4.501824148432776e-01 4.501838716010567e-01 4.501835546612318e-01 4.501836236163955e-01 4.501836086141363e-01 4.501836118781093e-01 4.501836111679816e-01 4.501836113224809e-01 4.501836112888672e-01 4.501836112961803e-01 4.501836112945893e-01 4.501836112949354e-01 4.501836112948601e-01 4.501836112948765e-01 4.501836112948729e-01 4.501836112948737e-01 4.501836112948736e-01 >> exit

3