Spis treści

Matlab

Środowisko graficzne

Matlab jako kalkulator

Operacje arytmetyczne + dodawnie, - odejmowanie, * mnożenie, / dzielenie, ^ potęgowanie

5 + 5
ans =  10
-2 * 4.3
ans = -8.6000
2 ^ 10
ans =  1024
3 / 5
ans =  0.60000
3 / 0 
warning: division by zero
ans =  Inf

Ćwiczenie: oblicz ile wynoisi $\frac{1.3^{2.2}}{5.3 - 3/13}$

Funkcje matematyczne

Stałe: pi $\pi$, inf $\infty$, NaN not a number, realmax największa liczba, rzeczywista, eps dokładność maszynowa

Funkcje: sin(), cos(), tan, cot, sinh, asin, sqrt() $\sqrt{x}$, exp() $e^x$, abs $|x|$, log, log2, log10, sign znak, mod reszta z dzielenia

sin( pi / 4 )
ans =  0.70711
realmax
eps
ans =   1.7977e+308
ans =    2.2204e-16
sqrt(16)
ans =  4
sqrt(-1)
ans =  0 + 1i

Ćwiczenie: oblicz ile wynosi $\sqrt{\sin(0.3\pi) \cdot e^{-0.1}} $

Wyjście poleceń

sin(5) + 3;
disp(sin(5) + 3);
disp('Witaj świecie')
 2.0411
Witaj świecie
fprintf('Witaj ściecie!\n')
fprintf('sin(%d) = %f\n', 2, sin(2))
Witaj ściecie!
sin(2) = 0.909297

Formatowanie printf:

fprintf('%f\n', pi)
fprintf('%e\n', pi)
fprintf('%g\n', pi)
fprintf('%.10f\n', pi)
fprintf('%10f\n', pi)
fprintf('%10.2f\n', pi)
fprintf('%-10.2f\n', pi)
fprintf('%d %x %c\n', 42, 42, 42)
fprintf('Liczba %s wynosi %f', 'pi', pi)
3.141593
3.141593e+00
3.14159
3.1415926536
  3.141593
      3.14
3.14      
42 2a *
Liczba pi wynosi 3.141593

Zmienne

x = 16 
y = sqrt(x)
z = x + y + sin(x) 
napis = 'Witaj Świecie'
x =  16
y =  4
z =  19.712
napis = Witaj Świecie
x = x + 1
x =  17

błędne przypisanie

1 = x
parse error:

  invalid constant left hand side of assignment

>>> 1 = x
      ^
who
Variables in the current scope:

ans    napis  x      y      z
whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        ans         1x1                          8  double
        napis       1x14                        14  char
        x           1x1                          8  double
        y           1x1                          8  double
        z           1x1                          8  double

Total is 18 elements using 46 bytes

Zmienna ans

sqrt(2)
ans =  1.4142
x = ans + 5
x =  6.4142

Typy danych

x = 1;
y = true;
z = int32(x);
d = 'abc';
whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  ===== 
        ans         1x1                          8  double
        d           1x3                          3  char
        napis       1x14                        14  char
        x           1x1                          8  double
        y           1x1                          1  logical
        z           1x1                          4  int32

Total is 21 elements using 38 bytes

Wektory

x = [ 1  2  3]
x =

   1   2   3
x = [ 1,  2,  3]
x =

   1   2   3
x = 1:10
x =

    1    2    3    4    5    6    7    8    9   10
y = [ 1 ; 2 ; 3 ; 4]
y =

   1
   2
   3
   4

Operacje na wektorach

x = [1 2 3 4 5]
x + 2
x =

   1   2   3   4   5

ans =

   3   4   5   6   7
sqrt(x)
ans =

   1.0000   1.4142   1.7321   2.0000   2.2361
y = [6 7 8 9 10]
z = x + y
sqrt(x)
y =

    6    7    8    9   10

z =

    7    9   11   13   15

ans =

   1.0000   1.4142   1.7321   2.0000   2.2361

Ćwiczenie: oblicz wartość wyrażenia $\begin{bmatrix} 1 & 2 & 3 \\ \end{bmatrix} \cdot \begin{bmatrix} 1 \\ 2\\ 3 \\ \end{bmatrix}$

Komendy wbudowane

help sin
'sin' is a built-in function from the file libinterp/corefcn/mappers.cc

 -- sin (X)
     Compute the sine for each element of X in radians.

     See also: asin, sind, sinh.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.

Pomoc

lookfor cosine
acos                Compute the inverse cosine in radians for each element of X
                    .
acosh               Compute the inverse hyperbolic cosine for each element of X
                    .
cos                 Compute the cosine for each element of X in radians.
cosh                Compute the hyperbolic cosine for each element of X.
acosd               Compute the inverse cosine in degrees for each element of X
                    .
cosd                Compute the cosine for each element of X in degrees.
cosint              Compute the cosine integral function:

Ćwiczenie: zobacz pomoc dla polecenia format. Porównaj wyniki wyświetlania liczby $\pi$ w róznych formatach, np.: short, long, rat, bin

Wykresy

x = -pi:pi;
plot(x, sin(x));

 png

plot(x, cos(x), 'o')

 png

Inne przydatne komendy

date
ans = 03-Nov-2020

Ćwiczenie: wykonaj nastepujące kroki:

  1. usuń wszystkie zmienne i wyczyść wiersz poleceń
  2. utwóż zmienne $x=\pi$ i $y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]$
  3. zapisz zmienne w pliku o nazwie dane
  4. usuń zmienne $x$ i $y$
  5. wczytaj zmienne z pliku dane i wypisz ich wartości

Skrypty

Tryby pracy Matlab:

Przykład: uruchomienie pliku hello.m

hello
Witaj Świecie
type hello
% To jest pierwszy progeam w Matlab

tekst = 'Witaj Świecie';

disp(tekst)
help hello
 To jest pierwszy progeam w Matlab


Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at https://www.octave.org and via the help@octave.org
mailing list.

Zadanie

Napisz skrypt o nazwie zad1.m, który wykona nastepujące instrukcje: