Matlab Guide Essay

Submitted By LiamLam
Words: 1323
Pages: 6

Matlab 101
Contents

% CRTSimpleIf
% A short piece of code to illustrate the use of if.
% CRT 2012

clc clear % The statements clc and clear are often useful in testing code so you can
% see what is going on. Of course, you need to be sure there is nothing in
% the Workspace you want to keep!

reply = input('Continue? Y/N [Y] ','s');
% Note the 's' which tells MATLAB that the input is a string (text).
% Note two choices are offered: Y/N and Y is the default if nothing is
% entered.

if isempty(reply)
% If the user presses Enter, reply is an empty or "null" vector.
% isempty is a check on this. If reply is empty, it is given the
% default value. reply = 'Y'; % Default. end if strncmpi(reply,'Y',1) % reply is Y or y disp('All you need is love') else % not Y or y disp('Bye') end % CRTSimpleIfElseif
% This is some simple code showing how if .. elseif .. else .. end can be
% used to make choices.
% CRT 2012

clc clear % The statements clc and clear are often useful in testing code so you can
% see what is going on. Of course, you need to be sure there is nothing in
% the Workspace you want to keep!

userVal = input('What is your value? '); if userVal < 0 disp('Negative') elseif userVal == 0 % Notice logical operator disp('Your value is zero') else disp('Positive') end % CRTSimpleSwitch
% switch is useful if you have a lot of cases to choose between. It can
% save writing lots of if statements and is very useful if your choices are
% text. Here the choices are the values of the discriminat of a quadratic
% equation.
% CRT 2012

clc clear % The statements clc and clear are often useful in testing code so you can
% see what is going on. Of course, you need to be sure there is nothing in
% the Workspace you want to keep!

disp('What type and number of roots does the general quadratic ax^2 + bx + c have ') disp('for user input values of the parameters?') disp(char(10)) % char(10) is ASCII code for new line

params = input('Input a vector of values for the parameters in the form [a b c] ');
% Note that input will accept a vector.
% The parameters are elements of the input vector. a = params(1); b = params(2); c = params(3);

discrim = b^2-4*a*c;

% Because of rounding errors, a discriminant that should be zero might be
% not be exactly that. It is dangerous to compare calculted floating point
% numbers with zero as we will later.
% However, we can compare the discriminant with eps, which is the "distance
% from 1.0 to the next largest double-precision number. eps = 2^(-52). That
% should allow for rounding errors.

% D = 1-0.2-0.2-0.2-0.2-0.2
% This code was used to test the following check works properly. It is now
% redundant but is intersting. Remove the comment symbol to try this.

if abs(discrim) 10000
% | in MATLAB means OR. || is often slightly better (quicker) because it
% tells MATLAB that if the first condition is met (i.e. numLoops < 1),
% it shouldn't bother checking the second condition as filure has already
% occurred.

numLoops = input('How many loops do you want? '); % input is a good way to get values from the user.

end

for index = 1:numLoops disp(['This is loop ' num2str(index)]); end % disp can only have one input argument. To combine text and a value use
% num2str to convert the number to a string (vector of characters i.e. more
% text)and put all the text in a vector i.e. in [ ] as the input to disp.

% It is nice to tell the user why the input value is unacceptable and to
% give a " default" value so the user can just press Return to proceed.
% See CRTBasicForWhileIf for one way to do this.

% CRTBasicForWhileIf
% This is a short piece of code showing the use of a for loop and some
% error checking involving a while loop. if is used so the user can be told