Matlab Final Duration:75 min 09.06.2009

advertisement
Matlab Final
Name and Surname:
Signature:
Duration:75 min
ANSWER KEY
09.06.2009
ID:
GRADE:
Q1. Write short answers for the following questions :
a) (5 points) What is the effect of the format short command?
FORMAT SHORT :
Scaled fixed point format with 5 digits.
b) (10 points) Write a Matlab function (m file) to evaluate the function y = sin2(x2) where
0  x  k and to plot the function when k is given by user. If k is negative, print an error on
the screen.
function y(k)
if nargin<1 | k<=0, error('check the data'), end;
x=0:0.01:k;
y=sin(x.^2).^2;
plot(y)
c) (5 points) Write a m file called ‘check1’ that takes a vector as input and prints an error
message for all vectors that do not have exactly three components.
function check1(v)
if size(v)~=3, error('this vector does not have 3 componenets'), end;
end
d) (5 points) A student has the following m file function:
function myfun1(a)
reciprocal=1/a;
result=2*reciprocal
In command window, the student types the following
>>a=3; myfun1(a), reciprocal
What values does Matlab print after these commands ?
result =
0.6667
??? Undefined function or variable 'reciprocal'.
e) (5 points) Write the commands to plot the functions y versus x and z versus x on the same
axes.
plot(x,y);
hold;
plot(x,z)
Page 1
Q2. (15 points) a) Draw a SIMULINK model which shows the solution of the system
 x  2 y  5,

2 x  y  3.
(10 points) b) Explain the following terms : pie3(x), syms , expand, subfunction, callback function.
PIE3 3-D pie chart.
SYMS Short-cut for constructing symbolic objects.
EXPAND expand records into sweeps with size specified in t
SUBFUNCTION In a m-file, subfunctions are functions that constructed for local use
CALLBACK FUNCTION In GUI, Callback functions defines what the object does.
Page 2
Q3.
(20 points) a) Write a complete Maple program to solve the following problem:
An object has been thrown vertically from the ground with an initial speed, V0=35 m/sec.

Assume that g  10m / sec 2 and neglect air friction.
 Compute and display the total flying time of the object,
 Compute and display the maximum height of the object,
 Compute and display the height of object at t=5 sec.
You can use the following formulae when necessary:

V  V0  g t
and
y  V0 t 
final:=proc()
local Vo, g, tmax, t, hmax, hatfive;
Vo:=35; g:=10;
tmax:=Vo/g;
t:=tmax*2;
print('Total_flying_time_is', t);
hmax:=Vo*tmax-1/2*g*tmax^2;
print('Maximum_height_is', hmax);
hatfive:=Vo*5-1/2*g*5^2;
print('Height_at_five_sec_is', hatfive);
end;
Page 3
1 2
gt
2

Q4. (5points) a) Give the output for the following Maple codes. Explain your result.
a:=0; b:=1; for i from 1 by 2 to 5 do c:=(a+b)/2;
if c<3/4 then a:=c else b:= c fi; od;
a := 0
b := 1
c :=
1
2
c :=
3
4
c :=
5
8
(20 points) b) Write a m.file, which founds how many digits of whole number and decimal
number are there in a number given by user.
For example : 1234.567 has 4 digits of whole number and 3 digits of decimal number.
0.32 has 1 digit of whole number and 2 digits of decimal number.
function digit(n);
k=0;
n=abs(n);
n1=n;
while ceil(n)-n~=0;
n=10*n
k=k+1;
end;
s=1;
while ceil(n1)>=10;
n1=n1/10;
s=s+1;
end;
the_number_of_whole_digits=s,
the_number_of_digits_in_decimal_part=k,
Page 4
Download