Lynda_Learning_Octave

2018-07-01  本文已影响0人  Percy大大

写在最前面
下定决心好好整理学习过的Lynda.com课程的笔记,在以前的学习过程中光是跟着视频学习,看似是学的很快,但是没有巩固,很快就忘记了。视频学习的一个弊病是不利于复习,而这恰恰是书本学习的强项,因为可以随心所欲的划重点,复习时可以剔除啰嗦的部分,只看重点。这也是我开始整理学习笔记的初衷,希望能一直坚持下去。

Lynda.com是全英文学习网站,所以所有笔记内容都是英文,而关于自己的心得体会还有思考,以及个别生涩单词,我会用中文在文中予以标注,便于自己反复查看。

0.Introduction

What You Should Know


1.Introducing Octave

Downloading Octave

Running Octave

octave:1> a = 3
a = 3
octave:2> b = a
b = 3
octave:3> PS1 <'>>'>;
>> exit  % close octave

Getting help

>> help help
>> help pi
>> doc % press Ctrl + C to leave DOC

Support/Help: https://www.gnu.org/software/octave/support.html
Online doc: https://octave.org/doc/interpreter/
PDF: https://octave.org/octave.pdf


2.Surveying Basic Octave Commands

01. Using build-in commands

>> pi
ans = 3.1416
>> format long
>> pi
ans = 3.14159265358979
>> format short
>> pi
ans = 3.1416
>> a = pi
a = 3.1416
>> b = pi;
>> disp(a)
  3.1416
>> c = rand()
c = 0.27080
>> disp(c)
  0.27080
>> randn()
ans = 0.50997
>> randn()
ans = 0.31921
>> randn()
ans = -0.34053
>>

02. Assigning value to variables

>> a = 3
a = 3
>> b = a
b = 3
>> a = pi
a = 3.1416
>> s = 'Product Code'
s = Product Code
>> st = 'Product"
error: unterminated string constant
parse error:

    syntax error

 >>> st = 'Product" 
                            ^

>> st = "Product"
st = Product
>> a = rand()
a = 0.41793
>> a = randn()
a = -0.12372
>> 

03. Introducing mathematical operators

>> 3 + 5
ans = 8
>> 5 - 3
ans = 2
>> 5 * 3
ans = 15
>> 5 / 3
ans = 1.6667
>> 5 ^ 3
ans = 125
>> 5 ** 4
ans = 625
>> mod(24, 5)
ans = 4
>> a = 3
a = 3
>> b = 2
b = 2
>> a == b
ans = 0
>> b = 3
b = 3
>> a == b
ans = 1
>> a ~= b
ans = 0
>> true && false
ans = 0
>> true && true
ans = 1
>> 1 && 0
ans = 0
>> 1 && 1
ans = 1
>> 1 || 0
ans = 1
>> 0 || 0
ans = 0
>> 1 || 1
ans = 1
>> xor(1, 0)
ans = 1
>> xor(1, 1)
ans = 0
>>

04. Calculating values using built-in functions and variables

>> sqrt(16)
ans = 4
>> sqrt(15)
ans = 3.8730
>> nthroot(81, 4)
ans = 3
>> fix(3.5)
ans = 3
>> ceil(6.001)
ans = 7
>> floor(6.999)
ans = 6
>> round(4.49)
ans = 4
>> round(4.5)
ans = 5
>> v = [1, 2, 3, 4]
v = 
     1   2   3   4
>> max(v)
ans = 4
>> min(v)
ans = 1
>> factorial(5)
ans = 120
>> primes(23)
ans = 
     2   3   5   7   11   13   17   19   23
>> list_primes(23)
ans = 
  Columns 1 through 15:
     2   3   5   7   11   13   17   19   23   29   31   37   41   43   47
  Columns 16 through 23:
     53   59   61   67   71   73   79   83
>>

05. Manipulating strings

>> s1 = "Spring "
s1 = Spring
>> s2 = "catalog"
s2 = catalog
>> strcat(s1, s2)
ans = Springcatalog
>> cstrcat(s1, s2)
ans = Spring catalog
>> i = 551
i = 551
>> strcat(s2, i)
warning: range error for conversion to character value
ans = catalog
>> num1 = num2str(i)
num1 = 551
>> strcat(s1, num1)
ans = Spring551
>> strcmp(s1, s2)
ans = 0
>> s3 = deblank(s1)
s3 = Spring
>>

06. Performing conditional steps using if statements

if (condition)
    statement
endif
% Created by Percy
% Script name is sdata
      
sales = 500
if (sales >= 500)
    printf ("Sales target reached. \n");

endif
>> sdata
sales = 500
Sales target reached.
>>
% Created by Percy
% Script name is sdata
      
sales = 400
if (sales >= 500)
    printf ("Sales target reached. \n");

endif
>> sdata
sales = 400
>>
if (condition)
    statements
else
    statements
endif
% Created by Percy
% Script name is sdata
      
sales = 400
if (sales >= 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 400
Sales target not reached.
>>
% Created by Percy
% Script name is sdata
      
sales = 500
if (sales >= 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 500
Sales target reached.
>>
if (condition)
    statements
elseif
    staements
endif
% Created by Percy
% Script name is sdata
      
sales = 500
if (sales >= 750)
    printf ("Bonus target reached. \n");
elseif (sales = 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 500
Sales target reached.
>>
% Created by Percy
% Script name is sdata
      
sales = 400
if (sales >= 750)
    printf ("Bonus target reached. \n");
elseif (sales = 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 400
Sales target not reached.
>>
% Created by Percy
% Script name is sdata
      
sales = 800
if (sales >= 750)
    printf ("Bonus target reached. \n");
elseif (sales = 500)
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> sdata
sales = 800
Bonus target reached.
>>

3.Manipulating Matrices

01. Defining vector向量 and matrices矩阵

>> M = [1 2 3; 4 5 6]
M =
      1    2    3
      4    5    6
>> rv = [1 2 3]
rv = 
      1    2    3
>> cv = [1; 2; 3]
cv = 
      1
      2
      3
>>

02. Adding, subtracting, and multiplying matrices

>> M = [1 2 3; 4 5 6]
M = 
      1    2    3
      4    5    6
>> N = [5 6; 7 8; 9 10]
N = 
      5    6
      7    8
      9    10
>> M * N
ans = 
      46    52
     109   124
>> N * M
ans = 
      29    40    51
      39    54    69
      49    68    87
>> M * 4
ans = 
      4    8    12
     16   20    24
>>

03. Generating useful matrices

>> ones(2, 3)
ans = 
      1    1    1
      1    1    1
>> zeros(5, 4) 
ans = 
      0    0    0    0
      0    0    0    0
      0    0    0    0
      0    0    0    0
      0    0    0    0
>> eye(3)
ans = 
Diagonal Matrix
      1    0    0
      0    1    0
      0    0    1
>> M = [1 2 3; 4 5 6; 7 8 9]
M = 
      1    2    3
      4    5    6
      7    8    9
>> M * eye(3)
ans = 
      1    2    3
      4    5    6
      7    8    9    
>> v = [1: 0.1: 2]
v = 
    Columns 1 through 8:
      1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000
    Columns 9 through 11:
      1.8000    1.9000    2.0000
>> W = rand(2, 3)
W = 
      0.89360    0.19536    0.34167
      0.75238    0.21295    0.81458
>> randn(3, 4)
ans = 
     -0.076539   -0.236123   -0.630989    0.119407
     -0.248545   -0.749690   -0.124957    0.616097
      1.002778   -0.255473   -1.626574   -0.173041
>>

04. Transposing转置 and inverting matrices

>> eye(2)
ans = 
Diagonal Matrix
      1    0
      0    1
>> B = [1 2; 3 4]
B = 
      1    2
      3    4
>> inv(B)
ans = 
     -2.00000    1.00000
      1.50000   -0.50000
>> B * inv(B)
ans = 
      1.00000    0.00000
      0.00000    1.00000
>> pinv(B)
ans = 
     -2.00000    1.00000
      1.50000   -0.50000
>> A = [1 2 3; 4 5 6; 7 8 9]
A = 
      1    2    3
      4    5    6
      7    8    9
>>

05. Performing element-wise calculations

> A = [10 15; 20 25]
A = 
      10    15
      20    25
>> A * 3
ans = 
      30    45
      60    75
>> B = [4 8; 9 18]
B = 
      4    8
      9   18
>> A * B
ans = 
      175    350
      305    610
>> A .* B
ans = 
       40    120
      180    450
>>

06. Referring to matrix rows and columns

>> v = [1: 0.1: 2]
v = 
    Columns 1 through 8:
      1.0000    1.1000    1.2000    1.3000    1.4000    1.5000    1.6000    1.7000
    Columns 9 through 11:
      1.8000    1.9000    2.0000
>> v(7)
ans = 1.6000
>>v(7:9)
ans = 
      1.6000    1.7000    1.8000
>> A = [1 5; 8 9]
A = 
      1    5
      8    9
>> A(2, 1)
ans = 8
>> A(2, :)
ans = 
      8    9
>> A(:, 1)
ans = 
      1
      8
>>

4.Managing Executable Octave Programs

01. Sending output to the screen

day = 2;
month = "August";
printf ("Signature validated on %s %d.\n Thank you!\n", month, day);
Signature validated on August 2.
Thank you!
>> day = 2
day = 2
>> month = "August"
month = August
>> printf ("Signature validated on %s %d.\n Thank you!\n", month, day)
Signature validated on August 2.
 Thank you!
>> s = sprint("%s %d", month, day)
s = August 2
>> disp(s)
August 2
>>

02. Sending output to a file

>> a = [1 2; 3 4; 5 6]
a = 
      1    2
      3    4
      5    6
>> save -ascii datafile.mat A
>> csvwrite("csvdata.mat", A)
>>

03. Using data stored in a external file

>> C = load("datafile.mat")
C = 
      1    2
      3    4
      5    6
>> D = csvread("csvdata.mat")
D = 
      1    2
      3    4
      5    6
>>

04. Defining a function

% create a file named displaypi.m

function displaypi
        disp(pi);
endfunction
>> displaypi
  3.1416
>>
% create a file named timespi.m

function timespi(x)
        disp(x * pi);
endfunction
>> timespi(10)
  31.416
>> timespi(8)
  25.133
>>
% create a file named cubed.m

function retval = cubed(x)
        retval = x ^ 3
endfunction
>> a = cubed(5)
retval = 125
a = 125
>>

05. Creating a executable script

% Percy, July 1, 2018

a = 3;
disp(a * 3)
>> firstscript
  9
>>

06. Adding comments to an Octave program

# Created July 1, 2018 by Percy

sales = 800

% $750 is new daily target

if (sales >= 750);
    printf ("Bonus target reached. \n");
elseif (sales = 500);      % Formerly top bonus target amount.
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif

07. Debugging your Octave code

% Testing if statements

sales = 800;

if (sales >= 500);
    printf ("Bonus target reached. \n");
elseif (sales >= 750);  
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> debugging
Bonus target reached.
>>
% Testing if statements

sales = 600;

if (sales >= 750);
    printf ("Bonus target reached. \n");
elseif (sales >= 500);  
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> debugging
Sales target reached.
% Testing if statements

sales = 600;

printf ("Press Enter to continue.")
pause;

if (sales >= 750);
    printf ("Bonus target reached. \n");
elseif (sales >= 500);  
    printf ("Sales target reached. \n");
else 
    printf ("Sales target not reached. \n");

endif
>> debugging
Sales = 600
Press Enter to continue.
Sales target reached.
>>

5.Plotting Data

01. Creating a simple plot

>> X = -5:0.1:5
% there will be 101 results
>>plot(X, sin(X))
>>plot(X, cos(X))
>>

02. Summarizing data using a histogram柱状图

>> X = [1, 3, 7, 14, 15, 16, 14]
X = 
      1    3    7    14    15    16    14
>> hist(X)
>> hist(X, 3)
>>

03. Creating a scatter分散 plot

>> X = [1, 3, 5, 7, 9]
X = 
      1    3    5    7    9
>> Y = [15, 4, 18, 3, 9]
Y = 
      15    4    18    3    9
>> scatter(X, Y)
>> scatter(X, Y, "red")
>> scatter(X, Y, "red", "filled")
>>

6.Conclusion

Further Resources

上一篇下一篇

猜你喜欢

热点阅读