{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Plot a vector field\n",
"\n",
"First let's look at a simple vector field plot. It requires four parameters `x`, `y`, `dx` and `dy`, where `dx` and `dy` determine the endpoints of the arrows attached to the points with coordinates given in `x` and `y`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:with => \"vectors\"]>], @options=Hamster::Hash[:key => false, :title => \"Vector Field\"], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"require 'gnuplotrb'\n",
"include GnuplotRB\n",
"include Math\n",
"\n",
"x = Array.new(10) { (0..9).to_a }.flatten\n",
"y = (0..9).map { |i| Array.new(10) {i} }.flatten\n",
"dx = x.zip(y).map { |p| cos(p[0].to_f*PI/10.0) }\n",
"dy = x.zip(y).map { |p| sin(p[1].to_f*PI/10.0) }\n",
"\n",
"Plot.new([[x,y,dx,dy], with: 'vectors'], \n",
" key: false, title: 'Vector Field')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Colored arrows\n",
"\n",
"Now, color the arrows according to their slope."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:with => \"vectors\", :lc => \"palette\", :filled => true]>], @options=Hamster::Hash[:key => false, :title => \"Vector Field\", :tics => false], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#the slopes of the vectors on the logistic scale\n",
"col = dx.zip(dy).map do |p| \n",
" p[0]==0 ? 1.0 : 1.0 / (1.0 + Math::exp(-p[1].to_f / p[0].to_f))\n",
"end\n",
"\n",
"Plot.new([[x,y,dx,dy,col], with: 'vectors', filled: true, lc: 'palette'],\n",
" key: false, tics: false, title: 'Vector Field')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##3D vector field\n",
"\n",
"Vector fields may be visualized in 3D as well."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:with => \"vectors\", :lc => \"palette\", :filled => true]>], @options=Hamster::Hash[:key => false, :title => \"3D Vector Field\", :tics => false], @already_plotted=true, @cmd=\"splot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xx = ([x] * 10).flatten\n",
"yy = ([y] * 10).flatten\n",
"zz = (0..9).map { |i| Array.new(100) {i} }.flatten\n",
"dxx = xx.zip(yy, zz).map { |p| cos(p[0].to_f*PI/10.0) }\n",
"dyy = xx.zip(yy, zz).map { |p| sin(p[1].to_f*PI/10.0) }\n",
"dzz = xx.zip(yy, zz).map { |p| cos(p[2].to_f*PI/10.0) }\n",
"color = dxx.zip(dyy, dzz).map do |p| \n",
" p[0]==0 ? 1.0 : 1.0 / (1.0 + exp(-p[1].to_f / p[0].to_f))\n",
"end\n",
"Splot.new(\n",
" [[xx,yy,zz,dxx,dyy,dzz,color], with: 'vectors', filled: true, lc: 'palette'],\n",
" key: false,\n",
" tics: false,\n",
" title: '3D Vector Field'\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.1.2",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": "rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}