AlIsra University Faculty of Information Technology Department of

  • Slides: 37
Download presentation
Al-Isra University Faculty of Information Technology Department of CS Programming Mathematics using MATLAB 605351

Al-Isra University Faculty of Information Technology Department of CS Programming Mathematics using MATLAB 605351 Dr. Khaled Al-Qawasmi Dr khaled Alqawasmi, 2010 -2011 1

Chapter 2 MATLAB Script Dr khaled Alqawasmi, 2010 -2011 2

Chapter 2 MATLAB Script Dr khaled Alqawasmi, 2010 -2011 2

Creating and using a script file n n n n To create a script:

Creating and using a script file n n n n To create a script: Click file->then new-> then M-file. You will see a new edit window This is the Edit/Debugger window To create a new script, simply type the sequence of statements. When finished, select save from the file menu Make sure the extension is. m Dr khaled Alqawasmi, 2010 -2011 3

. Cont The rules of filenames are the same as for variables. n By

. Cont The rules of filenames are the same as for variables. n By default, script will be saved in the work directory (see current directory tab) n Ex. Create a script called pro 1. m that calculates the area of a circle. rad. m radius=5; area=pi*(radius^2) n Dr khaled Alqawasmi, 2010 -2011 4

. Cont In the command window the content of the script can be shown

. Cont In the command window the content of the script can be shown using: >>type pro 1 n To run or execute the script, there are two ways: 1. Enter the file name at the command prompt >> pro 1 2. Click Debug menu-> Run (save and run) F 5 n Dr khaled Alqawasmi, 2010 -2011 5

. Cont n n The results are displayed at the command prompt. Any assignment

. Cont n n The results are displayed at the command prompt. Any assignment statement proceeded by semicolon (; ) its result will not displayed at the command prompt during program execution. Dr khaled Alqawasmi, 2010 -2011 6

Documentation The symbol % designates a comment. n For example write a script file

Documentation The symbol % designates a comment. n For example write a script file that compute the square root. %program pro 2. m % this program compute the sine of %square root x=sqrt([5: 2: 13]); y=sin(x) To show the comments at the command prompt, write: >> help pro 2 n Dr khaled Alqawasmi, 2010 -2011 7

Input Function Input statement read in values that have been entered by the user

Input Function Input statement read in values that have been entered by the user n The values are entered through the command prompt n The input function in MATLAB called input n For example >> rad=input (‘Enter the radius: ’) Enter the radius : 5 rad = 5 n Dr khaled Alqawasmi, 2010 -2011 8

. Cont If character or string input is desired, ‘s’ must be added after

. Cont If character or string input is desired, ‘s’ must be added after the prompt. >>letter=input (‘enter a char : ’, ’s’) enter a char: g letter=g n If blank spaces are entered before other characters they are included in the string. >>mystr=input (‘enter a string: ‘, ’s’) enter a string: go mystr = go n Dr khaled Alqawasmi, 2010 -2011 9

. Cont It is possible for the user to type quotation marks around the

. Cont It is possible for the user to type quotation marks around the string rather than including the second argument ‘s’ >> name = input (‘enter your name: ’); enter your name: ‘khaled’ n What happen if string input has not been specified >> num = input (‘enter a number: ’) enter a number: t ? ? ? Error using ==> input Undefined function or variable ‘t’. enter a number: 3 num=3 n Dr khaled Alqawasmi, 2010 -2011 10

. Cont MATLAB gave an error messge and repeated the prompt. n But if

. Cont MATLAB gave an error messge and repeated the prompt. n But if t is the name of a variable, MATLAB will take its value as the input >> t=11 num = input (‘enter a number: ’) enter a number: t num = 11 n Dr khaled Alqawasmi, 2010 -2011 11

. Cont n Separate input statements are necessary if more than one input is

. Cont n Separate input statements are necessary if more than one input is desired. >>x = input (‘Enter the x coordinate: ’); >>y = input (‘Enter the y coordinate: ’); Dr khaled Alqawasmi, 2010 -2011 12

Output statements: disp and fprintf Output statements display strings and the results of expressions,

Output statements: disp and fprintf Output statements display strings and the results of expressions, and allow formatting. n disp, is used to display the result of expression or a string without assigning any value to the default variable ans. n disp does not allow formatting >>disp(‘Hello’); >>disp(4^3) n Dr khaled Alqawasmi, 2010 -2011 13

fprintf Formatted output can be printed to the screen using the fprintf function >>fprintf(‘the

fprintf Formatted output can be printed to the screen using the fprintf function >>fprintf(‘the value is %d, for sure!n’ , 4^3) n First string called the format string contains any text to be printed. n %d called a placeholder, it specifies: 1. Where the value of the expression that is after the string is to be printed. n Dr khaled Alqawasmi, 2010 -2011 14

. Cont 2. The character in the place holder is called the conversion character.

. Cont 2. The character in the place holder is called the conversion character. It specifies the type of value that is being printed. n List of simple palceholder %d integers %f float %c single characters %s strings Dr khaled Alqawasmi, 2010 -2011 15

cont n n ‘n’ is a special character called the newline. The newline character

cont n n ‘n’ is a special character called the newline. The newline character can also be used in the prompt in the input statement for ex. >>x=input(‘Enter the nx coordinate: ‘); Enter the X coordinate: 4 n filed width can also included in the placeholder in fprintf. Dr khaled Alqawasmi, 2010 -2011 16

cont filed width specifies how many characters total are to be used in printing

cont filed width specifies how many characters total are to be used in printing >>fprintf(‘the int is %3 d and the float is %6. 2 fn’, 4, 4. 9); n For example, %5 d, a field width of 5 for printing an integer %10 s a field width of 10 for printing a string %6. 2 f a field width of 6 (including the decimal point and the decimal places) with two decimal places. %. 3 f indicate three decimal places n Dr khaled Alqawasmi, 2010 -2011 17

. Cont If field width is wider than necessary, leading blanks are printed. n

. Cont If field width is wider than necessary, leading blanks are printed. n If more decimal places are specified than necessary, trailing zeros are printed. n If the field width is not large enough to print the number, the field width will be increased. n The decimal places will rounded. >>fprintf(‘%3. 2 fn’, 1234. 5678) 1234. 57 n Dr khaled Alqawasmi, 2010 -2011 18

. Cont If we use the %d to print real number; MATLAB will show

. Cont If we use the %d to print real number; MATLAB will show the result using exponential notation >>fprintf(‘%dn’, 1234567. 89) 1. 234568 e+006 n n The value being printed can be left-justified within the field width using a minus sign n >>fprintf(‘the integer is xx%5 dxx and xx%-5 dxxn’, 3. 3) n X’s are just used to show the spacing. Dr khaled Alqawasmi, 2010 -2011 19

. Cont n also; strings can be truncated by specifying decimal spaces. >>fprintf(‘the string

. Cont n also; strings can be truncated by specifying decimal spaces. >>fprintf(‘the string is %s or %. 4 sn’, ‘truncate’, … ‘truncate’) n n There are several special character, such as: n, t, \, ‘’ >>fprintf(‘tabt quote ‘’ tab quote ‘ slash Dr khaled Alqawasmi, 2010 -2011 slash \ n’) 20

Printing Vectors and Matrices To print a column use n in the format string

Printing Vectors and Matrices To print a column use n in the format string >> vec=2: 5; >>fprintf(‘%dn’, vec) n n Note that this was a row vector. A column vector would print exactly the same way, as a column. without the newline character, it would print a row (regardless of whether the vector itself is a row vector or a column vector) but the next prompt would appear on the same line: Dr khaled Alqawasmi, 2010 -2011 21

. Cont A separit newline could be printed to avoid this problem. (as the

. Cont A separit newline could be printed to avoid this problem. (as the following script file) printvec. m n vec=2: 5; fprintf(‘%d’, vec) fprintf(‘n’) >>printvec 2345 >> Dr khaled Alqawasmi, 2010 -2011 22

. Cont If the number of elements in the vector is known, many conversion

. Cont If the number of elements in the vector is known, many conversion character can be specified: >>fprintf (‘%d %d %dn’, vec) n This form is not preferable. n For matrices, MATLAB unwinds the matrix column by column. >> mat= randint(2, 3, [1, 10]) >>fprintf(‘%dn’, mat) n Dr khaled Alqawasmi, 2010 -2011 23

. Cont In the previous statement we use single %d, the matrix will printed

. Cont In the previous statement we use single %d, the matrix will printed in one column. n If three of the %d are specified, the fprintf will pritnt three numbers across on each line of output, but again the matrix is unwound column by column. >>fprintf(‘%d %d %dn’, mat) n If transpose is used, the matrix is printed as it appears when created. >>fprintf(‘%d %d %dn’, mat’) n Dr khaled Alqawasmi, 2010 -2011 24

. Cont For vector and matrices, even though formatting cannot be specified. n The

. Cont For vector and matrices, even though formatting cannot be specified. n The disp function may be easier to use. >> mat=randint(2, 3, [5 15]) >>disp(mat) >>vec=2: 5 >>>disp(vec) n Dr khaled Alqawasmi, 2010 -2011 25

The Plot Function n n n Type help to display help topics that contain

The Plot Function n n n Type help to display help topics that contain graph functions including graph 2 d and graph 3 d. The following script plotpoint. m, plots one point x and y coordinate are specified The point then plotted using a red * the plot then customized by specifying the minimum and maximum values for x and y Then put labels and title Dr khaled Alqawasmi, 2010 -2011 26

Ploypoint. m %simple plot program %create coordinate varables and plot a red '*' x=11;

Ploypoint. m %simple plot program %create coordinate varables and plot a red '*' x=11; y=48; plot(x, y, 'r*') %cjange the axes and label them axis([9 12 35 55]) xlabel('Time') ylabel('Temperature') %put a title on the plot title('Time and Temp') Dr khaled Alqawasmi, 2010 -2011 27

Plot figure Dr khaled Alqawasmi, 2010 -2011 28

Plot figure Dr khaled Alqawasmi, 2010 -2011 28

. Cont n n n All this can be done from the command window

. Cont n n n All this can be done from the command window but it is much easier to use a script In the axis function, one vector is passed. The first two values are the minimum and maximum for the x-axis The last two are the minimum and maximum for the y-axis To be more general the script could prompt the user for the time and temperature Dr khaled Alqawasmi, 2010 -2011 29

. Cont For example axis([x-2 x+2 y-10 y+10]) n n To plot more than

. Cont For example axis([x-2 x+2 y-10 y+10]) n n To plot more than one point, x and y vectors are created to store the values of the (x, y) points for example: (1, 1) (2, 5) (3, 3) (4, 9) (5, 11) (6, 8) Create vector x using colon operator y vector is created with the y values Dr khaled Alqawasmi, 2010 -2011 30

. Cont >>x=1: 6; >>y=[1 5 3 9 11 8] >> plot(x, y) Dr

. Cont >>x=1: 6; >>y=[1 5 3 9 11 8] >> plot(x, y) Dr khaled Alqawasmi, 2010 -2011 31

. Cont Notice: n The points are plotted with straight lines n The axes

. Cont Notice: n The points are plotted with straight lines n The axes are set up according to the data, x values range from 1 to 6 and the y values from 1 to 11. n In this case the x values are the indices of y vector, so you can write: >>plot(y) n Dr khaled Alqawasmi, 2010 -2011 32

Customizing a plot n n n n n You can customize to plot with,

Customizing a plot n n n n n You can customize to plot with, labels, titles. You can add many options for the line types, colors, and so on. The possible colors are: b blue c cyan g green k black m magenta r red y yellow Dr khaled Alqawasmi, 2010 -2011 33

. Cont n n n n The plot symbols, or markers, that can be

. Cont n n n n The plot symbols, or markers, that can be used are: o circle • * star d diamond • v down triangle h hexagram • < left triabgle • > right trangle p pentagram • ^ up trangle + plus • x x-mark. point s square Dr khaled Alqawasmi, 2010 -2011 34

. Cont n n n Line types: -dashed -. dash dot : dotted solid

. Cont n n n Line types: -dashed -. dash dot : dotted solid If no line type is specified, a solid line drawn. Dr khaled Alqawasmi, 2010 -2011 35

Related plot functions n n n Some functions are useful in customizing plots, such

Related plot functions n n n Some functions are useful in customizing plots, such as: clf clears the figure window figure create a new empty figure window, calling figure(n) where n is an integer is a way of creating and maintaining multiple figure windows. hold is a toggle that freezes the current graph in the figure window, so that new plots will be superimposed on the current one. Calling hold once turns the hold on, and then the next time turns it off. Also the command hold on and hold off can be used Dr khaled Alqawasmi, 2010 -2011 36

. Cont n n n legand display strings passed to it in legand box

. Cont n n n legand display strings passed to it in legand box in the figure window. grid display grid lines, call grid turns the grid lines on and off. Also you can use grid on and grid off commands There are many plot types, we will see more in chapter 10. Dr khaled Alqawasmi, 2010 -2011 37