Overview of Programming
Overview of Programming
Overview of Programming
Definition of variables
Variables in MATLAB can be defined with significant flexibility with the programmer (you) in
charge
Variable length maximum recognizable number of characters is 31 or 63 (for a 32-bit and 64-bit
system respectively) and it can be a combination of
a-z; A-Z; 0-9; and the _ the underscore. Other special symbols like @#$% are NOT acceptable
Case sensitive; e.g. aA
First character for a variable must be a letter
Individual variables are stored as such (scalars or real numbers)
A matrix is an m*n (rows, columns) definition and storage of a set of numbers.
A vector (array) is special matrix with one row or one column.
Basic computations with MATLAB
>> 2+6-4
ans =
4
>> ans/2
ans =
2
>> ans/3
ans =
0.6667
2|Page
>> sind(30)
ans =
0.5000
Similarly for other trigonometric functions, cosd, tand etc.
log(3.2) = 1.1632
>>log10(x)
log10(3.2) = 0.5051
Differentiate between the inverse of logarithmic scales
>>exp(x)
exp(1.1632) = 3.200
>>10x
10^(0.5051) = 3.1996
4|Page
[Ret]
>>x=[1 3 0 -1 5];
[Ret]
would result in no output in the command window because the semicolon suppresses the
output. One of the ways to see the numerical value of the variable x is to issue a specific display
command like
>>disp(x) [Ret]
or simply type:
>>x
[Ret]
To display x as a column array, use the transpose operator (the apostrophe), or semicolons to
separate entries of the array
>>x=[1 3 0 -1 5]
or
>>x=[1;3;0;-1;5]
What would you if you forgot the space between a and b as follows:
>>c=[a-b]
Manual entry of matrices can be accomplished separating column entries by spaces and row entries by
semicolons for example:
>> A=[1 2 3;4 5 6;7 8 9] %would result in the 3X3 matrix echoed below
A=
1
Matrices can also be created using built-in functions e.g. eye, diag, ones, linspace, etc.
We will explore these throughout the class.
Say you want to define a 3X4 matrix with ones in it,
>>nr=3
%nr=number of rows
>>nc =4
%nc=number of columns
>>mymat=ones(nr,nc)
mymat =
1
6|Page
>>x=linspace(0,pi,46)
7|Page
>>x=linspace(0,3*pi,76)
Array operations
When faced with a series of computations that could be accomplished using loops, coding
errors or blunders are possible. Even if one is successful in debugging the code completely,
accomplishing the computations is quite sluggish.
Consider the case where you need to compute the cosine of 5 equally spaced values of x as
follows:
cos(x) for 0 x
By observation you notice that you will need 5 locations along x starting at 0 ending at ,
resulting in the distance between stations of 0.25
In MATLAB once you define the range of values of x they are interpreted and saved as a vector,
in the next command MATLAB computes the 5 values of cos(x) at once as an array operation.
>> x=0:pi/4:pi
x=
0 0.7854 1.5708 2.3562 3.1416
8|Page
>> y=cos(x)
y=
1.0000 0.7071 0.0000 -0.7071 -1.0000
Element by element operations are accomplished with the aid of .*, ./ and .^ for array
multiplication, division and exponentiation respectively.
>> x=[1 2 3]; y=[4 5 6];
>> w=x.*y
w=
4 10 18
>> v=x./y
v=
0.2500 0.4000 0.5000
R=
9 14 24
24 25 24
21 16
9|Page
ans =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
2.6458 2.8284 3.0000
>> Q.^2
ans =
81 64 49
36 25 16
9
When matrix or array dimensions are not equal MATLAB responds with an error message
>> Q1=[9 8 7;6 5 4];
>> R1=P.*Q1
??? Error using ==> times
Matrix dimensions must agree.
10 | P a g e
11 | P a g e
>>plot(x,y,'*')
13 | P a g e
Alternative scaling
Ref. Appendix B, from p. 636
Experiment with the linspace command and different plotting commands which effectively
scales the axes
x=linspace(0,3);
y=10*exp(-2*x);
plot(x,y) %use semilogy, semilogx, loglog in the place of plot for plotting options
grid on
Plot of x y
14 | P a g e
Semilogy plot. Note y axis has been rescaled to the log scale
Try plotting with semilogx and loglog
Multiple plots
Ref. Chapter 5
Syntax for subplots
subplot(nrows,ncols,thisplot)
plot(this plot); include all plotting annotations
Create a script file with commands shown on p. 69 Recktenwald
x is defined once
x = linspace(0,2*pi);
subplot(2,2,1);
plot(x,sin(x)); axis([0 2*pi -1.5 1.5]); title('sin(x)');
15 | P a g e
subplot(2,2,2);
plot(x,sin(2*x)); axis([0 2*pi -1.5 1.5]); title('sin(2x)');
subplot(2,2,3);
plot(x,sin(3*x)); axis([0 2*pi -1.5 1.5]); title('sin(3x)');
subplot(2,2,4);
plot(x,sin(4*x)); axis([0 2*pi -1.5 1.5]); title('sin(4x)');
Insert multiple plots
16 | P a g e