User Tools

Site Tools


gibson:teaching:spring-2016:math445:lecture:scripts

This is an old revision of the document!


A PCRE internal error occured. This might be caused by a faulty plugin

====== Math 445 lecture 3: scripts and plotting ====== topics <code> Matlab scripting plot, semilogy, semilogx, loglog xlabel, ylabel, title, legend, axis, grid, clf, linestyles load, save what, pwd </code> ===== Matlab scripting ===== A Matlab **script** is simply a list of Matlab commands in a plain-text (ASCII) file with a ''.m'' filename extension. If you **call** or **execute** the script by its name at the Matlab prompt //without the .m extension//, Matlab will read the file and execute the commands sequentially. For example, here is a Matlab script that will make a plot of $\sin x$ and $\cos x$, complete with labeled axes and a legend. <code matlab | plotsincos.m> % plot cos(x) and sin(x) over 0 < x < 2pi x = linspace(0, 2*pi, 100); plot(x, cos(x), 'b-', x, sin(x), 'r-'); xlabel('x'); legend('cos x', 'sin x'); title('two trig functions') grid on axis equal axis([0 2*pi -1 1]) </code> If you download this file and place it in your current working directory (folder), Matlab will be able to find it and execute it. In my case, Matlab is running in the directory ''/home/gibson/math445''. <code matlab> >> pwd % print working directory ans = /home/gibson/math445 >> what % print names of Matlab scripts in working directory MATLAB Code files in the current folder /home/gibson/math445 </code> When the script is run, Matlab opens a new figure window and draws the plot <code matlab> >> plotsincos % execute the plotsincos script </code> {{ :gibson:teaching:spring-2016:math445:sincos.png?direct&400 |}} ===== Markers and line styles ===== The above plotting script used the matlab **linestyle** codes '' 'b-' '' and '' 'r-' '' to generate blue and red curves for $\cos x$ and $\sin x$. Matlab's **help** function provides a pretty good description of how line and marker style codes work. <code matlab> >> help plot % clip out irrelevant stuff here Various line types, plot symbols and colors may be obtained with plot(X,Y,S) where S is a character string made from one element from any or all the following 3 columns: b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star (none) no line y yellow s square k black d diamond w white v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram For example, plot(X,Y,'c+:') plots a cyan dotted line with a plus at each data point; plot(X,Y,'bd') plots blue diamond at each data point but does not draw any line. </code>

gibson/teaching/spring-2016/math445/lecture/scripts.1454346782.txt.gz · Last modified: 2016/02/01 09:13 by gibson