<texit info> author=Roman Putanowicz title=Solution to exercise 3.1.3 </texit>

back

Solution to exercise 3.1.3

There are three main issues in this problem:

  • element-by-element operations on vectors
  • controlling plot annotations
  • saving plot in a file

The standard way to make function plots is to prepare two vectors containing the sequence of function arguments and the corresponding function values. In order to filter each element of the arguments vector x through the function f(x) one have to write function requesting element-by-element operation on vectors, which means prefixing each operator with the “.” (dot): .+, .* and so on.

Controlling plot annotations requires making oneself familiar with several Octave functions and it might be a bit more difficult if one wishes to have the output on the screen as well as in a file. The main difficulty is in setting right font scale and text position.

Finally saving when saving a plot in a file one has to decide on the file format which depend also on the way the picture will be further processed. The figures below show three different ways of making PNG image of the plot:

  • plotting on screen and then making screen capture of the plot window (for instance with gimp or xv)
  • saving the plot directly in PNG format
  • saving the plot in FIG format and the using xfig to modify/enhance it and convert to PNG format.

Screen capture of Octave plotting window.

Direct saving in PNG format.

Export to FIG format and conversion to PNG.

The conversion from FIG to PNG can be done on command line using fig2dev utility:

   fig2dev -L png  file.fig > file.png

or by reading the *.fig file into xifg and exporting it from there.

The script which produces the above images can be downloaded here: function_plotting.m

<sxh c> N = 200; x = linspace(-5,5,N); y = 1./(1 + (x .* sin(x)).^2); axis([-5,5,0,1.5]); plot(x,y,“-;T;”) grid(); set (gca(), “defaultlinecolor”, “red”); l1=line([-pi,-1.8],[1.05,1.28]) l2=line([pi,1.8],[1.05,1.28]) xlabel(“X coordinate [m]”) ylabel(“temperature T [K]”) title(“Temperature distribution”) text(0, 1.3, “Characteristic points”, “fontsize”, 12, “horizontalalignment”, “center”) replot print(“temp_dist.fig”, “-color”, “-F:20”); print(“temp_dist.png”, “-color”, “-r0”); pause() </sxh>

<texit> \begin{lstlisting} N = 200; x = linspace(-5,5,N); y = 1./(1 + (x .* sin(x)).^2); axis([-5,5,0,1.5]); plot(x,y,“-;T;”) grid(); set (gca(), “defaultlinecolor”, “red”); l1=line([-pi,-1.8],[1.05,1.28]) l2=line([pi,1.8],[1.05,1.28]) xlabel(“X coordinate [m]”) ylabel(“temperature T [K]”) title(“Temperature distribution”) text(0, 1.3, “Characteristic points”, “fontsize”, 12, “horizontalalignment”, “center”) replot print(“temp_dist.fig”, “-color”, “-F:20”); print(“temp_dist.png”, “-color”, “-r0”); pause() \end{lstlisting} </texit>

  • en/teaching/subjects/it/labs/sol_3_1_3.txt
  • Last modified: 2017/10/02 15:54
  • (external edit)