foundation course term 2 project: engineering software 1. introduction

advertisement
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
FOUNDATION COURSE
TERM 2 PROJECT: ENGINEERING SOFTWARE
1. INTRODUCTION
Before an engineering system is built, there are various steps in the
design process that need to take place. In one of these stages,
computers are used to simulate how a product will behave based on a
mathematical description (called a mathematical model). For
example, before an F1 car is built, each of its systems and subsystems
would have been mathematically modelled (i.e. a set of complex
equations that show mathematically how the output of a system will
react when stimulated by a number of inputs). These mathematical
models would then be programmed into software packages where such
equations can be represented as a program or in graphical form (Fig
1.1). The software then performs the necessary calculations to help
test how the system will behave under various conditions.
Fig 1.1
As the system is being tested using software, any problems in the
design can be remedied by making amendments to the mathematical
model in order to get the system to behave in the desired way.
As is evident, computers and accompanying software can greatly help
in the design process of engineering systems. By the time a prototype
is built, many of the design problems will have been solved using
software packages, and their solutions tested.
Three software packages will be looked at in this project. They are
MATLAB, MULTISIM and LABVIEW. Each is used extensively in
engineering, and are particularly useful for electrical and electronics
circuit design.
FOUNDATION YEAR, CITY UNIVERSITY
1 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
MATLAB
MATLAB is a very powerful mathematical computation tool. It has its
own programming language as well as being a numerical computing
environment. MATLAB can also display information graphically. It
contains hundreds of pre-programmed commands that can perform
mathematical functions. These commands are similar to the way
engineering steps are expressed in mathematics.
MULTISIM
MULTISIM is simulation based software. It has the ability to perform
circuit simulation that can model the behaviour of a particular analogue
or digital circuit. It gives the circuit designer the capacity to model any
conceivable circuit design, examine the corresponding circuit at
particular components, or probe the behaviour of the entire circuit by
performing certain tests. Within MULTISIM there is access to
thousands of parts and components. This means that in addition to not
actually having the components physically, you can have limitless
combinations of circuit designs.
LABVIEW
LABVIEW is short for Laboratory Virtual Instrumentation Engineering
Workbench. It is a graphical system design software designed with the
intention of helping engineers and scientists work with real world
signals and visually analyse the resultant data. It contains extensive
libraries of functions for many programming tasks. It differs from text
based languages because it used a graphical programming language
to create programs in block diagram form. A LABVIEW program is
referred to as a virtual instrument because its appearance and
operation can imitate an actual instrument.
FOUNDATION YEAR, CITY UNIVERSITY
2 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
2. LAB STRUCTURE
This laboratory based project will be split into three sections. Each
section lasts three weeks. In the first section, MATLAB will be studied,
followed by MULTISIM in the second and LABVIEW in the third and
final section – giving a total of 9 weeks lab time (27 hours).
You are required to bring with you a USB flash stick to store your files,
and also a lab book to keep a record of your findings. Lab books will be
signed at the end of each lab session, as well as a register being
taken.
The lab will be assessed on attendance, a formal lab report and a
formal presentation to be held in the lab in week 10.
The formal lab report will be due two weeks after the presentations are
held and should be handed in to the General Office no later than
Monday 20th April. Guidelines on how to compile a formal report can be
found at http://www.staff.ac.uk/eleclab . Further information about the
structure of the report will be given throughout the duration of the lab.
You will work in pairs, and both group members must have a lab book,
and submit a report each. The presentation will last approximately 10
minutes, and each group member will talk about one of the software
packages studied (you are not permitted to both talk about the same
one!). Details of the structure of the presentation will be given nearer
the time.
Do not forget the importance of your lab book. As a future engineer, it
is vital to adopt good habits early in your career. Making notes of your
observations in your lab book will improve your chances of submitting a
high quality formal report, and will also aid in your preparations for the
end of term presentations.
As with all sessions carried out in the lab, please adhere to the
guidelines that have been set out with regards to lab health and safety
rules, as well as the expected professional manner in which you are to
conduct yourselves when on these premises.
Finally, my lab assistants and I will be on hand to help in any way we
can. However, we will not type any code for you, nor will any answers
be given, as we will just point you in the right direction. All the best with
your efforts.
FOUNDATION YEAR, CITY UNIVERSITY
3 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
3. SECTION 1 – WEEKS 1 TO 3 - MATLAB
3.1 WEEK 1 - TASK 1
3.1.1 Basics
This first task will consist of a basic exercise that will lead to becoming
familiar with the MATLAB environment.
In order to start the program, locate the MATLAB icon (desktop or start
menu) and double click. This opens the window as shown in Figure
3.1.
Fig. 3.1
There will be several display windows, including the large command
window on the right, and command history and current directory
windows. There are also tabs which allow you to access hidden
windows.
MATLAB can be used in two basic modes. The command window
allows you to save the value you calculate, but not the commands used
to generate those values. If you want to save a sequence of command
windows, you will need to use the editing window to create an m-file.
FOUNDATION YEAR, CITY UNIVERSITY
4 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
An m-file is a MATLAB file that contains a sequence of programming
code).
To begin with, you will concentrate on using the command window,
where calculations can be performed in a manner very similar to those
made on a scientific calculator. Type
5^2
The following output will be displayed
ans =
25
Make a note in your lab book (as you should with all the procedures
you are asked to follow) of this, and suggest the mathematical
operation performed. Also experiment with other arithmetic operators
(+, - , / and *).
Next, type in the command window
sqrt(49)
This obviously returns the square root of 49.
So, as you can see from these fundamental commands, MATLAB
appears to be a very powerful calculator, but it is capable of much
more as we shall see.
The command history window records the commands used in the
command window. When exiting MATLAB, or when typing clc in the
command window, the command window is cleared, yet the command
history retains a list of all your commands, and can only be cleared
under the edit toolbar menu. Type
clc
This clears the command window, but the data in the command history
window is intact. You can transfer any command from the command
history window to the command window by double clicking the desired
command, or by dragging it across to the command window.
Experiment with this, and note your observations.
The workspace window (top left window – right tab at the bottom)
keeps track of the variables you have defined as you execute
commands in the command window. Once a mathematical operation is
carried out, the answer is stored in a variable called ans. For example,
type
6*8
FOUNDATION YEAR, CITY UNIVERSITY
5 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
The answer displayed is 48, and this is stored in ans. So typing
ans + 2
returns 50, and now 50 is stored in ans. You can define additional
variables in the command window, which will be listed in the workspace
window. Type
A=5
which returns
A=
5
Variable A has now been added to the workspace window, and can be
called up at any time until it is cleared. MATLAB is also case sensitive,
so a and A would be different variables.
A simple one-dimensional matrix can be entered simply by typing
B = [1, 2, 3, 4]
which returns
B=
[1 2 3 4]
The commas are optional, so by typing
B = [1 2 3 4]
returns the same result.
Two-dimensional matrices can also be entered in a similar fashion
C = [1 2 3 4;10 20 30 40; 5 10 15 20]
The graphics window(s) launches automatically when you request a
graph. To create a simple graph, first create an array of x values:
x = [1 2 3 4 5];
Note the semi colon at the end of the command. When this is used, the
result is not displayed, but the variable (in this case x) is still stored in
memory and can be seen in the workspace window.
Now type a list of y values:
FOUNDATION YEAR, CITY UNIVERSITY
6 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
y = [10 20 30 40 50]
Then to create a graph, use the plot command:
plot(x,y)
The graphics window automatically opens. It can be closed in the usual
way a window is closed.
3.1.2 Basic Operations
Type
a=1+2
This means a has been assigned a value of 1 plus 2. Now assign the
value of 5 to b. Then type
x=a+b
which should yield
x=
8
This means that x has been assigned a value of 8. So now by typing
x=x+1
can you explain what has now happened?
MATLAB also permits several operations to be combined in a single
arithmetic operation, so it is important to know the order in which
operations are performed:
1.
Parenthesis (brackets), innermost first
2.
Exponential, left to right
3.
Multiplication & division, left to right
4.
Addition & subtraction, left to right
Suppose we want to calculate the area of a trapezium, where the base
is horizontal and the two edges are vertical. The formula is:
A = ½ × (base) × (height1 + height2)
Now type the following into MATLAB
base = 5;
height_1 = 12;
height_2 = 6;
FOUNDATION YEAR, CITY UNIVERSITY
7 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
Now the problem can be solved by entering the following
area = 0.5 * base * (height_1 + height_2)
Work this out by hand using the above values, and explain in which
order of the equation MATLAB has carried out the computation.
3.1.3 Array Operations
Type
X = [1 2 3 4]
This returns a row vector, i.e. the numbers are aligned horizontally. To
specify a column vector, type the following:
Y = [1;2;3;4]
Here the column vector has its elements arranged vertically.
Type:
A = [1 2 3 4;2 3 4 5;3 4 5 6]
This returns a matrix with columns and rows. An alternative way to
create a matrix is to type each row on a separate line using the return
key. Try
B = [4 3 2 1;
5 4 3 2;
9 8 7 6]
If a complicated matrix had to be entered by hand, a matrix with evenly
spaced numbers can be entered more readily. Try
B = 1:5 (or B = [1:5])
This returns a row matrix containing the numbers 1 to 5, where each
number is incremented by 1. Try the following
C = 0:2:10
and
D = 1:5:51
Can you explain what the matrices C and D contain?
If you want MATLAB to calculate the spacing between the elements in
a vector, the linspace command is used. Type
FOUNDATION YEAR, CITY UNIVERSITY
8 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
linspace(1,10,3)
which returns a row vector with three values, evenly spaced between 1
and 10.
Matrices can be used in many calculations. If A = [1 2 3], we can add 5
to each value with the calculation
B=A+5
Explain what has happened when you execute these two commands.
Multiplication and subtraction operators work slightly differently. Set A
= [1 2 3], and B = [4 5 6]’. The ‘ makes B a column matrix. Now type
A*B. This should return the matrix multiplication, i.e. (1×4) + (2×5) +
(3×6) = 32 as you would expect. However if you want to perform an
element by element multiplication, the following should be typed (do
not forget to retype B as a row vector, i.e. B = [4 5 6], omitting the ‘:
A.*B
This will return [4 10 18], where an element by element multiplication
has been performed.
Try A./B and note your observations.
The matrix capability of MATLAB makes it easy to do repetitive
calculations. For example, assume you have a list of angles in degrees
that you would like to convert to radians. First put the angles into a
matrix, so type:
D = [10 15 70 90];
To change the values into radians, you must multiply by π/180, so type:
R = D*pi/180;
This command returns a matrix R with the equivalent values in radians.
To create an easy to read table, type:
table = [D’, R’]
Make a note of your observations.
3.1.4 Display Format
In MATLAB, values with decimal fractions are displayed using a default
format that shows four decimal digits. Thus A = 5 returns
FOUNDATION YEAR, CITY UNIVERSITY
9 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
A=
5
But A = 5.1 returns
A=
5.1000
MATLAB allows you to specify other formats that show more significant
digits, and the command
format long
now returns a format with 14 decimal digits
A
A=
51.10000000000000
The commands
format short
A
A=
51.1000
returns the format to four decimal digits. What happens when you use
the command format bank?
When numbers become too large or too small for MATLAB to display
using the default format, the program automatically expresses them in
scientific notation. For example
a = 602000000000000000000000
returns
a=
6.0200e+023
3.1.5 Saving Variables
To preserve the variables created in the command window (check the
workspace window on the left for the list of variables), you must save
the contents of the workspace window to a file. So to save the
workspace (remember that this is just the set of variables, not the lost
of commands in the command window) to a file, at the prompt type
save filename
FOUNDATION YEAR, CITY UNIVERSITY
10 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
where filename is a filename of your choice. The file will be stored in
the default C:\MATLAB\work directory. It is best to store all your files in
that directory, and if you wish, to copy them across to a USB stick later
on. Trying to run or load files outside the MATLAB directory structure
will not work, unless specified.
Typing
clear, clc
will clear the workspace and the command window. You can verify that
the work space is clear by checking the workspace window, or by
typing whos.
Now try it for your self. Define
A = 5;
B = [1 2 3];
C = [1 2;3 4];
Save these variables to a file, clear the command window and
workspace. Verify that A, B and C are empty, then load your file, and
check to see if the variables have been loaded.
3.1.6 Script M-Files
MATLAB can also be used to run script files, just as a real
programming language. As a programmer, you can create and save
code files called M-files. An M-file can be created and edited using the
MATLAB M-File editor, as shown in Figure 3.2. It can be opened by
clicking the New M-File icon to the top left of the main toolbar.
Fig 3.2
When you save an M-file, it will be stored in the work directory. You
will need to name your file with a legal MATLAB variable name, i.e. one
that contains only letters, numbers and the underscore (_). Spaces are
not allowed.
FOUNDATION YEAR, CITY UNIVERSITY
11 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
There are two types of M-files, called scripts and functions. A script Mfile is simply a list of MATLAB statements that are saved in a file
(typically with a .m file extension). The script has access to workspace
variables. Any variables created in the script are accessible to the work
space when the script finishes. A script created in the MATLAB editor
window can be executed by selecting the save and run icon from the
menu bar. Alternatively, a script may be executed by typing a filename
or by using the run command from the command window.
Using script M-files allows you to work on a project and to save the lost
of commands for future use. Because you will be using these files in
the future, it is a good idea to leave comments in the script (i.e. non
executable words that describe certain parts of the script). The
comment operator is a percentage sign that you type in the script file:
% This is a comment
When a comment is typed in this way, the subsequent text turns green,
to let you know that MATLAB will not execute any of this text.
As an example, open up the editor, and type the following
clear, clc
% This is an example of a script that will calculate the hypotenuse
% of a right angle triangle whose sides are 3cm and 4cm
x = 3;
y = 4;
r = sqrt((x^2 + y^2))
Save the script as test.m, and run it. In your own words, make a note of
what this script does, and how it does it. Amend the script so that it can
calculate the hypotenuse of a triangle whose sides are 5cm and 6cm.
The above program can be converted to a function file. A function file
allows you to run a set of commands for any input value. As an
example, type and save as hypo.m the following code in the editor
window
% This is an example of a MATLAB function file.
% It is used to calculate the length of the
% hypotenuse for an x and y that you input
function r = hypo(x,y)
r = sqrt((x^2 + y^2));
Now in the command window, type
r = hypo(3,4)
FOUNDATION YEAR, CITY UNIVERSITY
12 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
Based on your understanding and observations, can you explain the
difference between the two types of file?
3.1.7 End of Task 1 – LAB BOOKS MUST BE SIGNED AT THIS
POINT
That’s it for Task 1, and for the week. Your lab book should contain all
your observations and comments regarding what you have gone
through in this task. Before leaving, please get your lab book signed by
Dr. Daniel Nankoo.
3.2 WEEK 2 - TASK 2
3.2.1 The Help Command
Within MATLAB there are several toolboxes. Each toolbox contains
commands that are applicable to a certain branch of engineering or
mathematics. For a list of available toolboxes, type help in the
command window. The hyperlinks displayed are the toolboxes
incorporated in MATLAB. Click on a hyperlink of any toolbox, and you
should see a list of commands that are hyperlinked associated with that
toolbox. Click on the individual command in order to see further
information about a particular command and how it is used.
You can use help at any time in the command window. For example,
type
help sin
and make a note of what happens.
Try help with a few other commands, and make a note of them.
3.2.2 The Input and Disp Commands
The input command allows you to display a text string in the command
window, which then waits for the user to provide the requested input.
As an example, type
z = input(‘enter a value to be stored in z: ‘)
This line prompts you to enter a value that will be stored in z. The
number you then select will thus be stored in z.
The disp command can be used to display text enclosed in single
quote marks. For example, type
disp(‘Ohms Law is V = IR’)
FOUNDATION YEAR, CITY UNIVERSITY
13 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
The input and disp commands are often used together to create
programs where it is necessary to enter user defined variables, and to
display the results along with descriptive text.
Open up the editor using the New M-file icon. Type the following script,
and save the file as ohm.m
% This script calculates the value
% of resistance for user defined
% voltage and current
v = input('Enter voltage value in volts: ');
i = input('Enter current value in amps: ');
r = v/i;
disp('The resistance is:')
disp(r)
disp('Ohms')
Now type ohm in the command window, and follow the instructions. In
your lab books, explain each section of the above code. Explain the
significance of the %, ;, and discuss the order in which the script is
executed by MATLAB.
Can you alter the code so that instead of just entering a scalar (single
number) for the voltage and current values, a list (vector) of values are
inputted? Also, in addition to displaying the final resistance values, can
you add a line so that the voltage, current and resistance are displayed
in tabular form? Do not forget to save the changes you make to ohm.m,
as you’ll be coming back to it later
3.2.3 Plots
The plot command allows graphs to be plotted of defined x and y
vectors. For example, type in the command window:
x = [1:10];
y = [58.5, 63.8, 64.2, 67.3, 71.5, 88.3, 90.1, 90.6, 89.5, 90.4];
plot(x,y)
Explain in your lab books what is meant by x = [1:10];
So, on execution of the above three lines of code, what is displayed?
To add a title to the graph, simply type:
title(‘This is a test graph’)
Open the Figure 1 window, and explain what has been added.
You can also label the axes. In the command window type
FOUNDATION YEAR, CITY UNIVERSITY
14 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
xlabel(‘This is the x-axis’)
ylabel(This is the y-axis’)
What changes have been made to Figure 1?
Gridlines can also be added to the plot. In the command window, type
grid on
Based on what have studied so far, can you further amend the ohm.m
script so that a plot of the voltage values against the current values can
be displayed? Can you also give the plot an appropriate title, label the
axes appropriately and gridlines?
3.2.4 Your Turn
Write a MATLAB script that does the following:
1. To compute the kinetic energy (Joules) given by the formula:
2. KE = ½ mv2, where m is mass in kg, and v is velocity in m/s
3. The user is asked to input a list of velocities and constant
mass (i.e. the mass is the same regardless of velocity)
4. A vector showing the Kinetic Energy values must be
displayed
5. A table showing columns for mass, velocity and KE must be
displayed
6. A plot of KE against velocity with appropriate axis labels, title
and gridlines must also be shown.
In your script, you may like to add comments explaining each line. How
would you do this?
3.2.5 End of Task 2 – LAB BOOKS MUST BE SIGNED AT THIS
POINT
Make sure you have thorough details of the work covered this week in
your lab books. Before leaving, your lab books must be signed.
3.3 WEEK 3 - TASK 3
3.3.1 Ballistics
The range of an object shot at an angle θ with respect to the x axis and
with an initial velocity v0 is given by:
R = (v02/g)sin(2θ) for 0 ≤ θ ≤ π/2
FOUNDATION YEAR, CITY UNIVERSITY
15 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
The above formula neglects air resistance, and the range R is given in
metres, v0 is in m/s, the acceleration due to the earth’s gravity, g, is
9.81m/s2.
For an initial velocity of 100m/s, create a MATLAB script that shows
that the maximum range is obtained at θ = π/4 by plotting the range in
increments of 0.05 from
0 ≤ θ ≤ π/2
On the same plot, and using the same script, show that the maximum
range occurs at θ = π/4 for an initial velocity of 50m/s.
Your plot should include the appropriate title, labels and gridlines. Your
script should also show comments.
3.3.2 End of Task 3 – LAB BOOKS MUST BE SIGNED AT THIS
POINT
Do not forget to detail all your work in your lab book. Before leaving,
please have them signed. Also do not forget to save any files you wish
to use (e.g. for the presentation) onto a USB stick (or email them to
yourself!).
FOUNDATION YEAR, CITY UNIVERSITY
16 OF 17
TERM 2 PROJECT: ENGINEERING SOFTWARE
DR DANIEL NANKOO
4.0 SECTION 2 – WEEKS 4 TO 6 - MULTISIM
Download the following file:
http://www.staff.city.ac.uk/~danny/MusimP1.pdf
Complete the tutorials as shown in Chapters 2 and 3.
As you go along, make notes in your lab books, and save any relevant
screenshots to a word file.
The task for week 6 is to complete lab sheet 8 from term 1 on the bipolar
transistor using Multisim. If you have time, you mat want to use the Utilboard
facility to create a 3D image of your breadboard layout.
FOUNDATION YEAR, CITY UNIVERSITY
17 OF 17
Download