Lab No. 1: Swedish College of Engineering & Technology, Wah Cantt
Lab No. 1: Swedish College of Engineering & Technology, Wah Cantt
Lab No. 1: Swedish College of Engineering & Technology, Wah Cantt
Roll No.:________________________
Teacher’s Initials: ________________
Marks Obtained: _________________
Lab No. 1
Introduction of MATLAB
MATLAB is an interactive software which has been used recently in various area of
engineering and scientific applications. It is not a computer language in the normal
sense but it does most of the work of a computer language. Writing a computer code
is not a straightforward job. Typically boring and time consuming for beginners. One
attractive aspect of MATLAB is that relatively easy to learn. It is written on an
intuitive basis and it does not require in depth knowledge of operational principles
of computer programming like compiling and linking in most programming
languages. This could be regarded as a disadvantage since it prevents users from
understanding the basic principles in computer programming. The interactive mode
of MATLAB may reduce computational speed in some applications.
USING MATLAB:-
Either MATLAB must be installed on your computer or you must have access to a
network where it is available. Throughout this Lab Fall 2017. The latest version at
the time of writing is assumed (Version 2007)
Do not click on the X (close box) in the top right corner of the desktop. This does
not allow MATLAB to terminate properly and, on rare occasions, may cause
problems with your operating software.
Arithmetic:-
Since we have experience doing arithmetic, we want to examine if MATLAB does
it correctly. This is a required step to gain confidence in any tool and in our ability
to use it.
Type 2+3 after >> prompt, followed by Enter (press the Enter key) as indicated by
<Enter>
Answer:
Answer:
Answer:
Answer:
Answer:
The line with the >> prompt is called the command line.
You can edit a MATLAB command before pressing Enter by using various
combinations of the Backspace, Left Arrow, Right Arrow and Del Keys. This
helpful features is called command line editing.
You can select (and edit) commands you have entered using Up-Arrow and
Down Arrow. Remember to press Enter to have the command carried out
(i.e., to run or to execute the command).
MATLAB has a useful editing features called smart recall. Just type the first
few characters of the command you want to recall. For Example, Type the
character 2* and press the Up Arrow key—this recalls the most recent
command starting with 2*.
How do you think MATLAB would handle 0/1 and 1/0? Try it. MATLAB is sensible
about anticipating some errors; it warns you in case you didn’t realize you were
dividing by zero, but still gives the answer Inf. If you insist on using ∞ in a
calculation, which you may legitimately wish to do, type the symbol Inf. (short for
infinity). Try 13+Inf. and 29/Inf.
Another special value that you may meet NaN, which stands for Not a Number. It
is the answer to calculation like 0/0.
Variables:-
Now we will assign values to variables to do arithmetic operations with the variables.
First enter the command (statement in programming jargon) a=2. The MATLAB
command line should look like this:
The a is variable. This statement assigns the value of 2 to it. (Note that this value is
displayed immediately after the statement is executed). Now try entering the
statement a = a + 7 followed on a new line by a = a*10. Do you agree with the final
value of a? Do we agree that it is 90?
>> b = 3; <Enter>
The semicolon (;) prevents the value of b from being displayed. However, b still has
the value 3, as you can see by entering without a semicolon:
>> b (Enter)
Assign any value you like to two variables x and y. Now see if you can assign the
sum of x and y to a third variable z in a single statement. One way of doing this is
>> z = x + y
Answer:
Notice that, in addition to doing the arithmetic with variables with assigned values,
several commands separated by semi colons (or commas) can be put on one line.
Getting the right values in the right variables at the right time
A Variable name (like the variable balance that we used as in Lab 1) must comply
with the following two rules:
It may consist only of the letters a-z, the digits 0-9 and the Underscore ( _ )
It must start with a letter.
Mathematical Functions:
MATLAB has all of the usual mathematical function found on a scientific electronic
calculator, like Pi, exp (exponential), x^ (power), sqrt(square root) and log etc.,
>>pi
Answer:
>> exp(2)
Answer:
Answer:
If you write H in place h Than MATLAB gives you error. Check for answer if hamza
than what happen?
Answer:
Note that clear executed by itself clear all local variables in workspace.
Answer:
Answer:
Vectors:
Variables such as a and b above are called scalars, they are single-valued. MATLAB
also handles vectors (generally referred to as arrays), which are the key to many of
its power features. The easiest way of defining a vector where the elements
(components) increase by the same amount is with a statement like
>> a = [1 2 3 4 5]
Answer:
>> a = [1,2,3,4,5]
Answer:
>> a = [1;2;3;4;5]
Answer:
>> a = [1:5]
Answer:
>> a= [5:1]
Answer:
>> a=[1:1:5]
Answer:
>> s = [1:2:5];
Answer:
>> s = [1:3:5]
Answer:
Part of the real power of MATLAB is illustrated by the fact that other vectors can
now be defined (or created) in terms of the just defined vector a. Try
Answer:
x = [1 3 0 -1 5]
Answer:
Enter the command disp (x) to see how MATLAB displays a vector.
Enter the command whos. Under the heading Size you will see that x is 1 by 5, which
means 1 row and 5 columns. You will see that the total number of element is 5.
You can use commas instead of spaces between vector elements if you like. Try this:
a = [5,6,7]
Answer:
Don’t forget the commas (or Spaces) between elements; otherwise, you could end
up with something quite different:
x = [130-15]
Answer:
You can use one vector in a list for another one. Type in the following:
a= [1 2 3];
Answer:
Answer:
c= [a -b];
Answer:
Can you work out what c will look like before displaying it?
A= [1 3 7];
Answer:
A=[a 0 -1];
Answer:
Answer:
Making x empty is not the same as saying x=0 (in the latter case x has size 1 by 1)
or clear x (which removes x from the workspace, making it undefined).
x=1:10
Answer:
Answer:
(Elements are the value 1, 1.5,……, 4) in increment of 0.5. Note that if the colon
separate three values, the middle value is the increment);
x=10:-1:1
Answer:
x=[1:2:6]
Answer:
(Elements are 1,3,5; Note that when the increment is positive but not equal to 1, the
last element is not allowed to exceed the value after the second colon);
x=[0:-2:-5]
Answer:
x= 1:0
Answer:
Transposing Vectors
All of the vectors examined so far are row vectors. Each has one row and several
columns. To generate the column vectors that are often needed in mathematics, you
need to transpose such vectors—that is, you need to interchange their rows and
columns. This is done with the single quote, or apostrophe (‘), which is the nearest
MATLAB can get to the mathematical prime (‘) that is often used to indicate the
transpose.
Enter x= 1:5 and then enter x’ to display the transpose of x. Note that x itself remains
a row vector. Alternatively, or you can create a column vector directly:
y=[1 4 8 0 -1]’
Answer:
Matrices
a=[1 2 3;4 5 6]
and result in
Answer:
Answer:
Linear Equations:
System of linear equations are very important in engineering and scientific analysis.
A simple example is finding the solution to two simultaneous equations:
𝑥 + 2𝑦 = 4
2𝑥 − 𝑦 = 3
AX=B
X=A-1B
So in MATLAB, we have
x=inv(A)*B
Answer:
(a). 2 / 2 * 3(3)
(b). 2 / 3 ^ 2(2/9)
(c). (2 / 3) ^ 2(4/9)
(d). 2 + 3 * 4 – 4(10)
(e). 2 ^ 2 * ¾ + 3(6)
(f). 2 ^ 3 ^ 2(64)
(h). 2 ^ 3 ^ 2(64)
(i). -4 ^ 2(-16)
(a). √2
3+4
(b).
5+6
(g). 2𝜋 2
(h). 1 / √2𝜋
(i). Find the cube root of the product of 2.3 and 4.5
2
1−
3+2
(j). 2
1+
3+2
1
(k).
2 √𝜋
(a). 2, 4,6,8,10
(c). 1,1/2,1/3,1/4,1/5
1 1 1
(d) 1, 1/22 , , ,
32 42 52
𝑎 = [2 − 1 5 0]
𝑏 = [3 2 − 1 4]
(a). c = a-b
(b). c = b + a -3;
(c). c = 2 * a + a .^b;
(d). c = b ./ a;
(e). c = b . a;
(f). c = a . ^b;
(g). c = 2. ^ b+a;
(h). c = 2*b/3.*a;
(i). c = b*2.*a;
F = 9C/5 +32
Coverts from one to the other. Use the MATLAB command line to convert Celsius
37° (normal human temperature) to Fahrenheit?
(b). Engineers often have to convert from one unit of measurement to another, which
can be tricky sometimes. You need to think through the process carefully. For
Example, Convert 5 acres to hectares, given that an arce is 4840 square yards, a yard
is 36 inches. An inch is 2.54 centimeter, and a hectare is 1000 square meter. The best
approach is to develop a formula to convert x acres to hectares.
Once you have found the formula, MATLAB can do the rest:
x = 5;
h = 0.4047 * x
disp (h)
(d). One atmosphere pressure = 14.7 pounds per square inch (psi) = 101.325 kilo
Pasclas (kPa). Convert 40 psi to kPa.