Anda di halaman 1dari 8

MATLAB Exam1 Practice Fall 2015

Department of Mechanical Engineering

ME 2173: Numerical Methods

Fall 2015

Name: _______________________________Section: ____________ Date :_________

PRACTICE QUESTIONS FOR MATLAB EXAM 1


(This Exam is CLOSED book and notes, No-MATLAB, and No-Calculator)

1. Which one is the correct expression in MATLAB for this equation?

𝑙𝑜𝑔 (𝑒 )/ ln 2
a. log(exp(3))/log2
b. log10(e^3)/log(2)
c. log10*(exp^3)/log(2)
d. log10(exp(3))/log(2)

2. Write  the  output  matrix  “v”


t = [2:4];
k = [1:3];
v = t.*k – k.^2

Answer:
𝑣 =   [  _ _ _  ]

3. What is the correct MATLAB command evaluate: ∫ x cos 𝑥   𝑑𝑥

>>syms x

a. int(x^3*(cos(x))^2,2*pi,0)
b. int(x^3*(cos x)^2,0,2*pi)
c. int(x^3*cos^2 (x),0,2*pi)
d. int(x^3*cos(x)^2,0,2pi)

1 2 3
1 2 3
4. Given: D = 4 5 6 . Which command will extract the submatrix ?
4 5 6
7 8 9

a. D[1:2,1:3]
b. D(1,2 ;1,3)
c. [D(1:2),D(1:3)]
d. D(1:2,1:3)

5. Complete the Mtlab code that plots𝑦 = 𝑐𝑜𝑠(𝑥 )/𝑥 for ≤ 𝑥   ≤ 𝜋, using a dashed red line
MATLAB Exam1 Practice Fall 2015

x= linspace(____,____,20);

y=cos(x____)____x;

plot(x,y,’r___’)

6. The function below computes the areaA and volumeV of a cylinder given its radius and
height:

function [A, V] = cylinderprop(r,h)


A=2*pi*r*h;
V=pi*r^2*h;
end

Which Matlab code calls function for input arguments r=3 and h=5:

a. [ ] = cylinderprop(3,5)
b. [A,V] = cylinderprop(5,3)
c. [A,V] = cylinderprop(3,5)
d. (A,V) = cylinderprop(3,5)

7. What Matlab command is used to clear all the variables and functions from the workspace?

a. clear workspace
b. clear all
c. close all
d. clc

8. Complete the codes for solving these two equations

𝟐𝒙 + 𝒚 = 𝟓;
𝒙 − 𝒚 = 𝟔;

syms____
eq1 = 2*x+y-5;
eq2 = ____________________;

9. Given the anonymous function f=@(x,y) x+y. For x=[1 2] and y=[1 0], find f([1 2],[1 0]).

a. f([1 2],[1 0])=[0 2]


b. f([1 2],[1 0])=[2 2]’
c. f([1 2],[1 0])=2
d. f([1 2],[1 0])= [2 2]

10. Given  𝑣 = 𝟑𝒑𝒕 + 𝟓𝒕𝟐 , v can be evaluated at p=2 and t = 1 as follows:


MATLAB Exam1 Practice Fall 2015

syms____ ____
v = 3*p*t + 5*t^2;
subs( _________,[ ___ ___ ], [ ___ ___ ] );

𝒅𝟑
11. Use symbolic computation to Differentiate the following in MATLAB: 𝒅𝒙𝟑
𝟑𝒙𝟐 + 𝟗𝒆𝒙

syms x

a. diff(3x^2+9exp(x),3)
b. diff(3*x^2+9*exp(x),3)
c. diff^3(3*x^2+9*exp(x))
d. diff[3*x^2+9*exp(x),3]

12. Given x=linspace(1,10,30) and y=linspace(6,15,30), evaluate 𝟑𝒙𝒚 + 𝒙𝟐 + 𝒙𝒚 :

a. 3*x.*y+x^2+x.*y
b. 3*x*y+x^2+x*y
c. 3*x*.y+x.^2+x*.y
d. 3*x.*y+x.^2+x.*y

( )
13. Write matlab commands to plot 𝑦 = + 𝑥 for 1 ≤ 𝑥 ≤ 3, using a solid black line and
( )
circles.
x=_______________________;

y=____________________________________;

plot___________________________________;

14. What will be the dimension of matrix B?

B=[ones(3) zeros(3) rand(3); 2*eye(9)]

a. B <12x9 Double>
b. B <9x9 Double>
c. B <12x12 Double>
d. B <9x12 Double>

15. What is the name of the window in MATLAB where variables are stored?

a. Command Window
b. Workspace
c. Command History
d. Editor
MATLAB Exam1 Practice Fall 2015

16. For 2 equal-sized vectors x and z, which of these expressions is not properly vectorized?

a. y = x.*z + 8;
b. y = x^2 – 2/z;
c. y = 2*(z + x);
d. y = z/4 + 7*x;

17. Write the missing command lines that are used to create the plot of y=x*sin(x) below for :

>> __________________ % Generate 100 values of x , between -3π  and  3π

>> __________________% y=x*sin(x);

>> __________________ % plots

18. Find  the  value  of  “C”

A=1:2:10;
B=linspace(1,5,5);
C = length(A)*B(2)+A(5)*B(3);

a) 34
b) 35
c) 36
d) 37

−1 2 −3
19. Matrix A is defined: A = 0 2 4 Which command deletes the second row?
2 −2 3
a) A(2,:) = 0;
b) A(:,2) = 0;
c) A(2,:) = [ ];
d) A(:,2) = [ ];
MATLAB Exam1 Practice Fall 2015

20. Which command gives the dimensions of matrix A?

a) dimension=size(a)
b) [r,c]=sizeof(a)
c) dimensions(A)
d) [r,c]=size(A)

21. What is the value of C?: ______________________

A=[1 2 3;4 5 6; 7 8 9];


B=[1 3; 5 7];
C=sum(B(:,1)+A(1:2,2));

22. What is the value of C?: Answer:______________________

A=[1 2 3;4 5 6; 7 8 9];


B=[1 3; 5 7];
D=B(1,:).*A(2,2:3);

23. Consider the following MATLAB command to plot y vs x .Which line gives an error message:

a) >> x=linspace(1,10,100)
b) >> y=x^2;
c) >> figure(1)
d) >> plot(x,y);

24. Find the answer for [x , y]=sample(3) for the function below:

function [A, B]=sample(x)


A = 2*x;
B = x^2;
end

a) [x , y] = [6, 18]
b) [x , y] = [9, 6]
c) [x , y] = [6, 9]
d) [x , y] = [18,9]

25. Using “subplot(m,n,p)” command create a figure window which will plot x ∙ sin(𝑥) and
cos(x+2), side by side in the same figure window. The vector 𝑥 ∈ [−𝜋, 𝜋], has 50 elements.

x=___________________________;
subplot(____________); _________________________
subplot(____________); _________________________

26. Solve the following system of equations using symbolic computation


MATLAB Exam1 Practice Fall 2015

2𝑥 + 𝑥 − 𝑥 = 1
5𝑥 + 2𝑥 + 2𝑥 = −4
3𝑥 + 𝑥 + 𝑥 = 5

________
eq1=___________________;eq2=__________________;eq3_____________________;
_____________________________;

27. Plot the following functions on the same graph, showing only the legend for 0 ≤ 𝑥 ≤ 2𝜋:
𝑦 = 2𝑥 sin  (𝑥/2) ( Blue squares); 𝑦 = 2𝑥cos (𝑥)(Green diamonds)

x=___________________________; y1=________________; y2=_______________;


______________________; ____________ on ; ______________________;
legend(________________________________);

28. How can you solve sin x = x − 1  in matlab using symbolic computation?
syms ___; ____________________

29. What commands compute Y  in  for  5  linearly  spaced  points  in  the  range  0≤k≤𝜋/2, given that:
𝑘 sinh 𝑘 + cosh 𝑘
𝑌=     sech √𝑘 + 𝑘/5
𝑘

k=_________________________________;

Y=_____________________________________________________________;

30. Use Symbolic computation to write the single line command that:
a) evaluates ∫ 𝑥 sin 𝑥 𝑑𝑥
syms _____; ______________________________________

b) evaluate d at p=1, v=2, t=1, where


𝑣 cos (𝑝⁄2)
 𝑑 =
√𝑣 − 3𝑡
syms _________ ; ___________________________________________________
MATLAB Exam1 Practice Fall 2015

Answer Key

1. d
2. 𝑣 =   [1 2 3]
3. d
4. d
5. x= linspace(pi/2, pi ,20);
y=cos(x.^2) ./x
plot(x,y,’r --’)
6. c
7. b
8. syms x y
eq1 = 2*x+y-5;
eq2 = x - y - 6;
9. d
10. syms p t
v = 3*p*t + 5*t^2;
subs ( v , [p t ], [2 1 ] );
11. b
12. d
13. x=linspace(1,3);
y=log(x)./(x.^2.*sin(2*x))+x ;
plot(x,y,'k-o')
14. a
15. b
16. b
17. x=linspace(-3*pi,3*pi);
y=x.*sin(x)) ;
plot(x,y,'r--s')
18. d
19. c
20. d
21. C=13
22. D=[ 5 18 ]
23. b
24. c
25. x=linspace(-pi,pi,50) ;
subplot(1,2,1) ; plot(x, x.*sin(x))
subplot(1,2,2) ; plot(x, cos(x+2))

26. syms x1 x2 x3
eq1=2*x1 + x2 - x3 -1; eq2=5*x1+2*x2 + 2*x3 + 4 ; eq3=3*x1 + x2 + x3 - 5 ;
[x1,x2,x3]=solve(eq1,eq2,eq3)

27. x= linspace(0,2*pi) ; y1=2*x.^2.* sin(x/2); y2=2*x.*cos (x).^3 ;


plot(x, y1,’bs’) ; hold on ; plot(x, y2,’gd’);
legend(‘y1’, ‘y2’);
MATLAB Exam1 Practice Fall 2015

28. syms x; solve(sin(x) - x^2 +1 )

29. k = linspace(0,pi/2,5);
Y = ((k.*sinh(k) + cosh(k))./(1-k).^2).*(sech(sqrt(k))).^2 + (k/5).^(1/3)

30. a) syms x ; int(x^2 *sin(x)^2,x,0,pi)

b) syms p v t ; subs((v*cos^2*(p/2)) / sqrt(v^3 – 3*t) , [ p v t], [ 1 2 1] )

Anda mungkin juga menyukai