- •Міністерство освіти і науки, молоді та спорту україни
- •Laboratory Works of Computing and Programming
- •Contents
- •1.2 Base Concepts of Operating Systems
- •1.3 Linux (lubuntu) Operating System
- •1.4 Tasks for Independent Work
- •1.5 Test Questions
- •2 Laboratory work № 2 Word Processor libreoffice.Writer
- •2.1 What Word Processors Can Do
- •2.2 Tasks for Independent Work
- •2.3 Test Questions
- •3 Laboratory work № 3
- •Introduction to the calc spreadsheet
- •3.1 The Basic Opportunities of Spreadsheets
- •3.1.1 Calc’s Environment
- •3.1.2 Calculations in Calc. Creating and Coping Formulas
- •3.2 The Calc charting capability
- •3.3 Tasks for Independent Work
- •4.2 Main Rules of Works in Scilab System
- •Variables
- •Input of vectors (arrays)
- •Input of matrixes
- •Some operations with matrixes with use of the operator ":"
- •Input from keyboard
- •Operators. Expressions use familiar arithmetic operators and precedence rules.
- •Intrinsic scilab Functions
- •Examples of Expressions
- •4.3 Individual Tasks for Laboratory Work
- •4.4 The Tasks for Self-Examination
- •4.5 Test Questions
- •5 Laboratory work № 5
- •5.3.1The plot function
- •5.3.3 Preparing Graphs for Presentation
- •Interactive Plot Editing
- •5.3.4 3D Plotting
- •5.4 Individual Tasks
- •5.6 Test Questions
- •6 Laboratory work № 6 programming in Scilab
- •6.1 Programming in scilab
- •What Happens When You Call a Function
- •Clearing Functions from Memory
- •6.2 Tasks for laboratory work
- •6.3 Example of performance of the laboratory work
- •6.4 Test Questions
- •7 ReferencEs
5.3.1The plot function
Both plot() and plot2d() create 2D plots. plot() is borrowed from Matlab. Persons with Matlab experience may experience may want to use it the benefits of plot2d() are doubtful.
Scilab has the added plot2d() function. It offers more options to tailor the plot. Multiple plots, for instance (recall however that at multiple plots were done with plot().
The plot2d function has different forms, depending on the input arguments. If y is a vector, plot2d(y) produces a piecewise linear graph of the elements of y versus the index of the elements of y. If you specify two vectors as arguments, plot2d(x,y) produces a graph of y versus x.
Plot2d(x,y,[xcap,ycap,caption])
For example, these statements use the colon operator to create a vector of x values ranging from zero to 2π, compute the sine of these values, and plot the result. The characters %pi create the symbol π.
-->x = 0:%pi/100:2*%pi;
-->y = sin(x);
-->plot2d(x,y)
Now label the axes and add a title.
-->plot(x,y,’X’,’Y’,’Plot function y=sin(x)’)
And add the grid.
-->xgrid(5);
Figure 5.1 – Plot of the Sine function
Function xgrid([style]) adds a grid on a 2D plot. style is the dash id or the color id to use for the grid plotting. Style is an integer.
Multiple Data Sets in One Graph
Multiple x-y pair arguments create multiple graphs with a single call to plot. SCILAB automatically cycles through a predefined (but user settable) list of colors to allow discrimination among sets of data. For example, these statements plot three related functions of x, each curve in a separate distinguishing color.
>>y2 = sin(x-.25);
>>y3 = sin(x-.5);
>>plot(x,y,x,y2,x,y3)
The legend command provides an easy way to identify the individual plots.
>>legend('sin(x)','sin(x-.25)','sin(x-.5)')
Figure 5.2 – Multiple Data Sets in One Graph
Plots of the Parameter Functions
For example, these statements plot function y(t)=0.7cos(t) of x(t)=0.5sin(t), where parameter t € [0, 2π]:
>>t=[0:0.01:2*pi];
>>x=0.5*sin(t);
>>y=0.7*cos(t);
>>plot(x,y)
Plot of the sectionally given function
It is given
These statements plot of the sectionally given function:
>>x1=[-2*pi:0.01:-pi];
>>y1=pi*sin(x1);
>>x2=[-pi:0.01:pi];
>>y2=pi-abs(x2);
>>x3=[pi:0.01:2*pi];
>>y3=pi*sin(x3).^3;
>>x=[x1 x2 x3];
>>y=[y1 y2 y3];
>>plot(x,y)
or
>>plot(x1,y1,x2,y2,x3,y3)
5.3.2 The fplot function
fplot(<nameFunction> ,limits) - produces a graph of function in an interval . Where
<nameFunction> - name of a m-file as:
@ MyFun or 'MyFun'
or a line of a kind:
'sin(x)',
'[sin(x) cos(x) ]',
'[sin(x), myfun1(x), myfun2(x)]'.
limits=[xmin xmax ymin ymax] – defines intervals for values of argument and function.
For example:
fplot(@humps,[0 1]);
fplot('humps',[0 1]);
fplot('[tan(x),sin(x),cos(x)]',2*pi*[-1 1 -1 1]);
fplot('sin(1 ./ x)', [0.01 0.1],1e-3).
5.3.3 Preparing Graphs for Presentation
Specifying Line Styles and Colors
It is possible to specify color, line styles, and markers (such as plus signs or circles) when you plot your data using the plot command.
plot(x,y,'color_style_marker')
color_style_marker is a string containing from one to four characters (enclosed in single quotation marks) constructed from a color, a line style, and a marker type:
color strings are 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'. These correspond to cyan, magenta, yellow, red, green, blue, white, and black;
linestyle strings are '-' for solid, '--' for dashed, ':' for dotted, '-.' For dash-dot. Omit the linestyle for no line;
the marker types are '+', 'o', '*', and 'x' and the filled marker types are 's' for square, 'd' for diamond, '^' for up triangle, 'v' for down triangle, '>' for right triangle, '<' for left triangle, 'p' for pentagram, 'h' for hexagram, and none for no marker.
You can also edit color, line style, and markers interactively.
Plotting Lines and Markers
If you specify a marker type but not a linestyle, SCILAB draws only the marker. For example,
plot(x,y,'ks')
plots black squares at each data point, but does not connect the markers with a line.
The statement
plot(x,y,'r:+')
plots a red dotted line and places plus sign markers at each data point. You may want to use fewer data points to plot the markers than you use to plot the lines. This example plots the data twice using a different number of points for the dotted line and marker plots.
>>x1 = 0:pi/100:2*pi;
>>x2 = 0:pi/10:2*pi;
>>plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
Figure 5.3 - This example plots the data twice using a different number of points for the dotted line and marker plots
Figure command
The figure command creates a new figure object using default property value:
>>x1=[-2*pi:0.01:-pi];
>>y1=pi*sin(x1);
>>x2=[-pi:0.01:pi];
>>y2=pi-abs(x2);
>>x3=[pi:0.01:2*pi];
>>y3=pi*sin(x3).^3;
>>figure;plot(x1,y1);grid on;
>>figure;plot(x2,y2);grid on;figure;plot(x3,y3);grid on
Adding Plots to an Existing Graph
The hold command enables you to add plots to an existing graph. When you type
>>hold on
SCILAB does not replace the existing graph when you issue another plotting command; it adds the new data to the current graph, rescaling the axes if necessary.
>>x1=[-2*pi:0.01:-pi];
>>y1=pi*sin(x1);
>>x2=[-pi:0.01:pi];
>>y2=pi-abs(x2);
>>x3=[pi:0.01:2*pi];
>>y3=pi*sin(x3).^3;
>>figure;plot(x1,y1);hold on;plot(x2,y2);hold on;plot(x3,y3);grid on
Create axes in tiled positions
Command subplot(m,n,p), or subplot(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc. For example,
>> figure; subplot(3,1,1); plot(x1,y1); grid on
>> subplot(3,1,2); plot(x2,y2); grid on
>> subplot(3,1,3); plot(x3,y3) ;grid on
|
Figure 5.4 - Illustration shows 3 subplot regions and indicates the command used to create each
Controlling the Axes
The axis command supports a number of options for setting the scaling, orientation, and aspect ratio of plots. You can also set these options interactively.
Setting Axis Limits
By default, SCILAB finds the maxima and minima of the data to choose the axis limits to span this range. The axis command enables you to specify your own limits
axis([xmin xmax ymin ymax])
or for three-dimensional graphs,
axis([xmin xmax ymin ymax zmin zmax])
Setting Axis Visibility
You can use the axis command to make the axis visible or invisible.
axis on
makes the axis visible. This is the default.
axis off
makes the axis invisible.
Setting Grid Lines
The grid command toggles grid lines on and off. The statement
grid on
turns the grid lines on and
grid off
turns them back off again.
Axis Labels and Titles
The xlabel, ylabel, and zlabel commands add x-, y-, and z-axis labels. The title command adds a title at the top of the figure and the text function inserts text anywhere in the figure. A subset of TeX notation produces Greek letters. You can also set these options interactively.
t = -pi:pi/100:pi;
y = sin(t);
plot(t,y)
axis([-pi pi -1 1])
xlabel('-\pi \leq {\itt} \leq \pi')
ylabel('sin(t)')
title('Graph of the sine function')
text(1,-1/3,'{\itNote the odd symmetry.}')
Figure 5.5 – Axis Labels and Titles
Saving a Figure
To save a figure, select Save from the File menu. To save it using a graphics format, such as TIFF, for use with other applications, select Export from the File menu. You can also save from the command line—use the saveas command, including any options to save the figure in a different format.