Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computer Applications in Engineering Design: Introductory Lecture

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 49

Computer Applications in

Engineering Design
MATLAB
AutoCAD
Visio
Rational Rose
LabVIEW
PSPICE
Orcad

Introductory Lecture

Course Syllabus
MATLAB
Orcad
Visio
Pspice
AutoCAD
Rational Rose
LabVIEW
UML

Tools

Matlab and Orcad is used for electrical/computer systems design

AutoCAD like design tools are taught for 3D engineering drawings.

Introduction to computer-aided design tools including AutoCAD,


OrCAD, MATLAB, LabVIEW, Rational Rose and Visio, etc.
Study of theoretical concepts of electronic components and circuits
using simulation softwares: PSPICE, MATLAB, and LabVIEW.
Tools like Visio and Rational Rose are used for software drawing like
process diagrams, class diagram, sequence diagram, interaction
diagrams and deployment diagram, Entity-Relationship diagram etc.
Design of software designs using Visio and Rational Rose for
understanding and implementing object oriented designs and
standards like UML.

Introduction to
Matlab
Click on the Matlab
icon/start menu
initialises the
Matlab
environment:

Variable
browser

Command
window

The main window is


the dynamic
command
interpreter which
allows the user to
issue Matlab
commands
The variable
browser shows
which variables

Command
history

Matlab Programming
Environment
Matlab (Matrix Laboratory)
is a dynamic, interpreted,
environment for
matrix/vector analysis
Variables are created at runtime, matrices are
dynamically re-sized,
User can build programs (in
.m files or at command
line) using a C/Java-like
syntax
Ideal environment for model
building, system
identification and control
(both discrete and

Variables
No need for types. i.e.,
int a;
double b;
float c;
All variables are created with double precision
unless specified and they are matrices.
Example:
>>x=5;
>>x1=2;
After these statements, the variables are 1x1
matrices with double precision

Basic Matlab
Example
a=[1,2,3,4,5];
b=[a;a;a]

Example
a=[1;2;3;4;5];
b=[a,a,a]

b=1 2 3 4 5
12345
12345

b= 1 1 1
222
333
444
555

Example (transpose)
a=[1;2;3;4;5];
b=a
b= 1 2 3 4 5

Input/Output Statements

Input Function

Keyboard Command

Menu Function

Pause Command

Two Dimensional Plots


Topics Covered:
1. Plotting basic 2-D plots.

The plot command.

The fplot command.

Plotting multiple graphs in the same plot.

Formatting plots.

MAKING X-Y PLOTS

MATLAB has many functions and commands that can be used to


create various types of plots.

In our class we will focus on two dimensional x y plots.

EXAMPLE OF A 2-D PLOT

Plot title

Light Intensity as a Function of Distance

1200

y axis
label

Legend
Theory
Experiment

1000

Text

Tick-mark

INTENSITY (lux)

800
Comparison between theory and experiment.
600

400

Data symbol

200

10

12

x axis
label

14

16
DISTANCE (cm)

18

20

22

24

Tick-mark label

TWO-DIMENSIONAL plot() COMMAND


The basic 2-D plot command is:

plot(x,y)
where x is a vector (one dimensional array), and y is a vector.
Both vectors must have the same number of elements.
The plot command creates a single curve with the
the abscissa (horizontal axis) and the
(vertical axis).

x values on

y values on the ordinate

The curve is made from segments of lines that connect the


points that are defined by the
elements in the two vectors.

and

coordinates of the

CREATING THE X AND Y VECTORS

If data is given, the information is entered as the elements of


the
vectors x and y.

y are determined by a function from the values


of x, than a vector x is created first, and then the values of y
are calculated for each value of x. The spacing (difference)
between the elements of x must be such that the plotted curve

If the values of

will show the details of the function.

PLOT OF GIVEN DATA


Given data:
x

7.5

10

6.5

5.5

A plot can be created by the commands shown below. This can be


done in the Command Window, or by writing and then running a
script file.
>> x=[1 2 3 5 7 7.5 8 10];
>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)
Once the plot command is executed, the Figure Window opens with
the following plot.

PLOT OF GIVEN DATA

LINE SPECIFIERS IN THE plot() COMMAND

Line specifiers can be added in the plot command to:


Specify the style of the line.
Specify the color of the line.
Specify the type of the markers (if markers are desired).

plot(x,y,line specifiers)

LINE SPECIFIERS IN THE plot() COMMAND

plot(x,y,line specifiers)
Line
Style
Solid
dotted
dashed
dash-dot

Specifier
:
--.

Line
Color

Specifier

red
green
blue
Cyan
magenta
yellow
black

r
g
b
c
m
y
k

Marker Specifier
Type
plus sign
circle
asterisk
point
square
diamond

+
o
*
.
s
d

LINE SPECIFIERS IN THE plot() COMMAND

The specifiers are typed inside the plot() command as strings.


Within the string the specifiers can be typed in any order.
The specifiers are optional. This means that none, one, two, or
all the three can be included in a command.
EXAMPLES:
plot(x,y)
A solid blue line connects the points with no markers.
plot(x,y,r) A solid red line connects the points with no markers.
plot(x,y,--y)A yellow dashed line connects the points.
plot(x,y,*) The points are marked with * (no line between the
points.)
plot(x,y,g:d)A green dotted line connects the points which are
marked with diamond markers.

PLOT OF GIVEN DATA USING LINE


SPECIFIERS IN THE plot() COMMAND
Year

1988

1989

1990

1991

1992

1993

1994

Sales (M)

127

130

136

145

158

178

211

>> year = [1988:1:1994];


>> sales = [127, 130, 136, 145, 158, 178, 211];
>> plot(year,sales,'--r*')

Line Specifiers:
dashed red line and
asterisk markers.

PLOT OF GIVEN DATA USING LINE


SPECIFIERS IN THE plot() COMMAND

Dashed red line and


asterisk markers.

CREATING A PLOT OF A FUNCTION


0.5 x

cos(6 x)
Consider: y 3.5
A script file for plotting the function is:

for 2 x 4

% A script file that creates a plot of


% the function: 3.5^(-0.5x)*cos(6x)
x = [-2:0.01:4];
y = 3.5.^(-0.5*x).*cos(6*x);
plot(x,y);

Creating a vector with spacing of 0.01.


Calculating a value of y
for each x.

Once the plot command is executed, the Figure Window opens with
the following plot.

A PLOT OF A FUNCTION

y 3.5

0.5 x

cos(6 x)

for 2 x 4

CREATING A PLOT OF A FUNCTION


If the vector x is created with large spacing, the graph is not accurate.
Below is the previous plot with spacing of 0.3.

x = [-2:0.3:4];
y = 3.5.^(-0.5*x).*cos(6*x);
plot(x,y)

THE fplot COMMAND


The fplot command can be used to plot a function
with the form: y

= f(x)

fplot(function,limits)
The function is typed in as a string.
The limits is a vector with the domain of x, and optionally with limits
of the y axis:

[xmin,xmax]

or

[xmin,xmax,ymin,ymax]

Line specifiers can be added.

PLOT OF A FUNCTION WITH THE fplot() COMMAND

y x 2 4 sin( 2 x) 1

A plot of:
>> fplot('x^2 + 4 * sin(2*x) - 1', [-3 3])

for 3 x 3

PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT


Plotting two (or more) graphs in one plot:
1. Using the plot command.
2. Using the hold

on, hold off commands.

USING THE plot() COMMAND TO PLOT


MULTIPLE GRAPHS IN THE SAME PLOT

plot(x,y,u,v,t,h)
Plots three graphs in the same plot:

y versus x, v versus u,

and h versus t.

By default, MATLAB makes the curves in different colors.


Additional curves can be added.
The curves can have a specific style by adding specifiers after
each pair, for example:

plot(x,y,-b,u,v,r,t,h,g:)

USING THE plot() COMMAND TO PLOT


MULTIPLE GRAPHS IN THE SAME PLOT

y 3x 3 26 x 10 and its first and second


2 x 4 , all in the same plot.

Plot of the function,


derivatives, for
x = [-2:0.01:4];
y = 3*x.^3-26*x+6;
yd = 9*x.^2-26;
ydd = 18*x;

vector x with the domain of the function.


Vector
2 x y
4 with the function value at each x.
Vector yd with values of the first derivative.

Vector ydd
with values of the second derivative.
2 x 4
plot(x,y,'-b',x,yd,'--r',x,ydd,':k')

Create three graphs, y vs. x (solid blue


line), yd vs. x (dashed red line), and ydd
vs. x (dotted black line) in the same figure.

USING THE plot() COMMAND TO PLOT


MULTIPLE GRAPHS IN THE SAME PLOT
120
100
80
60
40
20
0
-20
-40
-2

-1

USING THE hold on, hold off, COMMANDS


TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT

hold on

Holds the current plot and all axis properties so that


subsequent plot commands add to the existing plot.

hold off Returns to the default mode whereby plot commands


erase the previous plots and reset all axis properties
before drawing new plots.

USING THE hold on, hold off, COMMANDS


TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT

y 3 x 3 26 x 10 and its first and second


2 x 4 all in the same plot.

Plot of the function,


derivatives, for

x = [-2:0.01:4];
y = 3*x.^3-26*x+10;
yd = 9*x.^2-26;
ydd = 18*x;

First graph is created.

plot(x,y,'-b')
hold on
plot(x,yd,'--r')
plot(x,ydd,':k')
hold off

Two more graphs are created.

EXAMPLE OF A FORMATTED 2-D PLOT


Plot title
Light Intensity as a Function of Distance

1200

Legend

Theory
Experiment

y axis
label

1000

Text

Tick-mark

INTENSITY (lux)

800
Comparison between theory and experiment.
600

400

Data symbol

200

10

12

x axis
label

14

16
18
DISTANCE (cm)

20

22

24

Tick-mark label

FORMATTING PLOTS

A plot can be formatted to have a required appearance.


With formatting you can:
Add title to the plot.
Add labels to axes.
Change range of the axes.
Add legend.
Add text blocks.
Add grid.

FORMATTING PLOTS

There are two methods to format a plot:


1. Formatting commands.
In this method commands, that make changes or additions to
the plot, are entered after the plot() command. This can be
done in the Command Window, or as part of a program in a
script file.
1. Formatting the plot interactively in the Figure Window.
In this method the plot is formatted by clicking on the plot and
using the menu to make changes or add details.

FORMATTING COMMANDS
title(string)
Adds the string as a title at the top of the plot.

xlabel(string)
Adds the string as a label to the x-axis.

ylabel(string)
Adds the string as a label to the y-axis.

axis([xmin xmax ymin ymax])


Sets the minimum and maximum limits of the x- and y-axes.

FORMATTING COMMANDS
legend(string1,string2,string3)
Creates a legend using the strings to label various curves
(when several curves are in one plot). The location of the
legend is specified by the mouse.

text(x,y,string)
Places the string (text) on the plot at coordinate x,y relative to
the plot axes.

gtext(string)
Places the string (text) on the plot. When the command
executes the figure window pops and the text location is clicked
with the mouse.

EXAMPLE OF A FORMATTED PLOT

Below is a script file of the formatted light intensity plot (2nd slide).
x=[10:0.1:22];

Creating vector x for plotting the theoretical curve.

y=95000./x.^2;

Creating vector y for plotting the theoretical curve.

xd=[10:2:22];

Creating a vector with coordinates of data points.

yd=[950 640 460 340 250 180 140];


plot(x,y,'-','LineWidth',1.0)
hold on
plot(xd,yd,'ro--','linewidth',1.0,'markersize',10)
hold off

Creating a vector with


light intensity from data.

EXAMPLE OF A FORMATTED PLOT

Formatting of the light intensity plot (cont.)


xlabel('DISTANCE (cm)')

Labels for the axes.

ylabel('INTENSITY (lux)')

Title for the plot.

title('\fontname{Arial}Light Intensity as a Function of


Distance','FontSize',14)
axis([8 24 0 1200])
text(14,700,'Comparison between theory and
experiment.','EdgeColor','r','LineWidth',2)

Setting limits of the axes.


Creating text.
Creating a legend.

legend('Theory','Experiment',0)
The plot that is obtained is shown again in the next slide.

EXAMPLE OF A FORMATTED PLOT

FORMATTING A PLOT IN THE FIGURE WINDOW


Once a figure window is open, the figure can be formatted interactively.
Use the insert menu to

Use Figure,
Axes, and
Current ObjectProperties in
the Edit menu

Click here to start the


plot edit mode.

SUB-PLOTS

i=1:4;
v=4*i;
subplot(2,2,1);
plot(i,v);
x=[1 2 3 4];
y=[1 4 9 16]
subplot(2,2,2)
plot(x,y);
t=0:pi:2*pi;
subplot(2,2,3)
plot(t,sin(t));

20

20

15

15

10

10

-16

x 10

0
-2
-4

Specialized Two dimensional Plots

cos-x

x=0:0.02:4*pi;
y=cos(x);
area(x,y);
xlabel(x-axis);
ylabel(cos-x);
title(graph to plot y=cos(x)
using area function);

graph to plot y=cos(x) using area function

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

6
x-axis

10

12

Bar Function

bar(y)
bar(x,y)
bar(x,y,style option)
bar3(x,y,g)

graph to show bar function

30

25

y-label

20

x=[0 1 2 3 4 5 6];
y=[10 15 25 20 30 27 19];
bar(x,y,c);
xlabel(x-axis);
ylabel(y-label);
title(graph to show bar
function);

15

10

3
x-axis

Barh Function

graph to show barh function

6
5
4
y-label

x=[0 1 2 3 4 5 6];
y=[10 15 25 20 30 27 19];
barh(x,y,g);
xlabel(x-axis);
ylabel(y-label);
title(graph to show barh
function);

3
2
1
0

10

15
x-axis

20

25

30

Pie Function
pie chart showing concentration of different industry

pie(x)
pie(x,k)
pie(x,k,labels)

8%
20%
16%

industry=[4 8 20 2 7 10];
pie(industry);
title(pie chart showing
concentration of different
industry)

14%

4%

39%

industry=[4 8 20 2 7 10];
show=[0 0 0 1 0 1];
pie(industry,show);
title(pie chart showing concentration of different industry);
legend(cement,textile,software,chemical,telecom,Banking,3)

Pie Function

pie chart showing concentration of different industry


8%

20%

16%

14%
cement
textile
software
chemical
telecom
Banking
39%

4%

Stairs Function
stairs plot to show function

y-label

x=-2:0.1:2;
3.5
y=x.*x;
3
stairs(x,y,'r');
2.5
grid on;
2
xlabel('x-axis');
ylabel('y-label');
1.5
title('stairs plot to show function')1
0.5
0
-2

-1.5

-1

-0.5

0
x-axis

0.5

1.5

Stem Function

x=[0 1 2 3 4 5 6]
y=[3 -1 -6 4 5 2 3]
stem(x,y);

-2

-4

-6

Three Dimensional Plot

illustration of plot3 function

4
2
z-axis

t=-4:0.1:4;
x=t.^2;
y=4*t;
plot3(x,y,t);
grid on;
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('illustration of plot3
function)ction')

0
-2
-4
20
10
0
-10
y-axis

-20

10
x-axis

15

20

You might also like