0% found this document useful (0 votes)
9 views5 pages

Sheet1 Solution

Uploaded by

mahmoud amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Sheet1 Solution

Uploaded by

mahmoud amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

Assignment (1) Solution


1- Complete the following sentences
a) Command clear is used to clear the variable in workspace
b) Command clc is used to clear command window
c) Matlab is a high level language
d) Variable names must start with a letter followed by letters, digits, and
underscores.
e) It is not allowed to leave empty spaces in the name of the variable
f) Command clf is used to clear a Matlab figure.
g) The command whos is used to determine the list of local variables or
commands presently in the workspace.
h) To run an M-flle, type the filename (without the extension) at the
command prompt (>>)
i) The name of user-defined variables should not duplicate the name of a
built-in functions or Matlab commands

2- What is the function of writing “%” at the beginning of a line in a Matlab


m-file.

 It is an indication to a comment (i.e. not executable command)

3- How to connect two lines of a very long statement in Matlab.

 Write “ … ” at the end of the first line

4- Is the two following variable executed in Matlab are identical

>> a = 2;

>> A = 4 ;

 Variable names are case sensitive (i.e. a ≠ A)

5- If A=[1 2 ;3 4], what is the result of the following command, explain the
elements of the resulting matrix
Dr. Mohamed Saber Sokar
BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

>> C_A=[A, 2*A; 3*A, 4*A; 5*A, 6*A]

6- Consider the matrix X =


1 2 3
5 1 4
3 2 -1
I- Use suitable commands to display the whole second row of the matrix X

 >> y = X(2,:)

II- Use suitable commands to display the whole second column of the
matrix X

 >> y = X(:,2)

III- Use two different methods to get the element (3,2)

 >> X(6) or >> X(3,2)

IV- How to display the first two elements of the second column?

 >> X(1:2, 2)

7- What Matlab built-in functions should be used to calculate the square root
of a given number and the exponential function ex?

 sqrt(X) and exp(x)

8- Write a simple Matlab m-file to add three numbers and print the output.
9- What is the default increment in the following command » x = 0 : 10; ?

 By default, the increment is 1.

10- What is the output of the following command X = 0 : 2 : 10; ?

 t = [0 2 4 6 8 10]

Dr. Mohamed Saber Sokar


BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

11- Consider t = initial value : Step : final value


What if the right boundary number is smaller than the left boundary
number with a positive increment? (For example >>t = 10 : 2 : 1)
What if the right boundary number is greater than the left boundary
number with a negative increment?

 It yields an empty matrix, which is useless.

 t = Empty matrix: 1-by-0

12- If we define just some elements of a vector not fully, but sporadically,
(such as >>D(2) = 2; D(4) = 3). Will we have a row vector or a column
vector and how will it be filled in between?
 We will have a row vector filled with zeros between the defined
elements. D=0 2 0 3
13- How do we make a column vector in the same style of question 12?
 We must initialize it as a (zero-filled) column vector, prior to giving it a
value.
>> D = zeros(4,1); D(2) = 2; D(4) = 3

D=0
2
0
3
14- What happens if you typed D(5), D(0) or D(1.2) on the command
window after executing question (12 or 13) ?
It is rejected, the specified element index of an array exceeds the defined
range, MATLAB does not accept nonpositive or noninteger indices.

>>D(5)
??? Index exceeds matrix dimensions.
>>D(0) = 1;
??? Index into matrix is negative or zero.
>>D(1.2)
??? Subscript indices must either be real positive integers.

Dr. Mohamed Saber Sokar


BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

15- How do we know the size (the numbers of rows/columns) of an already


defined array , such as array of problem 13 and the matrix of problem 6?
 Use the length() and size() commands as indicated below.
>>length(D)
ans = 4
>>[M,N] = size(X)
M=2
N=2

16- Suppose vectors a1, a2, b1, b2 and matrices A1, A2, B are defined as
follows:

>> a1 = [-1 2 3]; a2 = [4 5 2]; b1 = [1 -3] '; b2 = [-2 0];


>> A1 = [a1; a2], A2 = [a1; [b2 1]], B = [b1 b2 ']

Perform the following various operations on these vectors/matrices (pay


attention to the error message):
I) Matrix/scalar addition/subtraction
>> A3 = A1 + A2, A4 = A1 - A2, 1 + A1


II) Matrix multiplication AB(m, n) =  A1(m, k )B(k , n)
k
>> AB = A1*B


>> BA1 = B*A1


III) Term wise (element by element) multiplication
>> AA = A1.*A2


Dr. Mohamed Saber Sokar
BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

IV) Term wise multiplication A1(m, n) B(m, n)


>> AB=A1.*B


V) >> Z = zeros(2,3), zeros(size(A1)) , ones(3,2), diag(A1)
 Yielding a 2 x 3 zero matrix

,
 Ones (3,2) yielding a 3 x 2 one matrix

,
 Yielding a 2 x 2 identity matrix

 Yielding a 2 x 1 matrix

Dr. Mohamed Saber Sokar

You might also like