<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Viamentis Blog &#187; chart</title>
	<atom:link href="http://blog.viamentis.com/articles/category/chart/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.viamentis.com</link>
	<description>Curious About Everything</description>
	<lastBuildDate>Mon, 12 Jul 2010 11:16:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Plotting Mathematical functions with Gnuplot</title>
		<link>http://blog.viamentis.com/articles/2008/01/18/plotting-mathematical-functions-with-gnuplot/</link>
		<comments>http://blog.viamentis.com/articles/2008/01/18/plotting-mathematical-functions-with-gnuplot/#comments</comments>
		<pubDate>Fri, 18 Jan 2008 13:00:44 +0000</pubDate>
		<dc:creator>unni</dc:creator>
				<category><![CDATA[chart]]></category>
		<category><![CDATA[gnuplot]]></category>
		<category><![CDATA[gnuplot graph]]></category>

		<guid isPermaLink="false">http://blog.viamentis.com/articles/2008/01/18/plotting-mathematical-functions-with-gnuplot/</guid>
		<description><![CDATA[Complex math always scares me. Mathematical functions written on the paper never made any sense to me. But i used to find it interesting to plot these functions and see the shapes they produce.But plotting them with pencil and paper was painful and took lot of time. Later after I learnt some elementary programming in [...]]]></description>
			<content:encoded><![CDATA[<p>Complex math always scares me. Mathematical functions written on the paper never made any sense to me. But i used to find it interesting to plot these functions and see the shapes they produce.But plotting them with pencil and paper was  painful and took lot of time. Later after I learnt some elementary programming in C, I generated datasets with simple programs and tried using<br />
putpixel(x,y) like functions, to plot them on screen. Even that  wasn&#8217;t easy and dint get smooth curves.</p>
<p>Now i am trying to do the same exercise using a powerful tool of the GNU tool chain &#8211; <a href="http://www.gnuplot.info/">Gnuplot</a>. Its available free with almost all Linux distros or can be downloaded for free from <a href="http://www.gnuplot.info/download.html">here</a>. Debian users can use</p>
<p><code>sudo apt-get install gnuplot</code></p>
<p><u><strong>Getting started with gnuplot</strong></u></p>
<p>gnuplot is extremely user/programmer friendly. Just type  &#8216;gnuplot&#8217; to get the gnuplot shell. I wanted to see my favorite Sine wave first.</p>
<p>gnuplot&gt; plot sin(x)<br />
Thats it and i got my first function plotted on gnuplot. It cant get anymore simple..!</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/sine.png" title="sine.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/sine.thumbnail.png" alt="sine.png" /></a></p>
<p><u><strong>Saving output to file</strong></u></p>
<p>Next I wanted to save the graph as an image file.</p>
<p>gnuplot&gt; set terminal png<br />
gnuplot&gt; set output &#8220;sine.png&#8221;<br />
gnuplot&gt; replot</p>
<p>Now the output is dumped into &#8220;sine.png&#8221; in the current directory.</p>
<p>&#8216;replot&#8217; simply repeats the last &#8216;plot&#8217; call.</p>
<p><u><strong>Plotting multiple functions in the same graph</strong></u></p>
<p>Intuitive ways work perfectly on gnuplot. To plot sine and cos waves on the same graph :<br />
gnuplot&gt; plot sin(x),cos(x)<br />
OR<br />
gnuplot&gt; plot sin(x)<br />
gnuplot&gt; replot cos(x)</p>
<p>replot adds its arguments to the previous plot&#8217;s argument list and then calls plot again.</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/multiple.png" title="multiple.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/multiple.thumbnail.png" alt="multiple.png" /></a></p>
<p><u><strong>Plotting parametric equations</strong></u></p>
<p>1. <strong>Line</strong></p>
<p>x = constant<br />
y = t</p>
<p>gnuplot&gt; set parametric<br />
gnuplot&gt; plot 3,t</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/vertical_line.png" title="vertical_line.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/vertical_line.thumbnail.png" alt="vertical_line.png" /></a></p>
<p>gnuplot&gt; plot t,3</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/horizontal_line.png" title="horizontal_line.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/horizontal_line.thumbnail.png" alt="horizontal_line.png" /></a></p>
<p>2. <strong>Square</strong></p>
<p>Basically 2 vertical and 2 horizontal striaght lines.</p>
<p>gnuplot&gt; set parametric<br />
gnuplot&gt; set xrange[-2:8]<br />
gnuplot&gt; set yrange[-2:8]<br />
gnuplot&gt; set trange[0:4]<br />
gnuplot&gt; plot 0,t<br />
gnuplot&gt; replot 4,t<br />
gnuplot&gt; replot t,0<br />
gnuplot&gt; replot t,4</p>
<p>3. <strong>Circle</strong><br />
Circle of radius r and centered at (a,b) :</p>
<p>x = r * Cos(t) + a<br />
y = r * Sin(t)  + b</p>
<p>gnuplot&gt; set parametric<br />
gnuplot&gt; set angle degree<br />
gnuplot&gt; set trange [0:360]<br />
gnuplot&gt; set size square<br />
gnuplot&gt; r = 4<br />
gnuplot&gt; plot r*cos(t), r*sin(t)</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/circle.png" title="circle.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/circle.thumbnail.png" alt="circle.png" /></a></p>
<p>4. <strong>Spiral</strong><br />
In the circle&#8217;s equation, instead of keeping radius constant, if we make it a function of t, we get a spiral (when r(t) = t)</p>
<p>x = r(t) * Cos(t) + a<br />
y = r(t) * Sin(t)  + br(t) = t</p>
<p>gnuplot&gt; set parametric<br />
gnuplot&gt; set trange [0:10*pi]<br />
gnuplot&gt; set xrange[-10*pi:10*pi]<br />
gnuplot&gt; set yrange[-10*pi:10*pi]<br />
gnuplot&gt; set samples 1000<br />
gnuplot&gt; plot t*cos(t), t*sin(t)</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/spiral.png" title="spiral.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/spiral.thumbnail.png" alt="spiral.png" /></a></p>
<p>5. <strong>Cardioid</strong><br />
As its name indicates, Cardioid is a heart shaped curve. In spiral&#8217;s parametric equation, if we put  r(t) = 1 + Cos(t) , the result is a cardioid.</p>
<p>gnuplot&gt; set parametric<br />
gnuplot&gt; r(t) = 1 + cos(t)<br />
gnuplot&gt; plot r(t)*cos(t), r(t)*sin(t)</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/cardioid.png" title="cardioid.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/cardioid.thumbnail.png" alt="cardioid.png" /></a></p>
<p><u><strong>Plotting Styles</strong></u><br />
plot command can take different options like lines, impulses, boxes, points, linespoints etc</p>
<p>gnuplot&gt; plot sin(x) with impulses</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/sine_impulse.png" title="sine_impulse.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/sine_impulse.thumbnail.png" alt="sine_impulse.png" /></a></p>
<p><u><strong>3D Plotting</strong></u></p>
<p>splot is the command used for 3-dimensional plotting.</p>
<p>gnuplot&gt; splot sin(x)</p>
<p>Click and drag with mouse to rotate the figure 3-dimensionally.</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/3d_sine.png" title="3d_sine.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/3d_sine.thumbnail.png" alt="3d_sine.png" /></a></p>
<p>i. Sphere</p>
<p>Parametric equations are:</p>
<p>x = cos(u)*sin(v)<br />
y = sin(u)*sin(v)<br />
z = cos(v)</p>
<p>gnuplot&gt; set parametric<br />
gnuplot&gt; set urange [0:2*pi]<br />
gnuplot&gt; set vrange [0:pi]<br />
gnuplot&gt; set isosample 40<br />
gnuplot&gt; splot cos(u)*sin(v),sin(u)*sin(v),cos(v)</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/sphere.png" title="sphere.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/sphere.thumbnail.png" alt="sphere.png" /></a></p>
<p>ii. <strong>Cone</strong></p>
<p>Parametric equations are:</p>
<p>x = r cos(t)<br />
y = r sin(t)<br />
z = t</p>
<p>gnuplot&gt; set parametric<br />
gnuplot&gt; set urange [-2:2]<br />
gnuplot&gt; set vrange [0:2*pi]<br />
gnuplot&gt; set isosample 40<br />
gnuplot&gt; set view 75,30<br />
gnuplot&gt; splot u*cos(v),u*sin(v),u</p>
<p><a href="http://blog.viamentis.com/wp-content/uploads/2008/01/cone.png" title="cone.png"><img src="http://blog.viamentis.com/wp-content/uploads/2008/01/cone.thumbnail.png" alt="cone.png" /></a></p>
<p>Parametric equations for 3d figures were obtained from <a href="href=www.mssu.edu/math/glathrom/math260/gnuplot_tutorial.pdf">Introduction to gnuplot for Vector Calculus</a></p>
<div id="pfButton"><a href="http://blog.viamentis.com/articles/2008/01/18/plotting-mathematical-functions-with-gnuplot/?pfstyle=wp" title="Print an optimized version of this web page"><img id="printfriendly" style="border:none; padding:0;" src="http://cdn.printfriendly.com/pf-button-both.gif" alt="Print"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.viamentis.com/articles/2008/01/18/plotting-mathematical-functions-with-gnuplot/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
