Scalar and Array Operations

 

Scalar Operations

 

·       Try the following commands in the command window:

 

W=5+6

X=5*6

Y=5/6

Z=X^2

 

·       You can also use various functions with scalar inputs

 

Z1=sin(X)

Z2=exp(X)

Z3=atan(X)

Z4=log(X)          % Natural log - base e

Z5=log10(X)      % Log base 10

 

Etc…

 

Array Operations

 

·       Now try these array operations

 

X=[1:1:5]
Y=[-2:1:2]

size(X)

size(Y)

length(X)

length(Y)

 

Z1=X.*Y

Z2=X./Y

Z3=X.^3

Z4=X+Y             % don’t use .+

 

·       You can also use various functions with array inputs (and get array outputs).  Most, but not all, functions work with array inputs, so be careful.

 

Z1=sin(X)

Z2=exp(X)

Z3=atan(X)

Z4=log(X)          % Natural log - base e

Z5=log10(X)      % Log base 10

 

Etc…

 

·       Below are two examples from the text which use array operations

 

% Example on page 46

% Speed and time for a 4 leg flight

speed=[200 250 400 300];  % mph

time=[2 5 3 4];                      % hours

dist=speed.*time                  % miles

sum(dist)  % total distance traveled (miles)

 

 

% Example 2.3-2  (Pg. 50)

% Power calculations in resistive circuits

R=[1e4 2e4 3.5e4 1e5 2e5];

V=[120 80 110 200 350];

current=V./R

power=V.^2./R

totalpower=sum(power)

 

 

Mixtures of scalars and vectors

 

·       Try these commands

 

% same output

Z=3*X

Z2=3.*X

 

% same output

Z3=X/3

Z4=X./3

 

% only the second form is valid

Z3=3/X   % does not work

Z4=3./X  % OK

 

% adds 3 to each element

Z5=X+3

 

If ever in doubt, use:  .*  ./   .^

Always use +

 

Searching for Maxima and Minima

 

·       Set a few simple arrays (say position x at various times t)

 

t=[0,1,2,3,4,5];

x=[2,3,1,7,8,0];

plot(t,x)

 

·       Now find the max and the time where the max occurs

 

[maxx,nmax]=max(x);

nmax                             %  index of maximum

maxx                             %  maximum value of x

tmax=t(nmax)             %  time maximum occurs

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


·       Now find the min and the time where the min occurs

 

[minx,nmin]=min(x);

nmin                              %  index of minimum

minx                              %  minimum value of x

tmin=t(nmin)               %  time minimum occurs

 

·       Compare these results with visual inspection of the plot.  Does it make sense?

 

 

 

In Class Challenge

 

·       Plot the function

 

v(t)=-.1t3+t2+5

 

for 0<t<10 (t is time in seconds).  Determine the maximum value and time where the maximum occurs for each, over the range indicated.

 

Hint: 

 

1.    Open a NEW m-file (save it as ‘myplot.m’).  When you are finished editing, execute your m-file by typing the name of the m-file at the command line (without the .m extension).  Be sure to resave the file every time you make a change.

 

2.    First, in your m-file, generate an array t over the desired range.  Use many t values to get a smooth plot (1000 points is good).  I suggest you use linspace( ) to generate the t vector. 

 

3.    Generate an array v, using the necessary array operators, operating on the newly created t.  You will need to successfully convert the mathematical expression above into proper MATLAB syntax (v=?).  Plotting key equations from your textbooks and class notes will greatly enhance your intuition and understanding of engineering.  

 

4.    Use plot(t,v) to see your graph.  You can see by inspection what the maximum value is and where it occurs.

 

5.    Use [maxval,index]=max(v); to find the exact maximum value (maxval) and the array index (index) where the maximum occurs.  Note, the time we are looking for is the value of the element in the t array corresponding to the maximum element in v (same position in the t array as the maximum value is in v).   Use t(index) to get the correct time.

 

 

 

 

·       Now, approximate the area under this curve:

 

delta=t(2)-t(1)

Area=sum(v*delta)

 

·       Do you see why this works?  Look at the picture below to help you understand what is happening.  You are adding up rectangular areas (height * width) under the curve.  This is called numerical integration and it is only a numerical approximation.  Calculus is required to get exact values, but some equations don’t have closed form integrals and numerical integration is the only way.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Additional Problem

 

·       Want to try another one?  Sure you do…

 

v(t)= t exp(-2t)

 

exp( ) is the exponential function e-2t where e=2.7183…

 

 

Plot this for 0<t<10 (t is time in seconds).  Determine the maximum value and time where the maximum occurs for each, over the range indicated.  Also approximate the area under your plotted curve.