%% Creating Variables in Matlab
% In this lab, you will learn how to create variables in Matlab and how to
% create "anonymous functions".
%% Creating variables in Matlab
% So far, we have only entered expressions at the command line, and
% obtained numerical results. However the real power of any programming
% language comes from its ability to compute values, store them in
% named variables, and use these stored values in later computations.
% On a graphing calculator, you might have used "memory" buttons
% for this.
% Before we start, we are going to clear all global memory to make sure % we are starting with a clean workspace. We discuss the clear keyword below. % clear all %% % % We will also set the formatting to the "short, scientific notation" style % Recall that this style prints four digits after the decimal place, rather than the % 16, used in the "long" format. % format short e %% % % In Matlab, we store values using an assignment operator % = in the following manner : % x = 5 %% y = 7 %% % % The values x and y % are now stored in % Matlab's "global memory", and as long as they are in memory, they % can be recalled for later use and can participate in any of the % arithmetic operations that we have so far described or used % as arguments to functions. For example, we could now make the % assignment % z = x + y %% % % Variable names can be created using the following rules: %
Here are some more examples of variable names. % x1 = 5 %% y2 = -25 %% pressure = 1000.013 %% Density = 1.01 %% x_velocity = -56.45 %% y_veloctiy = 12.0 %% Latitude = 180.1 %% latitude = -57.8 %% alpha_1 = sqrt(pi) %% beta_2 = 1/pi %% big_number = 1e56 %% small_number = 1e-100 %% Word of caution in choosing variable names % % Matlab will also allow you to % redefine reserved keywords. For example, we can set % pi = 3 %% % % If we were to now evaluate cos(pi), we would get % bad_value = cos(pi) %% % % instead of the expected value of -1. Worse yet, we can % redefine the cos function itself : % cos = 4.5 %% % %
If later in our program, we evaluate the cosine function, % we should expect an error.%
% >> cos(pi) %%
% Index exceeds matrix dimensions. %% %% % % The precise meaning of this error may not yet be clear (basically, Matlab % thinks you are trying to find the third element of the array cos) but it should be obvious why redefining keywords % can lead to mysterious errors that can be hard to track down. % %% Clearing and listing variables from memory % % Suppose we want to restore the original meaning of % the Matlab keyword cos. We can do this by % "clearing" our definition from global memory. Let's also clear our % definition of pi as well % clear cos pi %% % % When we try our cos function again, we % get the expected result. % correct_value = cos(pi) %% % % Clearing the variables cos and pi % restored the original meaning of these keywords. We can also clear any % variables we previously defined. But before we do so, let's see what % we have currently stored in memory using the % who command % who %% % % A similar command whos shows you more % detail about each variable in your workspace: % whos %% % % This listing shows that each variable we have so far defined is a scalar (a '1x1' % array), it occupies 8 bytes, and it is of type double (Matlab uses the % term 'class' instead of the more familar 'type'). %
% >> Density %%
% Undefined function or variable 'Density'. %% %% % % To clear all the variables in memory, we use the command % clear all %% % % As you might expect, the whos has nothing to % show us once we have cleared all variables from memory. % whos %% Creating 'anonymous' functions in Matlab % % In many cases, you will want to evaluate expressions multiple times using % different values of the variables. While you might be able to cut and % paste the expressions into a script multiple times, this is error prone. % It is much better to create a "function" that can be called multiple % times using different arguments. %
% >> help function_handle % ............ % FUNHANDLE = @(ARGLIST)EXPRESSION constructs an anonymous function and % returns a handle to that function. The body of the function, to the % right of the parentheses, is a single MATLAB expression. ARGLIST is a % comma-separated list of input arguments. Execute the function by % calling it by means of the returned function handle, FUNHANDLE. For % more information on anonymous functions, see "Types of Functions" in % the MATLAB Programming documentation. % % To call the function referred to by a function handle value, use ordinary % parenthesis notation. That is, specify the function handle variable % followed by a comma-separated list of input arguments enclosed in % parentheses. For example, HANDLE(ARG1, ARG2, ...). To call a % function_handle with no arguments, use empty parenthesis, e.g., % HANDLE(). % ............ %% %% Lab exercises % %