Video: Polynomial Example (4.14)
Transcript
Let's do this exercise. So what we have to do. We have to create a function m-file called plotpoly.m and
inputs are the coefficient array of the polynomial and 2 endpoints from where to where you want to plot the polynomial and the output will be the graph of that polynomial between those given end point.
Open a new function m file template.
Your output is a graph so it is put variable name g. you could use y but I am going to use y for something else.
So that is fine. Name of the function is plotpoly. Input arguments are a polynomial array, p, 2 endpoints
a and b, and that is it for input. And you can put some commands there if you like. I am leaving it like that.
Now what we need to do to plot the graph what do we need? We need array of x points and another array of y points, so let's do it. X equal to I can use linspace and I want between a and b, how many points?
Say 400. Or maybe 1 thousand, or 10 thousand, should be enough.
Okay and now your x and y areas are created and we are ready to graph. Don't forget your variable
the output variable is g. So, g equal to plot and you are plotting x and y, plot x , y.
So that is your output.
Now save your function m-flie Save as Plotpoly.m. It is saved now. And now from your command window you can call your function and you can plot any polynomial you want begin any given points.
If we say plot poly - so suppose you want to plot 2 x squared plus 3 x minus 9. And you want to plot between minus 10 and plus 10. So these are your and b. Let's see. See - you got your graph immediately. This is plotted from minus 10 to plus 10 and because your inputted polynomial was a quadratic, it is a parabola.
Okay. Let's try for another polynomial. Say, let's just change it. So suppose I want to plot for 10, 4, 5, 6,
78, minus 98, and I want to plot from minus 100 up to 70. Okay. See we got a different graph now. It is from minus 100 to 70. And the shape is according to whatever polynomial we gave as the input. So I hope this makes sense now.