Graphs can be plotted with filling using the area function.
area([3, 4, 1, -2, 0, 3, 4, 1, 2])
xlim([0, 9])
Here are some examples of scripts to plot functions with fillings.
%Piecewise Filling
%Define x
x = -2*pi:0.01:2*pi;
%Sin Graph
y = sin(x);
y((x<-pi)|(x>=pi))=0;
fill(x,y,'r'); %'r' determines filling color (red)
grid on; %Activates a grid on the figure
hold on %Holds the first plot on the figure
%Second plot
%Cos Graph
y = cos(x);
y(x<-pi|x>=pi)=0;
fill(x,y,'k'); % 'k' determines filling color (black)
Another example when different colors are applied:
%Piecewise Filling
%Define x
x = -2*pi:0.01:2*pi;
%Define y(x);
y(x<=0)=cos(x(x<=0));
y(x>0)=sin(x(x>0));
fill(x(x>0),y(x>0),'r'); %'r' determines filling color
hold on
fill(x(x<=0),y(x<=0),'c') %'c' determines filling color
grid on; %Activates a grid on the figure
plot(x,y,'.','markersize',8) % plot function
title({'\rmPiecewise function y(x) = cos\itx\rm, -2\pi \leq x\leq 0,';...
'y(x) = sin\itx\rm, 0 < x\leq 2\pi'})
Another example:
%1 Smooth
x = linspace(0,2*pi,100);
y = sin(x);
area(x,y,'facecolor','green')
hold on
%2 Rough
x = linspace(0,2*pi,10);
y = sin(x);
area(x,y)
hold on
%Label Figure/Plot
title('Sin Function and it''s Rough Approximation')
xlabel('x')
ylabel('sin(x)')
legend('Sin(x)','Rough')
Example:
**DESCRIPTION OF PROBLEM GOES HERE**
This is a description for some MATLAB code. MATLAB is an extremely useful tool for many different areas in engineering, applied mathematics, computer science, biology, chemistry, and so much more. It is quite amazing at handling matrices, but has lots of competition with other programs such as Mathematica and Maple. Here is a code snippet plotting two lines (
y vs. x and
z vs. x) on the same graph. Click to view the code!
figure(1)
plot(x, y, 'Color', [1 0 0]) %blue line
hold on
plot(x, z, 'Color', [0 1 0]) %green line