Programming by GIUSEPPE CIABURRO

# Script Matlab

 ```Script Files Script files simply execute a series of commands when invoked in MATLAB. They do not involve the passing of variables. Script files are useful for a variety of things such as problem solving and data analysis. Script files are also useful for entering a long series of commands that may need corrections. To create a script file, you must go into your favorite editor and create a file. If you want to insert comments into the file, you must use a % symbol at the beginning of each line of the comment. Otherwise you type MATLAB commands as you would in the MATLAB window. For instance, to create a script file called squares.m that would calculate the squares from 1 to 30 and plot the number verses its square, you would go into an editor and type: %A script file to calculate the squares from 1 to 30 %and plot the number verses the square. square = [];i = 1; while i < 31 square(i) = i^2; i = i + 1; end plot(square) To invoke this file in MATLAB, you would simply type: >> squares MATLAB will then execute the file. The variables used in the file are global variables and are therefore accessible in the MATLAB worksheet once the file has been executed. This leads to another use of script files, namely storing commonly used variables. Sometimes there is a particular matrix or set of data that you will be using several times in your work. So that you do not have to retype the matrix or data each time you work with MATLAB, you can save the data in a script file. So once again, you must go into an editor to create the file. Below is an example of a file called useful.m created to store a matrix, its determinant and its inverse. %Matrix A, its determinant, and its inverse A = [1 2 3 6 8; 4 2 3 9 10; 15 12 16 14 19; 13 12 15 19 23; 27 32 25 28 15]; DETA = det(A) INVA = inv(A) To bring this matrix and information into the current MATLAB worksheet, you need to type: >> useful The variables A, DETA, and INVA are now accessible to you in MATLAB. Function Files Function files allow you to pass variables for calculating and get a value back. In order for an m-file to be a function file, you must type function at the top of the file. A function file is useful for when the variables may change and may need to be manipulated in a file. Creating a function file is similar to creating a script file, however you must remember to include the word function at the top. It is also important to note that your first set of comments that immediately follow the function line are displayed by MATLAB when you type help functionname. Also the first comment line is what MATLAB uses as a reference when you use the lookfor command. To create a file mean.m that will calculate the mean of an array of numbers, you would go into an editor and type: function [mean] = mean(x) %To calculate the mean or average of a list %x is an array of numbers [m,n] = size(x) if m == 1 m = n; end mean = sum(x)/m; To execute this function in MATLAB and to see what happens when you look for it type: >> w = [67 86 55 98 43 29]; >> [ave] = mean(w) >> lookfor mean You can also create functions that return more than one variable. To do this you must put the variables in braces following the function command. To create a function stats.m that returns the mean, standard deviation, and the variance of an array of numbers, go into an editor and type: function [mean,stde,var] = stats(x) %Mean, Standard Deviation, and Variance %For arrays, stats returns the mean, standard %deviation and the variance respectively. [m,n] = size(x); if m == 1 m = n; end mean = sum(x)/m; stde = sqrt(sum(x.^2)/m - mean.^2); var = stde.^2; To execute this function in MATLAB, you must assign the function to a vector answer to display all values found in the function. >> z = [100 98 45 67 34 54 87 75]; >> [mean,stde,var] = stats(z) >> help stats You can also have a function that accepts more than one input. For instance you can create a file sumprodinv.m in an editor that takes two matrices and finds their sum, product, and the inverse of their product. function [Sum,Prod,Inver] = sumprodinv(A,B); %Finds the sum, product, and product inverse %of two matrices. %To use this function assign the output to %a vector [sum1,prod1,inv1] = sumprodinv(A,B) Sum = A + B; Prod = A*B; Inver = inv(Prod); To call this function in MATLAB: >> A = [2 3 5; 6 8 9; 4 7 2]; >> B = [5 8 3; 2 9 5; 4 3 0]; >> [sum1,prod1,inv1] = sumprodinv(A,B) ```