{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## GNU Octave \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Operations" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 12\n" ] } ], "source": [ "5+7" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 1.5000\n" ] } ], "source": [ "6/4" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = -1\n" ] } ], "source": [ "5-6" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 16\n" ] } ], "source": [ "2^4" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 25\n" ] } ], "source": [ "5*5\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = -0.95892\n" ] } ], "source": [ "sin(5)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 0.28366\n" ] } ], "source": [ "cos(5)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = -1.2246e-16\r\n" ] } ], "source": [ "tan(pi)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 3.1416\r\n" ] } ], "source": [ "pi" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "format long" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 3.14159265358979\r\n" ] } ], "source": [ "pi" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "format short" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 2.7183\r\n" ] } ], "source": [ "exp(1)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 1\r\n" ] } ], "source": [ "log(exp(1))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 1\r\n" ] } ], "source": [ "log10(10)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 27.290\r\n" ] } ], "source": [ "sinh(4)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 0.99505\r\n" ] } ], "source": [ "tanh(3)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x1 = 80\n", "ans = 12\n" ] } ], "source": [ "x1 = sqrt(100^2 -60^2)\n", "x1*15*(60^2 + x1^2)^(-1/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# vector and matrix" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", "\n", " 1 2\n", "\n" ] } ], "source": [ "% row vector\n", "x=[1 2]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y =\n", "\n", " 3\n", " 4\n", "\n" ] } ], "source": [ "% column vector \n", "y=[3;4]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 3 4\n", "\n" ] } ], "source": [ "% transpose\n", "y'" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y =\n", "\n", " 3 4\n", "\n" ] } ], "source": [ "y=[3 4]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", "\n", " 1 2\n", " 3 4\n", "\n" ] } ], "source": [ "A=[1 2; 3 4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A*A and A^2 are same but A.^2 is different! A*A or A^2 are the multiplication of A with A; however, A.^2 is the component wise pultiplication." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 7 10\n", " 15 22\n", "\n", "ans =\n", "\n", " 7 10\n", " 15 22\n", "\n", "ans =\n", "\n", " 1 4\n", " 9 16\n", "\n" ] } ], "source": [ "A*A\n", "A^2\n", "A.^2" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "B =\n", "\n", " 5 6\n", " 7 8\n", "\n" ] } ], "source": [ "B = [5 6; 7 8]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C =\n", "\n", " -4.0000 3.0000\n", " 3.5000 -2.5000\n", "\n" ] } ], "source": [ "C=B^(-1)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 3.00000 -2.00000\n", " 2.00000 -1.00000\n", "\n" ] } ], "source": [ "A*C" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 1.00000 0.00000\n", " 0.00000 1.00000\n", "\n" ] } ], "source": [ "B*C" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " -20.000 18.000\n", " 24.500 -20.000\n", "\n" ] } ], "source": [ "% component wise multiplication \n", "B.*C" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " -4.0000 3.0000\n", " 3.5000 -2.5000\n", "\n" ] } ], "source": [ "inv(B)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = -2.0000\r\n" ] } ], "source": [ "% determinant\n", "det(B)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'eig' is a built-in function from the file libinterp/corefcn/eig.cc\n", "\n", " -- Built-in Function: LAMBDA = eig (A)\n", " -- Built-in Function: LAMBDA = eig (A, B)\n", " -- Built-in Function: [V, LAMBDA] = eig (A)\n", " -- Built-in Function: [V, LAMBDA] = eig (A, B)\n", " Compute the eigenvalues (and optionally the eigenvectors) of a\n", " matrix or a pair of matrices\n", "\n", " The algorithm used depends on whether there are one or two input\n", " matrices, if they are real or complex and if they are symmetric\n", " (Hermitian if complex) or non-symmetric.\n", "\n", " The eigenvalues returned by 'eig' are not ordered.\n", "\n", " See also: eigs, svd.\n", "\n", "\n", "Additional help for built-in functions and operators is\n", "available in the online version of the manual. Use the command\n", "'doc ' to search the manual index.\n", "\n", "Help and information about Octave is also available on the WWW\n", "at http://www.octave.org and via the help@octave.org\n", "mailing list.\n" ] } ], "source": [ "help eig" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", "\n", " 1 2 3\n", " 4 5 6\n", " 7 8 98\n", "\n" ] } ], "source": [ "A=[1 2 3; 4 5 6; 7 8 98]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eigvec =\n", "\n", " -0.031944 -0.812694 -0.329567\n", " -0.065196 0.582597 -0.938125\n", " -0.997361 0.010440 0.106334\n", "\n", "eigval =\n", "\n", "Diagonal Matrix\n", "\n", " 98.74715 0 0\n", " 0 -0.47228 0\n", " 0 0 5.72513\n", "\n" ] } ], "source": [ "[eigvec, eigval] = eig(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$a_{1,1} \\quad a_{1,2} \\quad a_{1,3}$\n", "\n", "$a_{2,1} \\quad a_{2,2} \\quad a_{2,3}$\n", "\n", "$a_{3,1} \\quad a_{3,2} \\quad a_{3,3}$" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 3\n", " 6\n", " 98\n", "\n" ] } ], "source": [ "A(1:3,3)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 3 3\n", "\n" ] } ], "source": [ "size(A)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", "\n", " 0 0 0\n", " 0 0 0\n", " 0 0 0\n", "\n" ] } ], "source": [ "A=zeros(3)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", "\n", "Diagonal Matrix\n", "\n", " 1 0 0 0\n", " 0 1 0 0\n", " 0 0 1 0\n", " 0 0 0 1\n", "\n" ] } ], "source": [ "A = eye(4)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A =\n", "\n", " 0.691284 0.458066 0.969734 0.981280\n", " 0.523134 0.193470 0.780486 0.076756\n", " 0.414799 0.367310 0.369840 0.400804\n", " 0.431737 0.505204 0.080192 0.592407\n", "\n" ] } ], "source": [ "A=rand(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve the linear system $Ax =b$." ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", "\n", " -0.50000\n", " 1.50000\n", " -0.50000\n", "\n" ] } ], "source": [ "A=[1 2 3; 3 3 4; 2 3 3];\n", "b=[1; 1; 2];\n", "x=A\\b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## more vectors" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", "\n", " 1 2 3 4 5\n", "\n" ] } ], "source": [ "x = 1:5" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", "\n", " Columns 1 through 8:\n", "\n", " 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000\n", "\n", " Column 9:\n", "\n", " 5.0000\n", "\n" ] } ], "source": [ "x = 1:0.5:5" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans = 9\r\n" ] } ], "source": [ "length(x)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x =\n", "\n", " Columns 1 through 8:\n", "\n", " 0.00000 0.50000 1.00000 1.50000 2.00000 2.50000 3.00000 3.50000\n", "\n", " Columns 9 through 11:\n", "\n", " 4.00000 4.50000 5.00000\n", "\n" ] } ], "source": [ "x = linspace(0,5,11)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "index =\n", "\n", " 6 7 8 9 10 11\n", "\n" ] } ], "source": [ "index = find(x>2)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000\n", "\n" ] } ], "source": [ "x(index)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## run a script file \n", "For example plottest.m is an script file. We can run plottest.m by simply write plottest" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "%plot gnuplot\n", "plottest" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Octave", "language": "octave", "name": "octave" }, "language_info": { "file_extension": ".m", "help_links": [ { "text": "GNU Octave", "url": "https://www.gnu.org/software/octave/support.html" }, { "text": "Octave Kernel", "url": "https://github.com/Calysto/octave_kernel" }, { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-octave", "name": "octave", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }