ENGINEERING 5: ENGINEERING METHODOLOGY
Matlab Reference Summary
>> Matlab prompt
control-C aborts a Matlab calculation in progress
% comments to the right of % are ignored by Matlab
; ; at end of a command suppresses printing
... ... at end of a line continues on the next line
>> quit quits Matlab without saving changes
>> exit quits Matlab without saving changes
>> clc clears command window
>> clf clears current figure
>> clear clears (destroys) all existing variables
>> casesen off turns case sensitivity off (Time = time)
>> casesen turns case sensitivity on (this is the default)
>> who lists variables that you have defined
>> whos lists variables with sizes and imaginary parts
>> size(A) lists the size of a matrix A
>> help lists help topics
>> save saves all variables in file matlab.mat
>> load recovers saved variables from file matlab.mat
>> save data1 A C saves variables A and C in file data1.mat
>> save data2.dat Z /ascii saves variable Z in ascii file format in file data2.dat
>> load data1 restores variables A and C from file data1.mat
>> load data2.dat reads data from file data2.dat and puts it in matrix data2
>> labtest executes commands in file labtest.m (previously created)
>> what shows directory listing of available m-files
>> echo displays commands in m-file as they are executed
>> type labtest displays the contents of file labtest.m without executing it
>> C = [1.5, 3.1, -4; 2.3, -5.8, 12.3; 0, 1, -3.1] creates 3 by 3 matrix C with values shown
>> C(2,1) = 9.2 replaces the 2.3 with a 9.2 in example matrix C above
>> H = 1:8 or H[1:8] generates the vector H = [1,2,3,4,5,6,7,8]
>> H1 = 1:1:8 (start_value=1, increment=1, end_value=8) generates the vector H1 = [1,2,3,4,5,6,7,8]
>> Q = 0.0: 0.5: 2.5 generates the vector Q = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
>> v = linspace(0,pi,111) generates a vector v with 111 values between 0 and pi
>> D = C(:, 1) copies first column of matrix C into vector D
>> x = input(‘Enter values for x’) prompts user for the contents of variable x
>> format long prints numbers using 15 significant digits
>> format short prints numbers using 5 significant digits
>> format long e prints numbers in scientific notation with 15 significant digits
>> format short e prints numbers in scientific notation with 5 significant digits
>> format + prints matrices with only + and - values
>> format compact prints matrices without so much white space between elements
>> format loose opposite of above
>> disp (x) prints contents of x on screen without variable name
>> disp (‘your text here’) prints ‘your text here’ on screen
>> fprintf (‘temp is %4.1f degrees \n’, x) prints ‘temp is 29.2 degrees’ on screen if x = 29.245
>> plot (p,q) plots graph with p vector on abscissa and q vector on ordinate
>> title (‘Graph Title’) puts Graph Title on existing plot
>> xlabel (‘time in sec’) puts label ‘time in sec’ under x-axis of plot
>> ylabel (‘height in m’) puts label ‘height in m’ under y-axis of plot
>> grid draws grid background on plot
>> pause pauses execution of a Matlab program until any key is pressed
>> pause (3) pauses execution of a Matlab program for 3 seconds
special values: pi, i, j, Inf, NaN, clock, date, eps, ans
special matrices: magic (3), zeros (3,2), ones (2,4), eye (3), pascal (5)
Scalar operations: a + b, a - b, a*b, a/b, a\b, a^b
Element-by-element array operations: a + b, a - b, a.*b, a./b, a.\b, a.^b
function output = stat(x)
% function returning two output variables from one input variable
% usually read in from a “.m” file, called “stat.m” in this case
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2)/n);
output(1) = mean;
output(2) = stdev;
function name = dummy(arg1,arg2)
% function returning one array “name” from two input variables
% if read in from “.m” file, would have name “dummy.m”
name(1) = cos(arg1.^2);
name(2) = arg2(1)*arg1;
Numerical integration:
[x,t] = ode23(‘dummy’, start_t, stop_t, initial) % where initial is an array of initial conditions
subplot(2,1,1), plot(t, x(:,1)), xlabel(‘time in s’), ylabel(‘voltage in mV’), title(‘upper plot’)
subplot(2,1,2), plot(t, x(:,2)), xlabel(‘time in s’), ylabel(‘velocity in m/s’), title(‘lower plot’) |