Sheet1 Solution
Sheet1 Solution
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
>> a = 2;
>> A = 4 ;
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
>> y = X(2,:)
II- Use suitable commands to display the whole second column of the
matrix X
>> y = X(:,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?
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; ?
t = [0 2 4 6 8 10]
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
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.
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
16- Suppose vectors a1, a2, b1, b2 and matrices A1, A2, B are defined as
follows:
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
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