Lecture_2
Lecture_2
Lecture_2
for Engineers
William J. Palm III
Chapter 1
An Overview of MATLAB
>> 8/10 % Note
ans =
0.8000
>> 5*ans
ans =
4
>> r=8/10
r=
0.8000
>> r
r=
0.8000 Note: The MATLAB use high precision for
its computations, but displays the
>> s=20*r default short format.
s=
16
Symbol Operation
^ exponentiation: aª a^b
* multiplication: ab a*b
+ addition: a + b a+b
- subtraction: a -b a-b
Precedence operation:
>> 8 + (3*5)
ans =23
>>(8 + 3)*5
ans =55
>>4^2-128/(4*2)
ans =0
>>4^2-128/4*2
ans =-48
3*4^2+5
ans =
53
5 + 2^)4*3(
ans =
4101
27^(1/3) + 32 ^(0.2)
ans =
5
27 ^(1/3) + 32^0.2
ans =
5
27 ^1/3 + 32^0.2
ans =
11
The term workspace refers to the names and values of any variables in
use in the current work session. Variable names must begin with a
letter; the rest of the name can contain
letters, digits, and underscore characters.
MATLAB is casesensitive.
>>u = [0:0.1:10];
>>w = 5*sin(u);
0.6000
>> w(7)
ans =
2.8232
>>m = length(w)
m=
101
Command Description
(continued …)
Q1. Suppose that x = 2 & y = 4.
Write MATLAB command(s) to compute the following. Then
write the answer.
(Ask the instructor to select 1&3 or 2&4 only).
Ax = b x=b/A
MATLAB provides the left-division method for solving the
equation set Ax = b. The left-division method is based on
Gauss elimination. To use the left-division method to solve for
x, type x = A\b.
For example,
>>A = [2,9;3,-4];
>> b = [5;7]
>> x = inv(A)*b
x=
2.3714
0.0286
Note : If you attempt to solve a singular problem using the inv
command, MATLAB displays an error message.
>>A = [6,12,4;7,-2,3;2,8,-9];
>>B = [70;5;64];