Video: Quadroots function m-file example (6.23)
Transcript
Let us create a function m-file called quadRoots to find the roots of a quadratic polynomial of this form ax squared plus b x plus c. We want these three coefficients to be 3 inputs and the outputs will be the 2 roots r1 and r2 which are calculated using this well known quadratic formula.
Open a new function m-file template. New function Output arguments are r1 and r2. r1, r2. Name of the function is quadRoots -quadRoots - Input arguments are those 3 coefficients a, b and c. All separated by commas and then you can define your r1 and r2 - so r1 is equal to minus b plus the square root of b squared minus 4 ac, so 4 times a times c, and this whole thing is divided by 2a. So put this in a bracket and divide it by 2 times a. And you can put a semicolon here.
Similarly you can define r2. Easier to copy-paste in and then edit. Okay, so. It is r2 and only difference is that you have minus here. That is it. So r1 and r2 are defined. And those are the only 2 outputs.
And therefore your function is defined. Now save your function as the same name as the name of the function dot m. So Save as, it is quadRoots.m. Save it. Okay, it is saved now.
Now see how we call this function from Command window. So if we have to find roots of this first polynomial, x squared plus 3 x plus 2 then those 3 coefficients, a b c will be 1, 3, and 2. So your inputs will be 1, 3, 2 all separated by commas. So let's call up the function. Okay. And it has a specific format because you have 2 outputs. Let me better - copy-paste, copy and I am going to Command Window now. So I can type there r1 comma r2 in a square brackets, quadRoots and those coefficients were 1, 3, and 2. It should give the r1 and r2, so they are minus 1 and minus 2 as expected.
What if I just type quadRoots 1, 3, 2 in Command Window without typing this left hand side in the square bracket - see what happens. So I just want this, see you
get just one output, only r1 but not r2. This is quite annoying. So you have to write the whole thing as you have defined the function.
We can actually modify our function m file to deal with these issues. Okay, let me go to the function file quadRoots dot m. And what I can do instead of having 2 outputs, r1 and r2, I just replace this by just 1 output, r and I can define as an array which consists of r1 and r2. Like that. Okay. And then save the function file. Okay so after this modification, if you go to Command Window and type quadRoots, 1 3 2, it is giving you both the roots, answer 1 and 2.
Let us try with function for another polynomial. x squared plus 6 x plus 10. So a, b, c now are 1, 6, and 10. So in the Command window I will have quadRoots, 1, 6 and 10 and see we got complex roots this time. It looks a bit messy, because they all are in the same row. Can we have them in different rows? Yes we can. We just need to modify our function file a bit.
Let me go to my function m file and what I am going to change is - when you have r1 r2 here, instead of comma use semicolon, so instead of being a row array, a row vector, r will become a column vector. So r1 and r2 will be in different rows. Okay, save it. And go to Command window. And call this function again for 1, 6 and 10. And see, now output is in different lines. That looks neater, right?