|
|
Line 3: |
Line 3: |
|
| |
|
| {{MerlinMultipageExperienceSubpages | | {{MerlinMultipageExperienceSubpages |
| |Pages=Preparation;Graphing;Sombrero | | |Pages=Preparation;Graphing;Sombrero;Histograms;Storage |
| }} | | }} |
|
| |
|
| |
| == Preparation ==
| |
| In this exercise, you'll be generating graphic files by using Octave. In order to view these files, you'll save them in a special directory called "~www" which must be created in your home directory. The following instructions will instruct you on how to create this directory.
| |
| {{Prepare WWW Directory}}
| |
| == Graphing ==
| |
| Octave may be started by typing 'octave' in the shell. It's easiest to perform this in your '''www''' directory so the files that you create will be saved there.
| |
| {{ConsoleLine|liang-xue@codermerlin:~$|cd ~/www}}
| |
| {{ConsoleLine|liang-xue@codermerlin:~$|octave}}
| |
|
| |
| You'll know you're in Octave when you see the Octave prompt.
| |
| {{ConsoleLine|octave:1>|}}
| |
|
| |
| In order to enable graphics plotting, execute the following command:
| |
| {{ConsoleLine|octave:1>|graphics_toolkit("gnuplot")}}
| |
|
| |
| We'll begin with a 3-D example to demonstrate the power of Octave and the ease with which complex graphs can be created.
| |
| == Examples ==
| |
| === 3-D Sombrero Plot ===
| |
| {{ConsoleLine|octave:2>|tx {{Equal}} ty {{Equal}} linspace (-8, 8, 41);}}
| |
| {{ConsoleLine|octave:3>|[xx, yy] {{Equal}} meshgrid (tx, ty);}}
| |
| {{ConsoleLine|octave:4>|r {{Equal}} sqrt (xx .^ 2 + yy .^ 2) + eps;}}
| |
| {{ConsoleLine|octave:5>|tz {{Equal}} sin (r) ./ r;}}
| |
| {{ConsoleLine|octave:6>|mesh (tx, ty, tz);}}
| |
| {{ConsoleLine|octave:7>|xlabel ("tx");}}
| |
| {{ConsoleLine|octave:8>|ylabel ("ty");}}
| |
| {{ConsoleLine|octave:9>|zlabel ("tz");}}
| |
| {{ConsoleLine|octave:10>|title ("3-D Sombrero Plot");}}
| |
| We can now print the plot to a file with:
| |
| {{ConsoleLine|octave:11>|print -dpng sombrero.png}}
| |
| It may take a few seconds to produce the file; be patient.
| |
|
| |
| It's necessary to adjust the file's permissions to enable web server access. This may easily be performed from within octave using the following command:
| |
| {{ConsoleLine|octave:12>|system("chmod -R a+rX ~/www")}}
| |
| When the prompt returns, the file may be viewed in a browser. Every user has a personal URL. Your file may be viewed here: {{PersonalURL|path=sombrero.png}}
| |
|
| |
| === Random Numbers ===
| |
| We can produce results similar to those of throwing a six-sided die by using the '''rand''' function which returns a random value in the interval (0, 1).
| |
| {{ConsoleLine|octave:13>|rand}}
| |
| Try executing this command several times and observe the results. In order to obtain integer values in the range from 1 to 6 we'll multiply the result of '''rand''' by 6, add 1, and then take the '''floor''' of this result.
| |
| {{ConsoleLine|octave:16>|floor(6*rand + 1)}}
| |
| Try executing this command several times and observe the results.
| |
| === Array of Random Numbers ===
| |
| An array of 10 random throws of a six-sided die can be produced as follows.
| |
| {{ConsoleLine|octave:17>|A {{Equal}} floor(6 * rand(10, 1) + 1)}}
| |
| === Plotting a Histogram ===
| |
| Histograms can easily be plotted from the array. The second argument [1 2 3 4 5 6] provides the midpoints of the 'bins' for our histogram.
| |
| {{ConsoleLine|octave:18>|hist(A, [1 2 3 4 5 6]);}}
| |
| === Plotting Multiple Histograms on Same Graph ===
| |
| <syntaxhighlight lang="matlab">
| |
| # Plots three histograms of random data from six-sided die
| |
| # Histograms vary in their population
| |
| graphics_toolkit("gnuplot")
| |
|
| |
| # Generate three random data sets of six-sided die throws
| |
| A = floor(6 * rand(10000, 1) + 1)
| |
| B = floor(6 * rand(100, 1) + 1)
| |
| C = floor(6 * rand(10, 1) + 1)
| |
|
| |
| # Plot data as histograms
| |
| hold on
| |
| hist(A, [1, 2, 3, 4, 5, 6], "facecolor", "cyan");
| |
| hist(B, [1, 2, 3, 4, 5, 6], "facecolor", "magenta");
| |
| hist(C, [1, 2, 3, 4, 5, 6], "facecolor", "green");
| |
|
| |
| # Use a logarithmic scale
| |
| set(gca, 'yscale', 'log');
| |
|
| |
| # Add Titles and Legends
| |
| grid on
| |
| title("Histogram Plots: Six-sided Die Simulations");
| |
|
| |
| xlabel("Six-sided Die Roll")
| |
| ylabel("Frequency of Occurrence (log scale)")
| |
|
| |
| legend ("10 thousand samples", "100 samples", "10 samples", "location", "northeastoutside");
| |
|
| |
| # Print to a file
| |
| print -dpng histogram.png
| |
|
| |
| </syntaxhighlight>
| |
|
| |
| === Reading from a Remote URL ===
| |
| We can read the contents of a remote URL using the following command:
| |
| {{ConsoleLine|octave:17>|s {{Equal}} urlread ("<nowiki>https://www.codermerlin.com/users/john-williams/sample.csv</nowiki>");}}
| |
| === Reading from a String ===
| |
| We can read the contents of a string using the following command, interpreting each value read as a Double:
| |
| {{ConsoleLine|octave:18>|A {{Equal}} strread(s, "%n");}}
| |
| === Appending from a String ===
| |
| We can append the contents of a string to an existing array using the following command:
| |
| {{ConsoleLine|octave:18>|A {{Equal}} [A; strread(s, "%n")];}}
| |
|
| |
|
| == References == | | == References == |
| <references/> | | <references/> |