Page 39 - MATLAB an introduction with applications
P. 39

24 ———  MATLAB: An Introduction with Applications


                   1.15.4 Output Commands
                   There are two commands that are commonly used to generate output. They are the disp and fprintf commands.
                   1.  The disp command:
                   The disp command displays the elements of a variable without displaying the name of the variable and
                   displays text.
                       disp(name of a variable) or disp(‘text as string’)
                               >>A = [1 2 3 ; 4 5 6 ];
                               >> disp(A)
                                         1  2  3
                                         4  5  6
                               >> disp(‘Solution to the problem.’)
                                 Solution to the problem.
                   2.  The fprintf command:
                   The fprintf command displays output (text and data) on the screen or saves it to a file. The output can be
                   formatted using this command.
                   Example E1.4: Write a function file Veccrossprod to compute the cross product of two vectors a and b, where
                   a = (a , a , a ), b = (b , b , b ), and a × b = (a b  – a b , a b  – a b , a b  – a b ). Verify the function by taking
                        1
                             3
                          2
                                         3
                                                      2 3
                                    1
                                      2
                                                                         1 2
                                                                               2 1
                                                                3 1
                                                                     1 3
                                                           3 2
                   the cross products of pairs of unit vectors: (i, j), (j, k), etc.
                   Solution:
                       function c = Veccrossprod(a, b);
                   % Veccrossprod : function to compute c = a × b where a and b are 3-D vectors
                   % call syntax:
                   % c = Veccrossprod(a, b);
                   c = [a(2)* b(3)– a(3)* b(2); a(3)* b(1)– a(1)* b(3); a(1)* b(2)– a(2)* b(1)];
                    1.16  PROGRAMMING IN MATLAB
                   One most significant feature of MATLAB is its extendibility through user-written programs such as the
                   M-files. M-files are ordinary ASCII text files written in MATLAB language. A function file is a subprogram.

                   1.16.1 Relational and Logical Operators
                   A relational operator compares two numbers by finding whether a comparison statement is true or false.
                   A logical operator examines true/false statements and produces a result which is true or false according to the
                   specific operator. Relational and logical operators are used in mathematical expressions and also in combination
                   with other commands to make decision that control the flow of a computer program.















                   F:\Final Book\Sanjay\IIIrd Printout\Dt. 10-03-09
   34   35   36   37   38   39   40   41   42   43   44