Video: myRand function m-file example

 

 

Transcript

Let's do this myRand exercise. So our task is to create a function m-file called myRand.m. And what this function is supposed to do? If you input 2 numbers, minRand, and maxRand, then this function should return you a number, a random number, which has value between those 2, given two inputted numbers.

 

Open a new function m-file template. First word has to be function, output is y. Name of function is myRand. Input arguments are minRand and maxRand

and then you can put some comments about your function. Well, I will just copy - paste the text given here in the exercise. So, just this part. Copy - and just Paste in here. So comments about the functions are already there.

 

And next thing is to define the function. So to define the function, insert given there, calculate the scale, calculate the offset and then calculate y, using your scale, offset and MATLAB's rand function. let's do it. Okay, so What is this? Scale? Scale is maxRand minus minRand because that is the difference between the 2 given inputted numbers. So it is maxRand minus minRand. Beware of spelling - you should use same word as you have inputted here. So capital R there. Put a semicolon.

 

What is offset? Offset is you don't want your random number to be smaller than minRand, so that is the smallest threshold. You can't go beyond minRand. So that is the offset for minRand. Okay, and then you can define your function y, the output, so y should be equal to offset plus scale times a random number between 0 and 1. So that is rand in MATLAB. Put a semicolon. So your function is defined. So y is the output and you have defined it here in terms of scale and offset. Scale and offset are already defined in terms of input argument.

 

Okay so, the final thing, is to save this fnction. So Save as. Name of the function is myRand, so it is myRand dot m. Just save it.

 

When you call this function myRand from Command window, with 2 input numbers, minRand and maxRand, what the MATLAB will do? It will calculate the scale using those 2 input numbers, calculate the offset, then generate a random number between 0 and 1, multiply that number with the scale and add the offset value. And what ever the resulting number is, will be the output.

 

Let us try this function in the Command window. So, if I want a random number between 1 and 10, I will type myRand 1 comma 10, and it gives me a random number which is between 1 and 10. Now I want something else. Say 100 and 100 plus 1 - it is still a valid input. Yes. And you are getting a random number between 100 and 101. Next I want a random number between 3 and pi. So I can type myRand 3 comma pi and it gives me a random number between 3 and pi.

 

What if you just give just one input to your function? For example if you say myRand 3? It is suppose to give an error message because your function is expecting you to give 2 inputs. See what it gives. Yes it is giving you an error message, not enough input arguments. So MATLAB can handle this 1 input case and returns an error message.

 

Now, how about the situation if you type myRand, and you type bigger number first and smaller number later? For example, you type myRand 3 comma 1. This will still return you some number, but that may not be the result that you want from your function because you did not write your function that way. You may want to edit your function m-file to handle that kind of situation. Let's do that.

 

I go to my function m-file. And what I want? I don't want this first this input to be bigger or equal to the second input. So I can write here, if maxRand is less than or equal to minRand then give an error message and in the bracket, put in inverted commas, whatever error message you want to see to be displayed, you can write that here. So I can say first input must be strictly smaller - I think that is it. And then you need an end for this if.  And then save it. And now go to Command window and see what happens. What it does. So myRand 3 1 gives you an error message - first input must be strictly smaller. You give any other input like 300 and it is just 100, so smaller, you get the same error message. And if you have the same number repeated then also you should be getting the same error message. So I think this makes sense now.

 

 

Last modified: Monday, 10 August 2015, 10:20 AM