Вы находитесь на странице: 1из 22

Department of Electrical

Engineering
College of Engineering
and Architecture
Saint Louis University

Kiling, Lormhel J.
EE 512
1:00-3:00 WF

Montalban, Jonathan T.
Villanueva, Jordan J.

1.) Evaluate the following expressions without using MATLAB. Check the answer with
MATLAB.
a.) 583
b.) y=7< 31+6> 2
c.) y=( 7< 3 )1+ ( 6> 2 )
20
d.) y=2 4+5= 7+
4
a.)script file:
5<=8-3
output:
ans =
1
b.)script file:
y=7<3-1+6>2
output:
y =
0
c.)script file:
y=(7<3)-1+(6>2)
output:
y =
0
d.)script file:
y=2*4+5==7+20/4
output:
y =
0

2.) Given: a=10, b=6. Evaluate the following expressions


without using MATLAB. Check the answer with MATLAB.

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

a.) y=ab

b
2
b
c.) y=a b
2
b.) y=ab

( )

a.)script file:
a=10; b=6;
y=a>=b
output:
y =
1
b.)script file:
a=10;
b=6;
y=a-b<= b/2
output:
y =
0
c.)script file:
a=10;
b=6;
y=a-(b<=b/2)
output:
y =
10

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

3.) Given: v = [4 -2 -1 5 0 1 -3 8 2] and w = [0 2 1 -1 0 -2 4 3 2]. Evaluate the following


expressions without using MATLAB. Check the answer with MATLAB.
a.) v w
b.) w =v
script file:

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

4.) Use the vectors v and w from the previous problem. Use relational operators to
create a vector y that is made up from the elements of w that are greater than the
elements of v.
script file:

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

5.) Evaluate the following expressions without using MATHLAB. Check the answer with
MATHLAB.
script file:
a.)5 & 2
b.)8 2 | 6 + 5 & ~ 2
c.)~ ( 4 & 0) + 8 * ~ (4 | 0)
script file:

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

6.) The maximum daily temperature (in F) for New York City and Anchorage, Alaska
during the month of January, 2001 are given in the vectors below (data from the U. S.
National Oceanic Atmospheric Administration).
TNY = [31 26 30 33 33 39 41 41 34 33 45 42 36 39 37 45 43 36 41 37 32 32 35 42
38 33 40 37 36 51 50]
TANC = [ 37 24 28 25 21 28 46 37 36 20 24 31 34 40 43 36 34 41 42 35 38 36 35 33
42 42 37 26 20 25 31]
Write a program in a script file to answer the following:
a.)Calculate the average temperature for the month in each city.
b.)How many days was the temperature below the average in each city?
c.)How many days, and which dates in the month, was the temperature in
d.)Anchorage higher than the temperature in New York?
e.)How many days, and which dates in the month, was the temperature the same in
both cities?
f.)How many days, and which dates in the month, was the temperature in both cities
above freezing (above 32F)
script file:
TNY=[31 26 30 33 33 39 41 41 34 33 45 42 36 39 37 45 43 36
41 37 32 32 35 42 38 33 40 37 36 51 50];
TANC=[37 24 28 25 21 28 46 37 36 20 24 31 34 40 43 36 34 41 42
35 38 36 35 33 42 42 37 26 20 25 31];
TNY_ave=mean(TNY)
TANC_ave=mean(TANC)
N_days_TNY_above_ave=sum(TNY>TNY_ave)
N_days_TANC_above_ave=sum(TANC>TANC_ave)
N_days_TANC_greater_TNY=sum(TANC>TNY)
dates_TANC_greater_TNY=find(TANC>TNY)
N_days_TNY_equal_TANC=sum(TNY==TANC)
dates_TNY_equal_TANC=find(TNY==TANC)
N_days_TNY_above_freezing=sum(TNY>32)
N_days_TANC_above_freezing=sum(TANC>32)
dates_TNY_above_freezing=find(TNY>32)
dates_TANC_above_freezing=find(TANC>32)
output:
TNY_ave =
37.6774
TANC_ave =
33.1290
N_days_TNY_above_ave =
14
N_days_TANC_above_ave =
18

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

N_days_TANC_greater_TNY =
11
dates_TANC_greater_TNY =
1

14

15

18

19

21

22

25

10

11

12

13

26
N_days_TNY_equal_TANC =
1
dates_TNY_equal_TANC =
23
N_days_TNY_above_freezing =
26
N_days_TANC_above_freezing =
19
dates_TNY_above_freezing =
4

14
27

15
28

16
29

17
30

18
31

19

20

23

24

25

26

dates_TANC_above_freezing =
1
19

7
20

8
21

9
22

13
23

14
24

15
25

16
26

17

18

27

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

7.) Use MATLAB in two different ways, described below, to plot the function:
a.) Write a program in a script file, using conditional statements and loops.
b.) Create a user-defined function for f(x), and then use the function in a script file to
make the plot.
script file:
for x=-6:0.01:2.5
if ((x>=-6)&&(x<=-2))
f=4*exp(x+2);
plot(f,x)
hold on
elseif ((x>=-2)&&(x<=2.5))
f=x^2;
plot(f,x)
hold on
else
f=(x+6.5)^(1/3);
plot(fx,x)
hold on
end
end

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

8.) Write a program in a script file that determines the real roots of a quadratic equation
ax2 + bx + c = 0. Name the file quadroots. When the file runs it asks the user to
enter the values of the constants a, b, and c. to calculate the roots of the equation
the program calculates the discriminant D given by:
D = b2 -4ac
If D > 0 the program displays a message: The equation has two roots, and the
roots are displayed in the next line.
If D = 0 the program displays the message: The equation has one root, and the
root is displayed in the next line.
If D < 0 the program displays a message: The equation has no real roots.
Run the script file in the Command Window three times to obtain solution to the
following three equations:
a.) 2x2 + 8x 3 = 0
b.) 15x2 + 10x + 5 =0
c.) 18x2 + 12x + 2 =0
script file:
a=input('a= ');
b=input('b= ');
c=input('c= ');
D=b^2-4*a*c;
if D>0
fprintf('The equation has two roots.')
x1=(-b+sqrt(D))/(2*a)
x2=(-b-sqrt(D))/(2*a)
elseif D==0
fprintf('The equation has one root.')
x=-b/(2*a)
else
fprintf('The equation has no real roots.\n')
end

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

9.) Use loops to create 4 x 7 matrix in which the value of each element is the sum of its
indices (the row number and column of the element). For example, the value of
element A(2,5) is 7.
script file:
A=[ ];
for k=1:4
for h=1:7
A(k,h)=k+h;
end
end
A

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

10.) Use loops and conditional statements to create a 5 x 8 matrix in which the value of
each element is equal to the square root of the sum of the elements indices unless
the element is in an even numbered column or row is equal to the sum of the
elements indices squared. (The indices of an element in a matrix are the row
number and column number of the element.)
script file:

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

11.) Write a program (using a loop) that determines the sum of the first m terms of the
Series :
m
1
( n=0,1,2, , m)
(1 ) 2 n+1
n=0
Run the program with m=10, and m=500. Compare the result with /4. This
series which is called the Leibniz series converges to /4.
script file:
A=[];
for k=1:5
for h=1:8
if ((rem(k,2)==0)|(rem(h,2)==0))
A(k,h)=(k+h)^2;
else
A(k,h)=sqrt(k+h);
end
end
end
A

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

12. A vector is given by: x = [15 -6 0 8 -2 5 4 -10 0.5 3]. Using conditional statements
and loops write a program that determines the sum of the positive elements in the
vector.
script file:
x=[15 -6 0 8 -2 5 4 -10 0.5 3] ;
sum=0;
for h=1:10
if x(1,h)>=0
sum=sum+x(1,h);
end
end
Sum_of_positive_elements=sum

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

13. Write a program in a script file that finds the smallest odd integer that is also
divisible by 3 and whose cube is greater than 4000. Use a loop in the program. The
loop should start from 1 and stop when the number is found. The program prints a
message: The required number is: and then prints the number.
script file:
x=1;
while ~(((rem(x,2)==1)&&(rem(x,3)==0))&&((x^3)>4000))
x=x+1;
end
fprintf('The required number is %0.0f.\n',x)

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

14. Write a user-defined function that sorts the elements of a vector (of any length) from
the largest to the smallest. For the function name and arguments use y = downsort(x).
The input to the function is a vector x of any length, and the output y is a vector in
which the elements of x are arranged in descending order. Do not use the MATLAB sort
function. Test your function on a vector with 14 numbers (integers) randomly distributed
between -30 and 30. Use the MATLAB rand function to generate the initial vector.
script file:
a=60*rand(1,3)-30
b=[];
for j=1:3
for k=1:3
for h=1:3
if a(1,k)>a(1,h)
b(1,j)=a(1,k);
a(1,k)=0;
end
end
end
end
b

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

15. Write a user-defined function that sorts the elements of a matrix. For the function
name and Arguments use B = matrixsort(A), where A is any size matrix and B is a matrix
of the same size with elements of A rearranged in an ascending order row after a row
where the (1,1) element is the smallest, and the (m,n) element is the largest.Test your
function on a 4 x 7 matrix with elements (integers) randomly distributed between -30
and 30. Use the MATLAB rand function to generate the initial matrix.
16. Write a program in a script file that calculates the cost of mailing a package
according to the following price schedule:
Type of service
Weight 0 2 lb.
Weight 2 10 lb.
Weight 10 50 lb.
Ground
$ 1.50
$1.50 + $0.50 for
$5.50 + $0.30 for
each pound or
each pound or
fraction of a pound
fraction of a pound
above 2 lb.
above 10 lb.
Air
$ 3.00
$3.00 + $0.90 for
$10.20 + $0.60 for
each pound or
each pound or
fraction of a pound
fraction of a pound
above 2 lb.
above 10 lb.
Overnight
$ 18
$18.00 + $6.00 for
No overnight
each pound or
service for
fraction of a pound
packages above 10
above 2 lb.
lb.
The program asks the user to enter the weight and the type of service.
The program then displays the cost. If a weight larger than 50 lb is entered for
ground or air service, a message Ground (or Air) service is not available for
packages that weigh more than 50 lb is displayed. If a weight larger than 10 lb is
entered for overnight service a message Overnight service is not available for
packages that weigh more than 10 lb is displayed. Run the program and enter
0.5, 6.3, 20, and 50.4 lb for Ground and Air service, and 2, 8.1, and 13 lb for
Overnight service.
script file:
format bank;
w=input('Enter the weight ');
s=input('Type of service(1-Ground,2-Air,3-Overnight) ');
if s==1
if w<=2
Cost=1.5
elseif (w>=2)&&(w<=10)
Cost=1.5+(w-2)*0.5
elseif (w>=10)&&(w<=50)
Cost=5.5+(w-10)*0.3

else
fprintf('Ground service is not available for packages
that weigh more than 50lb.')
end
elseif s==2
if w<=2
Cost=3.00
elseif (w>=2)&&(w<=10)
Cost=3.00+(w-2)*0.9
Kiling, Lormhel J.
elseif (w>=10)&&(w<=50)
Montalban, Jonathan T.
Cost=10.2+(w-10)*0.6
Villanueva, Jordan J.
else

fprintf('Air service is not available for packages that


weigh more than 50lb.')
end
else
if w<=18
Cost=1.5
elseif (w>=2)&&(w<=10)
Cost=18+(w-2)*6
else
fprintf('Overnight service is not available for
packages that weigh more than 10lb.')
end
end

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

17. A vector x is given by: x = [1:50]. Write a program in a script that removes all the
vectors elements that are divisible by 3, 4, or 5 and displays the new vector.
script file:

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

18. Write a user-defined function that determines the polar coordinates of a point from
the Cartesian coordinates in a two-dimensional plane. For the function name and
arguments use [theta radius] =CartesianToPolar(x,y). The input arguments are the x and
y coordinates of the point, and the output arguments are the angle and the radial
distance to the point. The angle is in degrees and is measured relative to the positive
x axis, such that it is a positive number in quadrants I, II, and III, and a negative number
in the IV quadrant. Use the function to determine the polar coordinates of points (15, 3),
(-7, 12), and (10, -6.5).
script file:

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

19. A cylindrical, vertical fuel tank has hemisphere end caps as shown. The radius of the
cylinder and the caps is r = 40 cm, and the length of the cylindrical part is 1.2 m.
Write a user-defined function (for the function name and arguments use V =
Vfuel( h ) that gives the volume of the fuel in the tank as a function of the height h.
Use the function to make a plot of the volume as a function of h for 0 h 2 m.
script file:

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

20. The velocity, as a function of time of a particle that moves along a straight line, is
shown on the right and given in the equation below.

1.4 t for 0 t 10 s

( t 10 ) for 10 t 25 s
14+5 sin
10
v ( x )=
9 for 25 t 35 s
9
9 ( t 35 ) for 35 t 40 s
5

Write two user- defined functions: One that calculates the velocity of the particle at
time t (for the function name and arguments use v = velocity (t)), and the other that
calculates the acceleration of the particle at time t (for the function name and
arguments use a = acceleration (t)). In a script file, write a program that creates plots
of the velocity and acceleration as functions of time (two plots on the same page). In
the program, first create a vector t, 0 t 40 s, and then use the functions velocity
and acceleration to create vectors of velocity and acceleration that are used for the
plots.

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

21. A scale is made of a tray attached to springs. When an object is placed on the tray,
the tray moves down and the weight of the object can be determined from the
displacement of the tray. Initially, only the two outside springs support the weight. If the
object is heavy enough, however, the tray makes contact with a third spring in the
middle.
k1 = 800 N/m, k2 = 1700 N/m, d = 20 mm.
Write a user-defined function that calculates the weight W of the object for a given
displacement x of the tray. For the function name and arguments use W = scale(x).
a) Using the function in the Command Window, determine the weight of two objects
for tray displacements of 1.5 and 3.1 cm.
b) Write a program in a script file that plots the weight as a function of displacement
for 0x4 cm.

Kiling, Lormhel J.
Montalban, Jonathan T.
Villanueva, Jordan J.

Вам также может понравиться