02.03.2016 Views

MATLAB by rudra pratap

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

112 Programming in <strong>MATLAB</strong>: Scripts and Functions<br />

idea to copy the function definition line without the word function among those<br />

first few comment lines so that the execution syntax of the function is di::>played<br />

<strong>by</strong> the on-line help. The command lookfor looks for the argument string in the<br />

first commented line of M-files. Therefore, in keeping with the somewhat confusing<br />

convention of <strong>MATLAB</strong>'s built-in functions, you should write the name of the script<br />

or function file in uppercase letters, followed <strong>by</strong> a short description with keywords<br />

as the first commented line. Thus, the first line following the function definition<br />

line in the example function on page 104, Section 4.2.1, reads<br />

% SDLVEXF solves a 3x3 matrix equation with parameter r .<br />

4.3.2 Continuation<br />

An ellipsis (three consecutive periods) . at the end of a line denotes continuation. So,<br />

if you type a command that does not fit on a single line, you may split it across two<br />

or more lines <strong>by</strong> using an ellipsis at the end of each but the last line.<br />

Examples:<br />

A = [1 3 3 3; 5 10 -2 -20 ; 3 5<br />

10 2; 1 0 0 9] ;<br />

x = s in (linspace ( 1,6*pi , 100)) ·* cos(linspace (1,6*pi ,100) ) + . ..<br />

0.5*ones (1,100) ;<br />

plot (tube_length ,fluid_pressure , ' :' , tube _length , . . .<br />

theoretical_pressure ,'-')<br />

(The last command assumes that variables tube_length, fluid_pressure, etc.<br />

exist in the workspace.)<br />

You may not, however, use continuation inside a character string. For example,<br />

typing<br />

logo 'I am not only the President and CEO of Miracle Hair , . ..<br />

but also a client' ;<br />

produces an error. For creating such long strings, break the string into smaller<br />

string segments and use concatenation (sec Section 3.3).<br />

4.3.3 Global variables<br />

It is possible to declare a set of variables to be globally access.ible to all or some<br />

functions without passing the variables in the input list. This is done with the<br />

global command. For example, the statement global x y z declares the variables<br />

x, y, aml z Lo be glulml. This statement goes before /l,llY executable otatement in<br />

the functions and scripts thaL uecd to access the values of the global variables. Be<br />

can'!fnl with the names of the global variables. It i:'l generally 11 good idea to name<br />

such variables with long strings to avoid any unintended match with vLher local<br />

variables.<br />

Hxample: Consider solving the following first-order ODE:<br />

x = kx + csin t, x(O) = 1 .0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!