Octave/Matlab-kod från föreläsning 1

OH-bilderna finns här, den Octave-kod jag skrev var:


GNU Octave, version 3.6.2
Copyright (C) 2012 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  For details, type `warranty'.

Octave was configured for "x86_64-pc-linux-gnu".

Additional information about Octave is available at http://www.octave.org.

Please contribute if you find this software useful.
For more information, visit http://www.octave.org/help-wanted.html

Read http://www.octave.org/bugs.html to learn how to submit bug reports.

For information about changes from previous versions, type `news'.

octave> a = 1
a =  1
octave> a = 1;
octave> a
a =  1
octave> b = 1 + 2 * 3
b =  7
octave> x = 1 / 3
x =  0.33333
octave> format long
octave> x
x =  0.333333333333333
octave> format short
octave> x
x =  0.33333
octave> format rat
octave> x
x = 1/3
octave> pi
ans = 355/113
octave> format short
octave> pi
ans =  3.1416
octave> sqrt(-2)
ans =  0.00000 + 1.41421i
octave> ans * ans
ans = -2.0000
octave> i * i
ans = -1
octave> i = 1
i =  1
octave> i * i
ans =  1
octave> j * j
ans = -1
octave> who
Variables in the current scope:

a    ans  b    i    x

octave> clear i
octave> who
Variables in the current scope:

a    ans  b    x

octave> i
ans =  0 + 1i
octave> A = [1 2
> 3 4]
A =

   1   2
   3   4

octave> B = [2, 3; 4, 5];
octave> B
B =

   2   3
   4   5

octave> B = [2, 3; 4 5];
octave> B
B =

   2   3
   4   5

octave> A + B
ans =

   3   5
   7   9

octave> A * B
ans =

   10   13
   22   29

octave> 4 * A
ans =

    4    8
   12   16

octave> 4 + A
ans =

   5   6
   7   8

octave> ones(2, 2)
ans =

   1   1
   1   1

octave> 4 * ones(2, 2) + A
ans =

   5   6
   7   8

octave> ones(2, 3)
ans =

   1   1   1
   1   1   1

octave> ones(2, 3) + ones(2, 2)
error: operator +: nonconformant arguments (op1 is 2x3, op2 is 2x2)
octave> size(A)
ans =

   2   2

octave> 4 * ones(size(A)) + A
ans =

   5   6
   7   8

octave> A = [1 2 3; 4 5 6]
A =

   1   2   3
   4   5   6

octave> zeros(2, 4)
ans =

   0   0   0   0
   0   0   0   0

octave> eye(3)
ans =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

octave> A = [i * eye(3)   4*ones(3,2)]
A =

   0 + 1i   0 + 0i   0 + 0i   4 + 0i   4 + 0i
   0 + 0i   0 + 1i   0 + 0i   4 + 0i   4 + 0i
   0 + 0i   0 + 0i   0 + 1i   4 + 0i   4 + 0i

octave> format long
octave> ans
ans =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

octave> A
A =

   0 + 1i   0 + 0i   0 + 0i   4 + 0i   4 + 0i
   0 + 0i   0 + 1i   0 + 0i   4 + 0i   4 + 0i
   0 + 0i   0 + 0i   0 + 1i   4 + 0i   4 + 0i

octave> format short
octave> A 
A =

   0 + 1i   0 + 0i   0 + 0i   4 + 0i   4 + 0i
   0 + 0i   0 + 1i   0 + 0i   4 + 0i   4 + 0i
   0 + 0i   0 + 0i   0 + 1i   4 + 0i   4 + 0i

octave> A = [1 2; 3 4]
A =

   1   2
   3   4

octave> A ^ -1
ans =

  -2.00000   1.00000
   1.50000  -0.50000

octave> inv(A)
ans =

  -2.00000   1.00000
   1.50000  -0.50000

octave> A * inv(A)
ans =

   1.00000   0.00000
   0.00000   1.00000

octave> a = [1 2 3]
a =

   1   2   3

octave> a * a
error: operator *: nonconformant arguments (op1 is 1x3, op2 is 1x3)
octave> a .'
ans =

   1
   2
   3

octave> a '
ans =

   1
   2
   3

octave> c = [1 i 2]
c =

   1 + 0i   0 + 1i   2 + 0i

octave> c '
ans =

   1 - 0i
   0 - 1i
   2 - 0i

octave> c .'
ans =

   1 + 0i
   0 + 1i
   2 + 0i

octave> a * a .'
ans =  14
octave> a .' * a
ans =

   1   2   3
   2   4   6
   3   6   9

octave> a + a
ans =

   2   4   6

octave> x = 1:1:10
x =

    1    2    3    4    5    6    7    8    9   10

octave> x = 1:10
x =

    1    2    3    4    5    6    7    8    9   10

octave> t = 0:0.1:1
t =

 Columns 1 through 7:

    0.00000    0.10000    0.20000    0.30000    0.40000    0.50000    0.60000

 Columns 8 through 11:

    0.70000    0.80000    0.90000    1.00000

octave> t = 0:0.1:1 .'
t =

 Columns 1 through 7:

    0.00000    0.10000    0.20000    0.30000    0.40000    0.50000    0.60000

 Columns 8 through 11:

    0.70000    0.80000    0.90000    1.00000

octave> t = (0:0.1:1) .'
t =

   0.00000
   0.10000
   0.20000
   0.30000
   0.40000
   0.50000
   0.60000
   0.70000
   0.80000
   0.90000
   1.00000

octave> [t t]
ans =

   0.00000   0.00000
   0.10000   0.10000
   0.20000   0.20000
   0.30000   0.30000
   0.40000   0.40000
   0.50000   0.50000
   0.60000   0.60000
   0.70000   0.70000
   0.80000   0.80000
   0.90000   0.90000
   1.00000   1.00000

octave> [t    t * t]
error: operator *: nonconformant arguments (op1 is 11x1, op2 is 11x1)
octave> [t    t + t]
ans =

   0.00000   0.00000
   0.10000   0.20000
   0.20000   0.40000
   0.30000   0.60000
   0.40000   0.80000
   0.50000   1.00000
   0.60000   1.20000
   0.70000   1.40000
   0.80000   1.60000
   0.90000   1.80000
   1.00000   2.00000

octave> [t    t .* t]
ans =

   0.00000   0.00000
   0.10000   0.01000
   0.20000   0.04000
   0.30000   0.09000
   0.40000   0.16000
   0.50000   0.25000
   0.60000   0.36000
   0.70000   0.49000
   0.80000   0.64000
   0.90000   0.81000
   1.00000   1.00000

octave> [t   sqrt(t)   t .* t]
ans =

   0.00000   0.00000   0.00000
   0.10000   0.31623   0.01000
   0.20000   0.44721   0.04000
   0.30000   0.54772   0.09000
   0.40000   0.63246   0.16000
   0.50000   0.70711   0.25000
   0.60000   0.77460   0.36000
   0.70000   0.83666   0.49000
   0.80000   0.89443   0.64000
   0.90000   0.94868   0.81000
   1.00000   1.00000   1.00000

octave> [t   sqrt(t)   t ^ 2]
error: for A^b, A must be a square matrix
octave> [t   sqrt(t)   t .^ 2]
ans =

   0.00000   0.00000   0.00000
   0.10000   0.31623   0.01000
   0.20000   0.44721   0.04000
   0.30000   0.54772   0.09000
   0.40000   0.63246   0.16000
   0.50000   0.70711   0.25000
   0.60000   0.77460   0.36000
   0.70000   0.83666   0.49000
   0.80000   0.89443   0.64000
   0.90000   0.94868   0.81000
   1.00000   1.00000   1.00000

octave> [t   sqrt(t)   t .^ 2   1 ./ t]
ans =

    0.00000    0.00000    0.00000        Inf
    0.10000    0.31623    0.01000   10.00000
    0.20000    0.44721    0.04000    5.00000
    0.30000    0.54772    0.09000    3.33333
    0.40000    0.63246    0.16000    2.50000
    0.50000    0.70711    0.25000    2.00000
    0.60000    0.77460    0.36000    1.66667
    0.70000    0.83666    0.49000    1.42857
    0.80000    0.89443    0.64000    1.25000
    0.90000    0.94868    0.81000    1.11111
    1.00000    1.00000    1.00000    1.00000
octave> c = [1 2 3]
c =

   1   2   3

octave> 1 ./ c
ans =

   1.00000   0.50000   0.33333

octave> 1 * ones(size(c)) ./ c
ans =

   1.00000   0.50000   0.33333

octave> x = 1:5
x =

   1   2   3   4   5

octave> exp(x)
ans =

     2.7183     7.3891    20.0855    54.5982   148.4132

octave> e^x
error: for x^A, A must be a square matrix
octave> e.^x
ans =

     2.7183     7.3891    20.0855    54.5982   148.4132

octave> x .^ 2 .* sqrt(x)
ans =

    1.0000    5.6569   15.5885   32.0000   55.9017

octave> A = [1 2; 3 4]
A =

   1   2
   3   4

octave> A * A ^ -1
ans =

   1.00000   0.00000
   0.00000   1.00000

octave> sqrt(A) * sqrt(A)
ans =

   3.4495   4.2426
   5.1962   6.4495

octave> A ^ (1/2)
ans =

   0.55369 + 0.46439i   0.80696 - 0.21243i
   1.21044 - 0.31864i   1.76413 + 0.14575i

octave> sqrt(A)
ans =

   1.0000   1.4142
   1.7321   2.0000

octave> B = A ^ (1/2)
B =

   0.55369 + 0.46439i   0.80696 - 0.21243i
   1.21044 - 0.31864i   1.76413 + 0.14575i

octave> B * B
ans =

   1.00000 + 0.00000i   2.00000 + 0.00000i
   3.00000 + 0.00000i   4.00000 + 0.00000i

octave> [t   sqrt(t)]
ans =

   0.00000   0.00000
   0.10000   0.31623
   0.20000   0.44721
   0.30000   0.54772
   0.40000   0.63246
   0.50000   0.70711
   0.60000   0.77460
   0.70000   0.83666
   0.80000   0.89443
   0.90000   0.94868
   1.00000   1.00000

octave> sqrt(A) ^ 2
ans =

   3.4495   4.2426
   5.1962   6.4495

octave> sqrt(A) .^ 2
ans =

   1.0000   2.0000
   3.0000   4.0000

octave> x = 1:10
x =

    1    2    3    4    5    6    7    8    9   10

octave> x .^ 2 - 2 * x - 2
ans =

   -3   -2    1    6   13   22   33   46   61   78

octave> x .^ 2 - 2 .* x - 2
ans =

   -3   -2    1    6   13   22   33   46   61   78

octave> 2 x
parse error:

  syntax error

>>> 2 x    %% vi måste skriva ut *
      ^

octave> 2i     %% utom då vi multiplicerar med i
ans =  0 + 2i
octave> x = 0:0.1:1
x =

 Columns 1 through 7:

    0.00000    0.10000    0.20000    0.30000    0.40000    0.50000    0.60000

 Columns 8 through 11:

    0.70000    0.80000    0.90000    1.00000

octave> n = 10
n =  10
octave> linspace(0, 1, n)
ans =

 Columns 1 through 8:

   0.00000   0.11111   0.22222   0.33333   0.44444   0.55556   0.66667   0.77778

 Columns 9 and 10:

   0.88889   1.00000

octave> x = linspace(0, 1);
octave> size(x)
ans =

     1   100

octave> f = @(x) x .* sqrt(x)
f =

@(x) x .* sqrt (x)

octave> f(1)
ans =  1
octave> f(2)
ans =  2.8284
octave> f = @(x) x / sqrt(1 + x^2);
octave> f(0)
ans = 0
octave> f(1)
ans =  0.70711
octave> t = (0:0.1:1) .'
t =

   0.00000
   0.10000
   0.20000
   0.30000
   0.40000
   0.50000
   0.60000
   0.70000
   0.80000
   0.90000
   1.00000

octave> [t   f(t)]
error: for A^b, A must be a square matrix
error: evaluating argument list element number 1
error: called from:
error:    at line -1, column -1
octave> f = @(x) x ./ sqrt(1 + x .^ 2);
octave> [t   f(t)]
ans =

   0.00000   0.00000
   0.10000   0.09950
   0.20000   0.19612
   0.30000   0.28735
   0.40000   0.37139
   0.50000   0.44721
   0.60000   0.51450
   0.70000   0.57346
   0.80000   0.62470
   0.90000   0.66896
   1.00000   0.70711

octave> plot(t, f(t))
octave> plot(t, f(t), 'o')
octave> plot(t, f(t), 'ro')
octave> plot(t, f(t), 'gx')
octave> t = 0:0.2:1;
octave> plot(t, f(t))
octave> t = linspace(0, 1);
octave> help plot
`plot' is a function from the file /usr/share/octave/3.6.2/m/plot/plot.m

 -- Function File:  plot (Y)
 -- Function File:  plot (X, Y)
 -- Function File:  plot (X, Y, PROPERTY, VALUE, ...)
 -- Function File:  plot (X, Y, FMT)
 -- Function File:  plot (H, ...)
 -- Function File: H = plot (...)
     Produce two-dimensional plots.

     Many different combinations of arguments are possible.  The
     simplest form is

          plot (Y)

     where the argument is taken as the set of Y coordinates and the X
     coordinates are taken to be the indices of the elements starting
     with 1.

     To save a plot, in one of several image formats such as PostScript
     or PNG, use the `print' command.

     If more than one argument is given, they are interpreted as

          plot (Y, PROPERTY, VALUE, ...)

     or

          plot (X, Y, PROPERTY, VALUE, ...)

     or

          plot (X, Y, FMT, ...)

     and so on.  Any number of argument sets may appear.  The X and Y
     values are interpreted as follows:

        * If a single data argument is supplied, it is taken as the set
          of Y coordinates and the X coordinates are taken to be the
          indices of the elements, starting with 1.

        * If the X is a vector and Y is a matrix, then the columns (or
          rows) of Y are plotted versus X.  (using whichever
          combination matches, with columns tried first.)

        * If the X is a matrix and Y is a vector, Y is plotted versus
          the columns (or rows) of X.  (using whichever combination
          matches, with columns tried first.)

        * If both arguments are vectors, the elements of Y are plotted
          versus the elements of X.

        * If both arguments are matrices, the columns of Y are plotted
          versus the columns of X.  In this case, both matrices must
          have the same number of rows and columns and no attempt is
          made to transpose the arguments to make the number of rows
          match.

          If both arguments are scalars, a single point is plotted.

     Multiple property-value pairs may be specified, but they must
     appear in pairs.  These arguments are applied to the lines drawn by
     `plot'.

     If the FMT argument is supplied, it is interpreted as follows.  If
     FMT is missing, the default gnuplot line style is assumed.

    `-'
          Set lines plot style (default).

    `.'
          Set dots plot style.

    `N'
          Interpreted as the plot color if N is an integer in the range
          1 to 6.

    `NM'
          If NM is a two digit integer and M is an integer in the range
          1 to 6, M is interpreted as the point style.  This is only
          valid in combination with the `@' or `-@' specifiers.

    `C'
          If C is one of `"k"' (black), `"r"' (red), `"g"' (green),
          `"b"' (blue), `"m"' (magenta), `"c"' (cyan), or `"w"'
          (white), it is interpreted as the line plot color.

    `";title;"'
          Here `"title"' is the label for the key.

    `+'
    `*'
    `o'
    `x'
    `^'
          Used in combination with the points or linespoints styles,
          set the point style.

    `@'
          Select the next unused point style.

     The FMT argument may also be used to assign key titles.  To do so,
     include the desired title between semi-colons after the formatting
     sequence described above, e.g., "+3;Key Title;" Note that the last
     semi-colon is required and will generate an error if it is left
     out.

     Here are some plot examples:

          plot (x, y, "@12", x, y2, x, y3, "4", x, y4, "+")

     This command will plot `y' with points of type 2 (displayed as
     `+') and color 1 (red), `y2' with lines, `y3' with lines of color
     4 (magenta) and `y4' with points displayed as `+'.

          plot (b, "*", "markersize", 3)

     This command will plot the data in the variable `b', with points
     displayed as `*' with a marker size of 3.

          t = 0:0.1:6.3;
          plot (t, cos(t), "-;cos(t);", t, sin(t), "+3;sin(t);");

     This will plot the cosine and sine functions and label them
     accordingly in the key.

     If the first argument is an axis handle, then plot into these axes,
     rather than the current axis handle returned by `gca'.

     The optional return value H is a graphics handle to the created
     plot.

     See also: semilogx, semilogy, loglog, polar, mesh, contour, bar,
     stairs, errorbar, xlabel, ylabel, title, print



Additional help for built-in functions and operators is
available in the on-line version of the manual.  Use the command
`doc ' to search the manual index.
5
Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.
octave> y2 = @(x) exp(-x / 4) .* sin(2 * pi * x);
octave> x = linspace(-5, 5);
octave> plot(x, y2(x))
octave> grid on
octave> y3 = @(x) exp(-x .^ 2)
ly3 =

@(x) exp (-x .^ 2)

octave> plot(x, y3(x))
octave> hold on
octave> plot(x, y2(x))
octave> y5 = @(x) 1 ./ (x-1) + 2 * x ./ (x + 1);
octave> plot(x, y5(x))
octave> clf
octave> plot(x, y5(x))
octave> axes([-5 5 -10 10])
error: axes: expecting argument to be a scalar axes handle
error: called from:
error:   /usr/share/octave/3.6.2/m/plot/axes.m at line 54, column 7
octave> axis([-5 5 -10 10])
octave> grid on
octave> pwd
ans = /home/public
octave> cd pt/spring/matlab
octave> ls
f1
octave> cd f1
octave> ls
matlab1.pdf
octave> 
octave> ls
matlab1.pdf  plots.m
octave> plots
octave> plots
y =  0.23693
octave> plots
octave> 1 > 0
ans =  1
octave> 0 > 1
ans = 0
octave> if 3
>   a = 123
> end
a =  123
octave> printTable
   0.00000:    0.00000
   0.10000:    0.31623
   0.20000:    0.44721
   0.30000:    0.54772
   0.40000:    0.63246
   0.50000:    0.70711
   0.60000:    0.77460
   0.70000:    0.83666
   0.80000:    0.89443
   0.90000:    0.94868
   1.00000:    1.00000
octave> x = (0:0.1:1).';
octave> [x sqrt(x)]
ans =

   0.00000   0.00000
   0.10000   0.31623
   0.20000   0.44721
   0.30000   0.54772
   0.40000   0.63246
   0.50000   0.70711
   0.60000   0.77460
   0.70000   0.83666
   0.80000   0.89443
   0.90000   0.94868
   1.00000   1.00000

octave> diary on
octave>