This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Session 8: Plotting Graphs [June 7, 2011, 2:31 a.m.]


Objectives

In this session you will learn the following:

  1. Plotting 2 dimensional line graphs
  2. Annotating graphs with grids, axis labels, graph titles
  3. Exporting graphs for inclusion in documents
  4. Plotting 3 dimensional surface graphs
  5. Plotting multiple graphs on one page (subplot())

Functions for Plotting

In this session, we will learn the following functions:

plot(), plot2d(), xgrid(), xtitle(), legend(), plot3d(), linspace()

Plotting Basics

Scilab is a data plotter, that is, it plots graphs from the data you input. This is in contrast to function plotting programs (such as gnuplot) which plot graphs of functions without the user having to first explicitly generate the data required to plot the graph. Function plotting programs will decide the range of the values of independent variable and its data interval, generate the data on the fly and plot the graph.

Let us plot a graph of sin(x) and cos(x) over the range x = 0 to 2 pi. We will first create vector x, containing the values of the independent variable x. Then we will create the matrix y, containing the same number of rows as x, with values of sin(x) in its first column and values of cos(x) in its second column.

-->x = [0:%pi/16:2*%pi]';
-->y = [sin(x), cos(x)];
-->plot(x, y); xgrid(1); xtitle('Harmonic Functions', 'x', 'sin(x) and cos(x)');

This must result in the following graph being plotted: