diff --git a/ch_03/pend_rot.m b/ch_03/pend_rot.m new file mode 100644 index 0000000..91a5af3 --- /dev/null +++ b/ch_03/pend_rot.m @@ -0,0 +1,5 @@ +function dy=pend_rot(t,y,l,w,a) + dy=zeros(size(y)); + dy(1)=y(2); + dy(2)=-9.81/l*sin(y(1))+w^2*sin(y(1))*cos(y(1))+a*w^2/l*cos(y(1)); +end diff --git a/ch_03/rotating_pendulum.m b/ch_03/rotating_pendulum.m new file mode 100644 index 0000000..40075fa --- /dev/null +++ b/ch_03/rotating_pendulum.m @@ -0,0 +1,10 @@ + +l=0.3; +a=1; +w=10; + +my_ode=@(t,y) pend_rot(t,y,l,w,a) + +[t,y]=ode23(my_ode,[0 2],[pi/6,0]); + +plot(t,y(:,1)) diff --git a/octave-workspace b/octave-workspace index 696cc40..2ee3267 100644 Binary files a/octave-workspace and b/octave-workspace differ diff --git a/rotating_pendulum.ipynb b/rotating_pendulum.ipynb new file mode 100644 index 0000000..c585767 --- /dev/null +++ b/rotating_pendulum.ipynb @@ -0,0 +1,334 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%plot --format svg" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "setdefaults" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "pkg load odepkg" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "function dy=pend_rot(t,y,l,w,a)\n", + " dy=zeros(size(y));\n", + " dy(1)=y(2);\n", + " dy(2)=-9.81/l*sin(y(1))+w^2*sin(y(1))*cos(y(1))+a*w^2/l*cos(y(1));\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "my_ode =\n", + "\n", + "@(t, y) pend_rot (t, y, l, w, a)\n", + "\n" + ] + }, + { + "data": { + "image/svg+xml": [ + "\n", + "\n", + "Gnuplot\n", + "Produced by GNUPLOT 5.0 patchlevel 3 \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t\n", + "\t \n", + "\t \n", + "\t\n", + "\t\n", + "\t \n", + "\t \n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t-0.2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.2\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.4\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.6\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.8\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t0.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t1.5\n", + "\t\n", + "\n", + "\n", + "\t\t\n", + "\t\t2\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\t\n", + "\n", + "\n", + "\tgnuplot_plot_1a\n", + "\n", + "\n", + "\n", + "\t\n", + "\t\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "\n", + "l=0.3;\n", + "a=1;\n", + "w=1;\n", + "\n", + "my_ode=@(t,y) pend_rot(t,y,l,w,a)\n", + "\n", + "[t,y]=ode23(my_ode,[0 2],[pi/6,0]);\n", + "\n", + "plot(t,y(:,1))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "error: A(I,J): column index out of bounds; value 4 out of bound 3\r\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[0;31muse_imwrite = 0\r\n", + "use_imwrite = 0\r\n", + "error: print: no axes object in figure to print\r\n", + "error: called from\r\n", + " print at line 304 column 5\r\n", + " _make_figures>inner_print at line 68 column 5\r\n", + " _make_figures>safe_print at line 59 column 5\r\n", + " _make_figures at line 48 column 13\r\n", + "\n", + "\u001b[0m" + ] + } + ], + "source": [ + "x=a*cos(w*t)+l*sin(y(:,1)); % x1-coordinate over time\n", + "y=a*sin(w*t)+l*sin(y(:,1)); % y1-coordinate over time\n", + "z=a-l*cos(y(:,1)); % z1-coordinate over time\n", + "linkx=[zeros(length(t),1),zeros(length(t),1),a*cos(w*t)];\n", + "linky=[zeros(length(t),1),zeros(length(t),1),a*sin(w*t)];\n", + "linkz=[zeros(length(t),1),a*ones(length(t),1),a*ones(length(t),1)];\n", + "for i=1:length(t)\n", + " clf\n", + " plot3(linkx(:,i),linky(:,i),linkz(:,i))\n", + " hold on\n", + " plot3([linkx(3,i),x(i)],[linky(3,i),y(i)],[linkz(3,i),z(i)],'-o')\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ans =\n", + "\n", + " 780 1\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[0;31muse_imwrite = 0\r\n", + "use_imwrite = 0\r\n", + "error: print: no axes object in figure to print\r\n", + "error: called from\r\n", + " print at line 304 column 5\r\n", + " _make_figures>inner_print at line 68 column 5\r\n", + " _make_figures>safe_print at line 59 column 5\r\n", + " _make_figures at line 48 column 13\r\n", + "\n", + "\u001b[0m" + ] + } + ], + "source": [ + "size(y)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Octave", + "language": "octave", + "name": "octave" + }, + "language_info": { + "file_extension": ".m", + "help_links": [ + { + "text": "MetaKernel Magics", + "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" + } + ], + "mimetype": "text/x-octave", + "name": "octave", + "version": "0.19.14" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}