Reputation: 63
I was given:
x = 2:0.1:10
y = sin(x)
and was asked to plot this function and fill it with green colour for sin above the x-axis and magenta for sin below the x-axis.
Since we have just started with matlab the colouring did not have to be exact but should cover most of the area between the function and axis.
My question is, why is this correct:
fill([2 2 2.6 pi], [0 sin(2) sin(2.6) 0], 'g');
why do I have to put two 2's in for the x-value and why does the y-value start and end with 0?
I am finding it difficult to understand where these numbers come from because for the second area,
fill([pi 3.9 3*pi/2 5.5 2*pi], [0 sin(3.9) -1 sin(5.5) 0], 'm');
I do not put in the same number twice for the x-values but still have to put in zeros for the y-values.
I would appreciate if someone could please explain how and why this data is chosen.
Upvotes: 0
Views: 624
Reputation: 15349
There are repeated x
values wherever the author of these examples has chosen to draw a vertical edge (by definition, vertical means x doesn't change). There are zero y
values wherever the author has chosen to fill all the way down to the x axis (where by definition, the y value is 0). Or up to it, in the second example.
To see this, for each of your examples, take the x values and the y values and look at them as a series of x,y pairs like this (in your first example):
>> [x(:), y(:)]
ans =
2.0000 0
2.0000 0.9093
2.6000 0.5155
3.1416 0
Now understand that each row gives you the coordinates of one vertex of a polygon that you are filling. Compare each coordinate point against the figures that are plotted.
If the object of the exercise is always to fill up to, or down to, the x axis, then you would generally expect repeated x
values at both beginning and end---but in the two specific examples you've given, y
seems to have already gone to zero (at the end of your first example, and at both beginning and end of your second example). It would be permissible (and perhaps more consistent, less confusing) to draw extra zero-length edges to the x axis—but it's redundant, and the author has chosen to omit them.
Upvotes: 0
Reputation: 134
The first field is for all the x-values and the second field is for all the y-values. These values form the ordered pairs that define the co-ordinates of the corners of the shape that you want to fill.
For example, your second example
fill([pi 3.9 3*pi/2 5.5 2*pi], [0 sin(3.9) -1 sin(5.5) 0], 'm');
has the corners
Upvotes: 0