Main topics: User Defined Functions Function .m files 1. Open MATLAB 2. Create a
ID: 3601238 • Letter: M
Question
Main topics: User Defined Functions Function .m files
1. Open MATLAB
2. Create a Function file named Lab07
(a) Right click Current Folder title of that window
(b) Select New File by moving mouse to that option
(c) Click on Function in the pop-up menu
(d) Type Lab07 in the highlight field and hit the enter key
3. Double click the Lab07.m entry (that you just created) in the Current Folder window
4. We have seen that we can generate a random number in the range [1, 6] to represent the roll of a six sided die, by using: randi( [1, 6], 1 ) Modify the Function file so that it:
(a) Takes three such die values as input
(b) Determines if those three die values constitute a Straight - they can be arranged to form a sequence of consecutive values
(c) Returns true if they constitute a Straight, and false if not.
5. Create a Function file named Lab07
(a) Right click Current Folder title of that window
(b) Select New File by moving mouse to that option
(c) Click on Script in the pop-up menu
(d) Type Driver07 in the highlight field and hit the enter key
6. Double click the Driver07.m entry (that you just created) in the Current Folder window Modify the Script file so that it:
(a) Generates three random ”die” values
(b) Determines if those three die values constitute a Straight by call your function Lab07
(c) Displays the generated die values and the functions output in a reasonable report like format
Explanation / Answer
Lab07.m
-----------------------------------------------------------------------------------------------------------------
function out = Lab07( in1, in2, in3)
if in2 = in1 + 1 and in3 = in1 + 2
out = TRUE
elseif in2 = in1 + 2 and in3 = in1 + 1
out = TRUE
elseif in1 = in2 + 1 and in3 = in2 + 2
out = TRUE
elseif in1 = in2 + 2 and in3 = in2 + 1
out = TRUE
elseif in1 = in3 + 1 and in2 = in3 + 2
out TRUE
elseif in1 = in3 + 2 and in2 = in3 + 1
out = TRUE
else
out = FALSE
end
-------------------------------------------------------------------------------------------------------------------------
Driver07.m
-------------------------------------------------------------------------------------------------------
function Driver07()
a = randi( [1,6], 1)
b = randi( [1,6], 1)
c = randi( [1,6], 1)
result = Lab07(a,b,c)
------------------------------------------------------------------------------------------------------
In this result holds the truth value whether the values a,b,c are straight or not.
/* hope this helps */
/* I am newbie to matlab, if any error please comment and let me solve that */
/* thank you */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.