Chapter_4
-
Upload
saadkhalis -
Category
Documents
-
view
1.055 -
download
0
description
Transcript of Chapter_4
-
5/24/2018 Chapter_4
1/19
Chapter 4: Loop Statements
Exercises1) Write a forloop that will print the column of real numbers from 1.5 to 3.1 in
steps of 0.2.
f or i = 1. 5: 0. 2: 3. 1di sp( i )
end
2) Write a function sumsteps2that calculates and returns the sum of 1 to nin steps
of 2, where nis an argument passed to the function. For example, if 11 is passed, it
will return 1 + 3 + 5 + 7 + 9 + 11. Do this using a for loop. Calling the function will
look like this:>> sumsteps2(11)
ans =36
sumst eps2. mf unct i on out sum = sumst eps2(n)% sum f r om 1 t o n i n st eps of 2% For mat of cal l : sumst eps2( n)% Retur ns 1 + 3 + . . . + n
out sum = 0;f or i = 1: 2: n
out sum = out sum + i ;endend
3) Write a functionprodby2that will receive a value of a positive integer nand will
calculate and return the product of the odd integers from 1 to n(or from 1 to n-1if n
is even).
prodby2.m
f unct i on out = pr odby2( n)% Cal cul at es and r et ur ns 1*3*5*. . *n
% For mat of cal l : pr odby2( n)% Ret ur ns pr oduct f r om 1 t o n i n st eps of 2
out = 1;f or i = 1: 2: n
out = out * i ;endend
-
5/24/2018 Chapter_4
2/19
4) Prompt the user for an integer nand print I love this stuff! n times.
Ch4Ex4.m
% Pr ompt s t he user f or an i nt eger n and pr i nt s
% "I l ove t hi s stuf f " n t i mes
n = i nput ( ' Ent er an i nt eger : ' ) ;f or i = 1: n
di sp( ' I l ove thi s stuf f ! ' )end
5) In the Command Window, write a forloop that will iterate through the integers
from 32 to 255. For each, show the corresponding character from the character
encoding.
>> for i = 32:255disp(char(i))
end
!"#$%et c.
6) In the Command Window, write a forloop that will print the elements from a
vector variable in sentence format. For example, if this is the vector:>> vec = [5.5 11 3.45];
this would be the result:
El ement 1 i s 5. 50.El ement 2 i s 11. 00.El ement 3 i s 3. 45.
The forloop should work regardless of how many elements are in the vector.
>> vec = [44 11 2 9 6];
>> for i = 1:length(vec)
fprintf('Element %d is %.2f\n',i,vec(i))
end
El ement 1 i s 44. 00El ement 2 i s 11. 00El ement 3 i s 2. 00
-
5/24/2018 Chapter_4
3/19
El ement 4 i s 9. 00El ement 5 i s 6. 00
7) Write a script that will:
generate a random integer in the range from 2 to 5 loop that many times too prompt the user for a number
o print the sum of the numbers entered so far with one decimal placeCh4Ex7. m% Gener ate a random i nt eger and l oop t o pr ompt t he% user f or t hat many numbers and pr i nt t he runni ng sums
r ani nt = r andi ( [ 2, 5] ) ;r unsum = 0;f or i = 1: r ani nt
num = i nput ( ' Pl ease ent er a number : ' ) ;r unsum = r unsum + num;f pr i nt f ( ' The sum so f ar i s %. 1f \ n' , r unsum)
end
Therearemanyapplicationsofsignalprocessing. Voltages,currents,andsoundsare
allexamplesofsignalsthatarestudiedinadiverserangeofdisciplinessuchas
biomedicalengineering,acoustics,andtelecommunications. Samplingdiscretedata
pointsfromacontinoussignalisanimportantconcept.
8) A sound engineer has recorded a sound signal from a microphone. The sound
signal was sampled, meaning that values at discrete intervals were recorded(rather than a continuous sound signal). The units of each data sample are volts.
The microphone was not on at all times, however, so the data samples which are
below a certain threshold are considered to be data values which were samples
when the microphone was not on, and therefore not valid data samples. The sound
engineer would like to know the average voltage of the sound signal.
Write a script that will ask the user for the threshold and the number of data
samples, and then for the individual data samples. The program will then print the
average and a count of the VALID data samples, or an error message if there were no
valid data samples. An example of what the input and output would look like in the
Command Window is shown below.
Please enter the threshold below which samples will beconsidered to be invalid: 3.0Please enter the number of data samples to enter: 7
Please enter a data sample: 0.4Please enter a data sample: 5.5Please enter a data sample: 5.0
-
5/24/2018 Chapter_4
4/19
Please enter a data sample: 2.1Please enter a data sample: 6.2Please enter a data sample: 0.3Please enter a data sample: 5.4
The average of the 4 valid data samples is 5.53 volts.
Note: In the absence of valid data samples, the program would print an error
message instead of the last line shown above.
Ch4Ex8.m
% Aver age val i d data sampl es f or a sound engi neer
di sp( ' Pl ease ent er t he t hr eshol d bel ow whi ch sampl es wi l l be' )t hr esh = i nput ( ' consi der ed t o be i nval i d: ' ) ;numsamp = i nput ( ' Pl ease enter t he number of dat a sampl es t o beent er ed: ' ) ;
mysum = 0;mycount = 0;f or i =1: numsamp
dat asamp = i nput ( ' \ nPl ease ent er a dat a sampl e: ' ) ;i f dat asamp >= t hresh
mysum = mysum + dat asamp;mycount = mycount + 1;
endendi f mycount > 0
f pr i nt f ( ' \ nThe aver age of t he %d val i d dat a sampl es i s %. 2f
vol t s. \ n' , . . .mycount , mysum/ mycount )el se
f pr i nt f ( ' Ther e wer e no val i d dat a sampl es. \ n' )end
9) Write a script that will load data from a file into a matrix. Create the data file
first, and make sure that there is the same number of values on every line in the file
so that it can be loaded into a matrix. Using a forloop, it will then create as many
Figure Windows as there are rows in the matrix, and will plot the numbers from
each row in a separate Figure Window.
xfile.dat
4 9 2230 18 4
Ch4Ex9.m
% l oad dat a f r om a f i l e and pl ot dat a% f r om each l i ne i n a separ at e Fi gur e Wi ndow
-
5/24/2018 Chapter_4
5/19
l oad xf i l e. dat[ r c ] = s i ze( xf i l e) ;f or i = 1: r
f i gur e( i )
pl ot ( xf i l e( i , : ) , ' k*' )end
10) A machine cuts N pieces of a pipe. After each cut, each piece of pipe is weighed
and its length is measured; these 2 values are then stored in a file called pipe.dat
(first the weight and then the length on each line of the file). Ignoring units, the
weight is supposed to be between 2.1 and 2.3, inclusive, and the length is supposed
to be between 10.3 and 10.4, inclusive. The following is just the beginning of what
will be a long script to work with these data. For now, the script will just count how
many rejects there are. A reject is any piece of pipe that has an invalid weight
and/or length. For a simple example if N is 3 (meaning three lines in the file) and
the file stores:
2. 14 10. 302. 32 10. 362. 20 10. 35
there is only one reject, the second one, as it weighs too much. The script would
print:
There were 1 rejects.
Ch4Ex10.m
% Count s pi pe r ej ect s. I gnor i ng uni t s, each pi pe shoul d be% bet ween 2. 1 and 2. 3 i n wei ght and bet ween 10. 3 and 10. 4% i n l engt h
% r ead t he pi pe dat a and separ at e i nt o vect or sl oad pi pe. datwei ght s = pi pe( : , 1) ;l engt hs = pi pe( : , 2) ;N = l engt h( wei ght s) ;
% t he pr ogr ammi ng met hod of count i ng
count = 0;
f or i =1: Ni f wei ght s( i ) < 2. 1 | | wei ght s( i ) > 2. 3 | | . . .
l engt hs( i ) < 10. 3 | | l engt hs( i ) > 10. 4count = count + 1;
endend
-
5/24/2018 Chapter_4
6/19
f pr i nt f ( ' Ther e wer e %d r ej ect s. \ n' , count )
11) Improve the output from the previous problem. If there is only 1 reject, it
should print There was 1 reject.; otherwise for n rejects it should print There
were n rejects.
Ch4Ex11.m
% Count s pi pe r ej ect s. I gnor i ng uni t s, each pi pe shoul d be% bet ween 2. 1 and 2. 3 i n wei ght and bet ween 10. 3 and 10. 4% i n l engt h
% r ead t he pi pe dat a and separ at e i nt o vect or sl oad pi pe. datwei ght s = pi pe( : , 1) ;l engt hs = pi pe( : , 2) ;
N = l engt h( wei ght s) ;
% t he pr ogr ammi ng met hod of count i ngcount = 0;
f or i =1: Ni f wei ght s( i ) < 2. 1 | | wei ght s( i ) > 2. 3 | | . . .
l engt hs( i ) < 10. 3 | | l engt hs( i ) > 10. 4count = count + 1;
endend
i f count == 1f pr i nt f ( ' Ther e was 1 r ej ect . \ n' )
el sef pr i nt f ( ' Ther e wer e %d r ej ect s. \ n' , count )
end
12) When would it matter if a forloop contained f or i = 1: 4 vs.f or i = [ 3 5 2 6] , and when would it not matter?
It would matter if the value of the loop variable was being used in the action of the
loop. It would not matter if the loop variable was just being used to count howmany times to execute the action of the loop.
13) Create a vector of 5 random integers, each in the range from 10 to 10. Perform
each of the following using loops (with ifstatements if necessary):
>> vec = r andi ( [ - 10, 10] , 1, 5)
-
5/24/2018 Chapter_4
7/19
subtract 3 from each elementf or i = 1: l engt h( vec)
vec( i ) - 1end
count how many are positivemysum = 0;f or i =1: l engt h( vec)
i f vec(i ) > 0mysum = mysum + 1;
endendmysum
get the absolute value of each elementf or i = 1: l engt h( vec)
abs(vec( i ) )end
find the maximummymax = vec( 1) ;f or i = 1: l engt h( vec)
i f vec( i ) > mymax
mymax = vec( i ) ;endendmymax
14) Write a function that will receive a matrix as an input argument, and will
calculate and return the overall average of all numbers in the matrix. Use loops, not
builtin functions, to calculate the average.
matave.m
f unct i on out ave = mat ave( mat )
% Cal cul at es t he over al l aver age of number s i n a mat r i x% usi ng t he pr ogrammi ng met hods% For mat of cal l : mat ave( mat r i x)% Ret ur ns t he aver age of al l el ement s
mysum = 0;[ r c] = si ze( mat ) ;f or i = 1: r
-
5/24/2018 Chapter_4
8/19
f or j = 1: cmysum = mysum + mat ( i , j ) ;
endendout ave = mysum/ ( r *c) ;
end
15) We have seen that by default, when using builtin functions such as sumand
prodon matrices, MATLAB will perform the function on each column. A dimension
can also be specified when calling these functions. MATLAB refers to the columns as
dimension 1 and the rows as dimension 2, such as the following:>> sum( mat , 1)>> sum( mat , 2)
Create a matrix and find the product of each row and column using prod.
>> mat = r andi ( [ 1, 30] , 2, 3)
mat =11 24 165 10 5
>> pr od( mat )ans =
55 240 80
>> prod( mat , 2)ans =
4224250
16) Create a 3x5matrix. Perform each of the following using loops (with if
statements if necessary):
Find the maximum value in each column. Find the maximum value in each row. Find the maximum value in the entire matrix.
Ch4Ex16.m
% Cr eat e a mat r i x, and use t he pr ogr ammi ng methods t o f i nd% t he maxi mum i n each r ow, and each col umn, and over al l
mat = r andi ( [ 1, 30] , 3, 5)[ r c] = si ze( mat ) ;
% Maxi mum over al lmymax = mat ( 1, 1) ;f or i = 1: r
f or j = 1: c
-
5/24/2018 Chapter_4
9/19
i f mat ( i , j ) > mymaxmymax = mat ( i , j ) ;
endend
end
f pr i nt f ( ' The over al l max i s %. 1f \ n\ n' , mymax)
% Maxi mum f or each rowf or i = 1: r
mymax = mat ( i , 1) ;f or j = 1: c
i f mat ( i , j ) > mymaxmymax = mat ( i , j ) ;
endendf pr i nt f ( ' The max of r ow %d i s %. 1f \ n' , i , mymax)
endf pr i nt f ( ' \ n' )
% Maxi mum f or each col umnf or j = 1: c
mymax = mat ( 1, j ) ;f or i = 1: r
i f mat ( i , j ) > mymaxmymax = mat ( i , j ) ;
endendf pr i nt f ( ' The max of col %d i s %. 1f \ n' , j , mymax)
end
17) With a matrix, when would:
your outer loop be over the rows your outer loop be over the columns it not matter which is the outer and which is the inner loop?The outer loop must be over the rows if you want to perform an action for every
row; it must be over the columns if you want to perform an action for every column.
It does not matter if you simply need to refer to every element in the matrix.
18) Assume that you have a matrix of integers mat. Fill in the rest of the fprintf
statement so that this would print the product of every row in the matrix, in the
format:The product of r ow 1 i s 162The product of r ow 2 i s 320
et c.Note: the value of colis not needed.
-
5/24/2018 Chapter_4
10/19
[ r ow col ] = si ze( mat ) ;f or i = 1: r ow
f pr i nt f ( ' The pr oduct of r ow %d i s %d\ n' , )end
f pr i nt f ( ' The pr oduct of r ow %d i s %d\ n' , i , pr od( mat ( i , : ) ) )
19) Write a script beautyofmaththat produces the following output. The script
should iterate from 1 to 9 to produce the expressions on the left, perform the
specified operation to get the results shown on the right, and print exactly in the
format shown here.
>> beautyofmath
1 x 8 + 1 = 9
12 x 8 + 2 = 98123 x 8 + 3 = 9871234 x 8 + 4 = 987612345 x 8 + 5 = 98765123456 x 8 + 6 = 9876541234567 x 8 + 7 = 987654312345678 x 8 + 8 = 98765432123456789 x 8 + 9 = 987654321
beautyofmath.m
% Shows t he beaut y of mat h!
l ef t num = 0;f or i = 1: 9
l ef t num = l ef t num * 10 + i ;r esul t = l ef t num * 8 + i ;f pr i nt f ( ' %d x 8 + %d = %d\ n' , l ef t num, i , r esul t )
end
20) Write a script that will print the following multiplication table:12 4
3 6 94 8 12 165 10 15 20 25
Ch4Ex20.m
% Pr i nt s a mul t i pl i cat i on t abl e
r ows = 5;
-
5/24/2018 Chapter_4
11/19
f or i = 1: r ows
f or j = 1: if pr i nt f ( ' %d ' , i *j )
end
f pr i nt f ( ' \ n' )end
21) The Wind Chill Factor (WCF) measures how cold it feels with a given air
temperature T (in degrees Fahrenheit) and wind speed V (in miles per hour). One
formula for WCF is
WCF = 35.7 + 0.6 T 35.7 (V 0.16) + 0.43 T (V 0.16)
Write a function to receive the temperature and wind speed as input arguments, and
return the WCF. Using loops, print a table showing wind chill factors for
temperatures ranging from 20 to 55 in steps of 5, and wind speeds ranging from 0
to 55 in steps of 5. Call the function to calculate each wind chill factor.
Ch4Ex21.m
% Pr i nt t abl e of wi nd chi l l f act or s
% Pr i nt col umn header sf pr i nt f ( ' %45s\ n ' , ' Wi nd Speeds' )f or v = 0: 5: 55
f pr i nt f ( ' %7d' , v)endf pr i nt f ( ' \ nTemp\ n' )
f or t = - 20: 5: 55f pr i nt f ( ' %3d' , t )f or v = 0: 5: 55
f pr i nt f ( ' %7. 1f ' , wcf ( t , v) )endf pr i nt f ( ' \ n' )
end
wcf.m
f unct i on out wc = wcf ( t , v)% Cal cul at es t he wi nd chi l l f actor
% For mat of cal l : wcf ( t emper at ur e, wi nd speed)% Ret urns 35. 74 + 0. 6215T ? 35. 75( V 0. 16) + 0. 4275T( V 0. 16)
out wc = 35. 74 + 0. 6215 . * t - 35. 75 . * ( v. 0. 16) + . . .0. 4275 . * t . * ( v. 0. 16) ;
end
-
5/24/2018 Chapter_4
12/19
22) Instead of printing the WCFs in the previous problem, create a matrix of WCFs
and write them to a file.
Ch4Ex22.m
% Pr i nt t abl e of wi nd chi l l f act or s f or t emper at ur es
% r angi ng f r om - 4 t o 11F and wi nd speeds f r om 0 t o 11mph
f or t = - 4: 11f or v = 0: 11
wcf mat ( t +5, v+1) = wcf ( 5*t , 5*v) ;end
end
save wcf s. dat wcf mat - asci i
23) The inverse of the mathematical constant ecan be approximated as follows:
1 11
n
e n
Write a script that will loop through values of nuntil the difference between the
approximation and the actual value is less than 0.0001. The script should then print
out the builtin value of e1and the approximation to 4 decimal places, and also print
the value of nrequired for such accuracy.
Ch4Ex23.m
% Appr oxi mates 1/ e as ( 1- 1/ n) n, and det er mi nes% t he val ue of n r equi r ed f or accur acy t o 4 dec. pl aces
act ual = 1 / exp( 1) ;di f f = 1;n = 0;
whi l e di f f >= 0. 0001n = n + 1;appr ox = ( 1 - 1/ n) n;di f f = act ual - appr ox;
end
f pr i nt f ( ' The bui l t - i n val ue of 1/ e i s %. 4f \ n' , act ual )
f pr i nt f ( ' The appr oxi mat i on i s %. 4f \ n' , appr ox)f pr i nt f ( ' The val ue of n i s %d\ n' , n)
24) Given the following loop:whi l e x < 10
act i onend
For what values of the variablexwould the action of the loop be skipped entirely?
-
5/24/2018 Chapter_4
13/19
The action would be skipped entirely if x is greater than or equal to 10 to begin with.
If the variablexis initialized to have the value of 5 before the loop, what wouldthe action have to include in order for this to not be an infinite loop?
The action would have to increment the value of x, so that eventually it becomes
greater than or equal to 10.
25) Write a script that will prompt the user for the radius rand height of a cone,
errorcheck the users input for the radius and the height, and then calculate and
print the volume of the cone (volume = /3 r2h).
Ch4Ex25.m
% Pr ompt t he user f or t he radi us & hei ght of a cone% and pr i nt t he vol ume
% Er r or - check t he user ' s i nput sr ad = i nput ( ' Ent er t he r adi us of t he cone: ' ) ;whi l e ( r ad findmine
Pl ease ent er your mi ni mum val ue: - 2Pl ease ent er your maxi mum val ue: 3Now ent er your choi ce i n t hi s r ange: 0I t t ook 3 t r i es t o gener at e your number
Ch4Ex26.m
-
5/24/2018 Chapter_4
14/19
% Prompt t he user f or a range of i nt eger s and t hen an% i nt eger i n t hi s r ange. Gener at e r andom i nt eger unt i l% user ' s i s gener at ed, count i ng how many t r i es i t t ook.
mymi n = i nput ( ' Pl ease ent er your mi ni mum val ue: ' ) ;
mymax = i nput ( ' Pl ease ent er your maxi mum val ue: ' ) ;mychc = i nput ( ' Now ent er your choi ce i n t hi s r ange: ' ) ;count er = 1;
myr an = r andi ( [ mymi n, mymax] ) ;whi l e ( myr an ~= mychc)
myr an = r andi ( [ mymi n, mymax] ) ;counter = count er + 1;
endf pr i nt f ( ' I t t ook %d t r i es t o gener at e your number \ n' , count er )
27) Write a script that will prompt the user for N integers, and then write thepositive numbers (>= 0) to an ASCII file calledpos.datand the negative numbers to
an ASCII file called neg.dat. Errorcheck to make sure that the user enters N
integers.
Ch4Ex27.m
% Pr ompt t he user f or N i nt eger s, wr i t i ng t he posi t i ve% i nt eger s t o one f i l e and t he negat i ve i nt eger s t o anot her
% i ni t i al i ze vect or s t o st or e pos and neg i nt eger sposi nt s = [ ] ;
negi nt s = [ ] ;% l oop n t i mesn=10;f or i =1: n
i nput num = i nput ( ' Ent er an i nt eger : ' ) ;num2 = i nt 32( i nput num) ;% er r or check t o make sure i nt eger s are ent er edwhi l e num2 ~= i nput num
i nput num = i nput ( ' I nval i d! Ent er an i nt eger : ' ) ;num2 = i nt 32( i nput num) ;
end
% add t o appr opr i at e vect ori f i nput num < 0negi nt s = [ negi nt s i nput num] ;
el seposi nt s = [ posi nt s i nput num] ;
endend
% wr i t e vect or s to f i l es
-
5/24/2018 Chapter_4
15/19
save pos. dat posi nt s - asci isave neg. dat negi nt s - asci i
28) In thermodynamics, the Carnot efficiency is the maximum possible efficiency of a
heat engine operating between two reservoirs at different temperatures. The Carnotefficiency is given as
1 C
H
T
T
whereC
T andH
T are the absolute temperatures at the cold and hot reservoirs,
respectively. Write a script that will prompt the user for the two reservoir temperatures in
Kelvin and print the corresponding Carnot efficiency to 3 decimal places. The script
should error-check the users input since absolute temperature cannot be less than or
equal to zero. The script should also swap the temperature values ifH
T is less thanC
T .
Ch4Ex28.m% Cal cul at es t he Car not ef f i ci ency, gi ven t he t emper at ur es% of col d and hot r eser voi r s, er r or - checki ng bot h
Tc = i nput ( ' Ent er t he col d r eser voi r t emperat ure: ' ) ;
whi l e Tc
-
5/24/2018 Chapter_4
16/19
% Pr ompt t he user f or posi t i ve number s and st or e t hem% i n a vect or unt i l t he user ent er s a negat i ve number
user val s = [ ] ;newval = i nput ( ' Ent er a posi t i ve number : ' ) ;
whi l e (newval >= 0)user val s = [ user val s newval ] ;newval = i nput ( ' Ent er a posi t i ve number : ' ) ;
end
% di spl ay vect oruser val s
30) Write a script echolettersthat will prompt the user for letters of the alphabet
and echoprint them until the user enters a character that is not a letter of the
alphabet. At that point, the script will print the nonletter, and a count of how many
letters were entered. Here are examples of running this script:
>> echoletters
Ent er a l et t er : TThanks, you enter ed a TEnt er a l et t er : aThanks, you enter ed a aEnt er a l et t er : 88 i s not a l et t erYou enter ed 2 l et t er s
>> echolettersEnt er a l et t er : !! i s not a l et t erYou enter ed 0 l et t er s
The format must be exactly as shown above.
echoletters.m
% Echo pr i nt l et t er s unt i l t he user ent er s a char act er% t hat i s not a l et t er of t he al phabet
count = 0;i nchar = i nput ( ' Ent er a l et t er : ' , ' s ' ) ;
%OR: whi l e i sl et t er ( i nchar )whi l e ( i nchar >=' a' && i nchar =' A' && i nchar
-
5/24/2018 Chapter_4
17/19
endf pr i nt f ( ' %c i s not a l et t er \ n' , i nchar)f pr i nt f ( ' You ent er ed %d l et t er s\ n' , count )
31) Write a script that will use the menufunction to present the user with choices
for functions fix, floor, and ceil. Errorcheck by looping to display the menuuntil the user pushes one of the buttons (an error could occur if the user clicks on
the X on the menu box rather than pushing one of the buttons). Then, generate a
random number and print the result of the users function choice of that number
(e.g. fix(5)).
Ch4Ex31.m
% Make t he user choose a f unct i on ' f i x' , ' f l oor ' or ' cei l '% and pr i nt t hat f unct i on of a r andom number
choi ce = menu( ' Choose a f unct i on' , ' f i x' , ' f l oor ' , ' cei l ' ) ;
whi l e ( choi ce < 1 | | choi ce > 3)f pr i nt f ( ' Er r or ; pl ease choose a f unct i on! \ n' )choi ce = menu( ' Choose a f unct i on' , ' f i x' , ' f l oor ' , ' cei l ' ) ;
end
x = r and*10;swi t ch choi ce
case 1f pr i nt f ( ' si n(%. 1f ) i s %. 1f \ n' , x, f i x(x) )
case 2f pr i nt f ( ' cos(%. 1f ) i s %. 1f \ n' , x, f l oor ( x) )
case 3f pr i nt f ( ' t an( %. 1f ) i s %. 1f \ n' , x, cei l ( x) )end
32) Write a script called prtempsthat will prompt the user for a maximum Celsius
value in the range from 16 to 20; errorcheck to make sure its in that range. Then,
print a table showing degrees Fahrenheit and degrees Celsius until this maximum is
reached. The first value that exceeds the maximum should not be printed. The table
should start at 0 degrees Fahrenheit, and increment by 5 degrees Fahrenheit until
the max (in Celsius) is reached. Both temperatures should be printed with a field
width of 6 and one decimal place. The formula is C = 5/9 (F32). For example, the
execution of the script might look like this (the format should be exactly like this):
>> prtemps
When pr ompt ed, ent er a t emp i n degr ees C i n range - 16t o 20.Ent er a maxi mum t emp: 30Err or ! Ent er a maxi mum t emp: 9
-
5/24/2018 Chapter_4
18/19
F C0. 0 - 17. 85. 0 - 15. 0
.
.
.40. 0 4. 445. 0 7. 2
Ch4Ex32. m% Prompt f or a maxi mum C t emperat ur e and pr i nt a t abl e% showi ng degrees C and degr ees F
f pr i nt f ( ' When pr ompt ed, ent er a temp i n degr ees C i n' )f pr i nt f ( ' r ange - 16\ n t o 20. \ n' )maxtemp = i nput ( ' Ent er a maxi mum t emp: ' ) ;
% Er r or - checkwhi l e maxt emp < - 16 | | maxt emp > 20
maxt emp = i nput ( ' Er r or ! Ent er a maxi mum t emp: ' ) ;end
% Pr i nt t abl e i ncl ude header sf pr i nt f ( ' F C\ n' ) ;
f = 0;c = 5/ 9*( f - 32) ;
whi l e ( c
-
5/24/2018 Chapter_4
19/19
f or i = 1: l engt h( y2)r an = r andi ( [ 1: 2] ) ;y2( i ) = y2( i ) + pl usMi nus( r an) *0. 25;
endpl ot ( x, y, x, y2, ' k* ' )
34) A blizzard is a massive snowstorm. Definitions vary, but for our purposes we
will assume that a blizzard is characterized by both winds of 30 mph or higher and
blowing snow that leads to visibility of 0.5 miles or less, sustained for at least four
hours. Data from a storm one day has been stored in a file stormtrack.dat. There are
24 lines in the file, one for each hour of the day. Each line in the file has the wind
speed and visibility at a location. Create a sample data file. Read this data from the
file and determine whether blizzard conditions were met during this day or not.
Ch4Ex34.m
% Reads wi nd and vi si bi l i t y dat a hour l y f r om a f i l e and
% det er mi nes whether or not bl i zzar d condi t i ons wer e met
l oad st or mt r ack. dat
wi nds = st or mt r ack( : , 1) ;vi si bs = st or mt r ack( : , 2) ;
l en = l engt h( wi nds) ;count = 0;i = 0;% Loop unt i l bl i zzar d condi t i on f ound or al l dat a
% has been read
whi l e count < 4 && i < l eni = i + 1;i f wi nds( i ) >= 30 && vi si bs( i )