help diary
 DIARY Save text of MATLAB session.
    DIARY FILENAME causes a copy of all subsequent command window input
    and most of the resulting command window output to be appended to the
    named file.  If no file is specified, the file 'diary' is used.
 
    DIARY OFF suspends it. 
    DIARY ON turns it back on.
    DIARY, by itself, toggles the diary state.
 
    Use the functional form of DIARY, such as DIARY('file'),
    when the file name is stored in a string.
 
    See also <a href="matlab:help save">save</a>.


    Reference page in Help browser
       <a href="matlab:doc diary">doc diary</a>

a=5  %come inserire una variabile

a =

     5

a=5;
b=[1 2 3] %vettore riga

b =

     1     2     3

b=[1;2;3]  %vettore colonna

b =

     1
     2
     3

b'   %vettore trasposto

ans =

     1     2     3

C=[1 2 3; 4 5 6]  %matrice

C =

     1     2     3
     4     5     6

C2=[1 2 3 ...
4 5 6]

C2 =

     1     2     3     4     5     6

C2=[1 2 3; ...
4 5 6]

C2 =

     1     2     3
     4     5     6

C3=C2'  %matrice trasposta

C3 =

     1     4
     2     5
     3     6

b

b =

     1
     2
     3

d=[1 2 3 4 5 6 7 8]

d =

     1     2     3     4     5     6     7     8

d1=1:8   %vettore
 
d1 =

     1     2     3     4     5     6     7     8

d2=1:2:8

d2 =

     1     3     5     7

d3=1:0.5:8

d3 =

  Columns 1 through 9

    1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000    4.5000    5.0000

  Columns 10 through 15

    5.5000    6.0000    6.5000    7.0000    7.5000    8.0000

diary off
F=[0 0 0; 0 0 0]

F =

     0     0     0
     0     0     0

F1=zeros(2,3) %matrice di zeri

F1 =

     0     0     0
     0     0     0

G=[1 1; 1 1; 1 1]

G =

     1     1
     1     1
     1     1

G1=ones(3,2)  %matrice di 1

G1 =

     1     1
     1     1
     1     1

G2=ones(3)

G2 =

     1     1     1
     1     1     1
     1     1     1

H=eye(3) %matrice con 1 sulla diagonale

H =

     1     0     0
     0     1     0
     0     0     1

H1=eye(3,1)

H1 =

     1
     0
     0

H1=eye(3,4)

H1 =

     1     0     0     0
     0     1     0     0
     0     0     1     0

J=[1 2 3 4; 5 6 7 8; 9 0 1 2]

J =

     1     2     3     4
     5     6     7     8
     9     0     1     2

K1=J(:,1) %estrazione di una colonna da una matrice

K1 =

     1
     5
     9

K2=J(2,:) %estrazione di una riga da una matrice

K2 =

     5     6     7     8

J(2,3) %estrazione di un elemento da una matrice

ans =

     7

J

J =

     1     2     3     4
     5     6     7     8
     9     0     1     2

b

b =

     1
     2
     3

J1=[J,b] %matrice composta da matrice + vettore

J1 =

     1     2     3     4     1
     5     6     7     8     2
     9     0     1     2     3

m=[3 4 5 6]

m =

     3     4     5     6

J2=[J;m]

J2 =

     1     2     3     4
     5     6     7     8
     9     0     1     2
     3     4     5     6

J

J =

     1     2     3     4
     5     6     7     8
     9     0     1     2

J'

ans =

     1     5     9
     2     6     0
     3     7     1
     4     8     2

H1

H1 =

     1     0     0     0
     0     1     0     0
     0     0     1     0

J+H1  %somma di matrici

ans =

     2     2     3     4
     5     7     7     8
     9     0     2     2

J'+2 %matrice + costante

ans =

     3     7    11
     4     8     2
     5     9     3
     6    10     4

C

C =

     1     2     3
     4     5     6

C*2 %matrice per costante

ans =

     2     4     6
     8    10    12

D=[1 0; 0 1; 1 1]

D =

     1     0
     0     1
     1     1

C*D  %prodotto di matrici

ans =

     4     5
    10    11

C*D'
??? Error using ==> mtimes
Inner matrix dimensions must agree.

C.*D'

ans =

     1     0     3
     0     5     6

C^2
??? Error using ==> mpower
Matrix must be square.

N=[ 1 2; 0 3]

N =

     1     2
     0     3

N^2 %potenza di matrice

ans =

     1     8
     0     9

N.^2 %quadrato di matrice elemento per elemento

ans =

     1     4
     0     9

inv(N) %inversa di matrice

ans =

    1.0000   -0.6667
         0    0.3333


M=[0 2; 3 1]

M =

     0     2
     3     1

M*N

ans =

     0     6
     3     9

M*inv(N)  %divisione di matrici

ans =

         0    0.6667
    3.0000   -1.6667

M/N  %divisione di matrici

ans =

         0    0.6667
    3.0000   -1.6667

M./N  %divisione elemento per elemento

ans =

         0    1.0000
       Inf    0.3333

diary off
A=[1 2; 3 4; 0 1]

A =

     1     2
     3     4
     0     1

exp(A) %esponenziale di matrice (viene fatto elemento per elemento)

ans =

    2.7183    7.3891
   20.0855   54.5982
    1.0000    2.7183

B=[1 2; 3 4]

B =

     1     2
     3     4

expm(B)

ans =

   51.9690   74.7366
  112.1048  164.0738

>> x=1:5

x =

     1     2     3     4     5

>> x=linspace(-1,1,5)  %genera 5 punti linearmente spaziati tra -1 e 1 (compresi)

x =

   -1.0000   -0.5000         0    0.5000    1.0000

>> x=logspace(-1,1,5)  %genera 5 punti logaritmicamente spaziati tra 10^-1 e 10 (compresi)

x =

    0.1000    0.3162    1.0000    3.1623   10.0000




c=[1 2 3 0 7 5 8 2 1]

c =

     1     2     3     0     7     5     8     2     1

find(c>2)  %indica gli elementi del vettore che sono >2

ans =

     3     5     6     7


c

c =

     1     2     3     0     7     5     8     2     1

[i,j,v]=find(c) %indici di riga e colonna degli elementi >0 e elementi >0

i =

     1     1     1     1     1     1     1     1


j =

     1     2     3     5     6     7     8     9


v =

     1     2     3     7     5     8     2     1

[i,j,v]=find(c>5)

i =

     1     1


j =

     5     7


v =

     1     1

mean(c) %media di un vettore

ans =

    3.2222

std(c) %deviazione standard  di un vettore

ans =

    2.8186

diary off

B

B =

     1     2
     3     4

>> diag(B)  %estrae la diagonale di una matrice

ans =

     1
     4

>> diag(ans) %genera una matrice con diagonale ans

ans =

     1     0
     0     4

>> A=[1 4 7 10;2 5 8 11; 3 6 9 12]

A =

     1     4     7    10
     2     5     8    11
     3     6     9    12

>> reshape(A,2,6)  %trasforma la matrice A in una matrice 2x6

ans =

     1     3     5     7     9    11
     2     4     6     8    10    12




det(B) %determinante di una matrice

ans =

    -2

inv(B) %inversa di una matrice

ans =

   -2.0000    1.0000
    1.5000   -0.5000

rank(B) %rango di una matrice

ans =

     2

D=[1 0 2; 3 4 5; 2 0 4]

D =

     1     0     2
     3     4     5
     2     0     4

rank(D)

ans =

     2

det(D)

ans =

     0

inv(D)
Warning: Matrix is singular to working precision.

ans =

   Inf   Inf   Inf
   Inf   Inf   Inf
   Inf   Inf   Inf

>> mean(D)  %media di una matrice

ans =

    2.0000    1.3333    3.6667

>> std(D)  %SD di una matrice

ans =

    1.0000    2.3094    1.5275


help eig
 EIG    Eigenvalues and eigenvectors.
    E = EIG(X) is a vector containing the eigenvalues of a square 
    matrix X.
 
    [V,D] = EIG(X) produces a diagonal matrix D of eigenvalues and a
    full matrix V whose columns are the corresponding eigenvectors so
    that X*V = V*D.
 
    [V,D] = EIG(X,'nobalance') performs the computation with balancing
    disabled, which sometimes gives more accurate results for certain
    problems with unusual scaling. If X is symmetric, EIG(X,'nobalance')
    is ignored since X is already balanced.
 
    E = EIG(A,B) is a vector containing the generalized eigenvalues
    of square matrices A and B.
 
    [V,D] = EIG(A,B) produces a diagonal matrix D of generalized
    eigenvalues and a full matrix V whose columns are the
    corresponding eigenvectors so that A*V = B*V*D.
 
    EIG(A,B,'chol') is the same as EIG(A,B) for symmetric A and symmetric
    positive definite B.  It computes the generalized eigenvalues of A and B
    using the Cholesky factorization of B.
    EIG(A,B,'qz') ignores the symmetry of A and B and uses the QZ algorithm.
    In general, the two algorithms return the same result, however using the
    QZ algorithm may be more stable for certain problems.
    The flag is ignored when A and B are not symmetric.
 
    See also <a href="matlab:help condeig">condeig</a>, <a href="matlab:help eigs">eigs</a>, <a href="matlab:help ordeig">ordeig</a>.

    Overloaded functions or methods (ones with the same name in other directories)
       <a href="matlab:help lti/eig.m">help lti/eig.m</a>
       <a href="matlab:help darray/eig.m">help darray/eig.m</a>
       <a href="matlab:help sym/eig.m">help sym/eig.m</a>

    Reference page in Help browser
       <a href="matlab:doc eig">doc eig</a>


A2=[1 2;0 3]

A2 =

     1     2
     0     3

[v,d]=eig(A2)  %autovettori e autovalori di una matrice

v =

    1.0000    0.7071
         0    0.7071


d =

     1     0
     0     3


[L,U]=lu(A2) %fattorizzazione LU di una matrice

L =

     1     0
     0     1


U =

     1     2
     0     3

A

A =

     1     2
     3     4
     0     1

B

B =

     1     2
     3     4

[L,U]=lu(B)

L =

    0.3333    1.0000
    1.0000         0


U =

    3.0000    4.0000
         0    0.6667

p=[1 -2 -4 8]

p =

     1    -2    -4     8

r=roots(p) %radici del polinomio x^3-2x^2-4x+8

r =

   -2.0000
    2.0000
    2.0000

poly(r) %polinomio le cui radici sono -2,2,2

ans =

    1.0000   -2.0000   -4.0000    8.0000

f=[1 2 3]

f =

     1     2     3

g=[4 5 6]

g =

     4     5     6

conv(f,g) %prodotto di due polinomi

ans =

     4    13    28    27    18

[q r]=deconv(ans,g) %divisione tra due polinomi

q =

     1     2     3


r =

     0     0     0     0     0

[q r]=deconv(f,g)

q =

    0.2500


r =

         0    0.7500    1.5000

A2

A2 =

     1     2
     0     3

poly(A2)  %polinomio caratteristico di A2

ans =

     1    -4     3

polyval(f,2) %valuto il polinomio f in 2

ans =

    11

A=[1 2 0; 0 4 2; 2 2 8]

A =

     1     2     0
     0     4     2
     2     2     8

b=ones(3,1)

b =

     1
     1
     1

% calcoliamo la soluzione del sistema Ax=b
x=inv(A)*b

x =

    0.4444
    0.2778
   -0.0556

x=A\b

x =

    0.4444
    0.2778
   -0.0556

%%%%%%%%%%%%%%%%%%%%%55

c=-2

c =

    -2

abs(c) %valore assoluto di c

ans =

     2

d=[1 -4 0 -3 2]

d =

     1    -4     0    -3     2

abs(d) %valore assoluto di un vettore

ans =

     1     4     0     3     2

E=[ -2 0 1; 4 -5 0]

E =

    -2     0     1
     4    -5     0

abs(E) %valore assoluto di una matrice

ans =

     2     0     1
     4     5     0

cos(c) %coseno

ans =

   -0.4161

cos(E)

ans =

   -0.4161    1.0000    0.5403
   -0.6536    0.2837    1.0000

g=[ 1.32  2.96 0.4 5.56]

g =

    1.3200    2.9600    0.4000    5.5600

fix(g) %parte intera inferiore

ans =

     1     2     0     5

round(g) %arrotondamento

ans =

     1     3     0     6

sign(g) %segno

ans =

     1     1     1     1

d

d =

     1    -4     0    -3     2

sign(d)

ans =

     1    -1     0    -1     1

min(d) %minimo di un vettore

ans =

    -4

max(d) %massimo di un vettore

ans =

     2

F=[0 2 -1; 4 0 1; -2 5 3]

F =

     0     2    -1
     4     0     1
    -2     5     3

max(F) %massimo di una matrice

ans =

     4     5     3

min(F) %minimo di una matrice

ans =

    -2     0    -1
 
sum(d)   %somma degli el. di un vettore

ans =

    -4

prod(d)  %prodotto degli el. di un vettore

ans =

     0

sum(F) %somma degli elementi di una matrice (x colonne)

ans =

     2     7     3

prod(F) %prodotto degli elementi di una matrice (x colonne)

ans =

     0     0    -3

rand %valore aleatorio distribuito uniforme

ans =

    0.8147

rand(2,3)

ans =

    0.9058    0.9134    0.0975
    0.1270    0.6324    0.2785

randn %generatore di numeri normali standard

ans =

   -0.6855

randn(4,3) %matrice di n. casuali normali

ans =

   -0.2684   -0.0410    0.3692
   -1.1883   -2.2476    0.1792
    0.2486   -0.5108   -0.0373
    0.1025    0.2492   -1.6033

[n,m]=size(ans) %dimensione di una matrice

n =

     4


m =

     3

sort(d) %ordina gli elementi di un vettore

ans =

    -4    -3     0     1     2

[y,i]=sort(d) %ordina gli elementi e gli indici corrispondenti

y =

    -4    -3     0     1     2


i =

     2     4     3     1     5

F

F =

     0     2    -1
     4     0     1
    -2     5     3

sort(F) %ordina gli elementi di una matrice

ans =

    -2     0    -1
     0     2     1
     4     5     3

[Y,I]=sort(F)

Y =

    -2     0    -1
     0     2     1
     4     5     3


I =

     3     2     1
     1     1     2
     2     3     3


%%%%%%%%% GRAFICA  %%%%%%%%%%%%%%%


x=0:0.1:10;
y=(x-5).^2;
plot(y)  %plotta y e sulla x mette i valori da 0 a 100
plot(x,y) %plotta la y corrispondentemente alla x
hold
Current plot held
z=(4-x).^2;
plot(x,z)
plot(x,y,x,z) %plotta due curve sullo stesso grafico
plot(x,y,'r',x,z,'g')
plot(x,y,'r--',x,z,'g:')
subplot(211);plot(x,y,'r--')
subplot(212);plot(x,x,'g:')
subplot(212);plot(x,z,'g:')
plot(x,y,'r--',x,z,'g:');
title('grafico') %titolo del grafico
xlabel('ascisse');ylabel('ordinate') %nomi assi x e y
axis([0 10 0 25]) %lunghezza assi coordinati
grid %inserisce la griglia
x=-2:0.1:2;y=-2:0.1:2;

z=x.*exp(-x.^2-y.^2);


x=-2:0.1:2;y=-2:0.1:2;
[x,y]=meshgrid(x,y); %tasforma il dominio specificato da x e y in vettori
                     %che permettono di valutare una funzione bidimens.
                     %le righe dell'output sono copie di x, le colonne 
                     %sono copie di y
z=x.*exp(-x.^2-y.^2);
mesh(z)  %prospettiva di un grafico a 3 dim.
mesh(x,y,z)
surf(x,y,z) %crea una superficie colorata
diary off
v=randn(1,100); %vettore di n. aleatori normali
size(v)

ans =

     1   100

hist(v)  %produce un istogramma dei dati con 10 celle
hist(v,5) %istogramma con 5 celle
hist(v,20) %istogramma con 20 celle
t=randn(1,500);
hist(t)
hist(t,20)
stairs(t) %grafico a scalini
stairs(v)
diary off
format short  %virgola fissa con 4 decimali
pi

ans =

    3.1416

format long %virgola fissa con 15 decimali
pi

ans =

   3.141592653589793

format short e %virgola mobile con 5 decimali
pi

ans =

  3.1416e+000

format long e  %virgola mobile con 15 decimali
pi 

ans =

    3.141592653589793e+000

format hex %formato esadecimale
pi

ans =

   400921fb54442d18

format short
F

F =

     0     2    -1
     4     0     1
    -2     5     3

clear F %cancella la matrice F
F
??? Undefined function or variable 'F'.

d

d =

     1    -4     0    -3     2

clear %cancella tutte le variabili
d
??? Undefined function or variable 'd'.

b
??? Undefined function or variable 'b'.

diary off

diary on
A=[1 2 3;4 5 6]

A =

     1     2     3
     4     5     6

b=[9 8 7]

b =

     9     8     7

c=0.5

c =

   0.500000000000000

f=pi

f =

   3.141592653589793

who  %dice che variabili sono in uso

Your variables are:

A  b  c  f  

whos %dice le variabili in uso e il tipo
  Name      Size            Bytes  Class     Attributes

  A         2x3                48  double              
  b         1x3                24  double              
  c         1x1                 8  double              
  f         1x1                 8  double              

diary off
save matrice A  %salvo la matrice in un file .mat
clear
load matrice %carico il file matrice.mat
whos
  Name      Size            Bytes  Class     Attributes

  A         2x3                48  double              

A  %richiamo A

A =

     1     2     3
     4     5     6

save matr A -ascii  %salvo A in un file ascii
clear
A
??? Undefined function or variable 'A'.

load matr %carico il file ascii matr
A
??? Undefined function or variable 'A'.

matr %nella varibile matr mette la matrice A

matr =

     1     2     3
     4     5     6

diary off
A
??? Undefined function or variable 'A'.

load matrice
A

A =

     1     2     3
     4     5     6

b=[9 8 7]

b =

     9     8     7

c=0.5

c =

    0.5000

save matrice %salvo tutte le variabili nel file matrice
clear
load matrice %carico il file matrice
whos
  Name      Size            Bytes  Class     Attributes

  A         2x3                48  double              
  b         1x3                24  double              
  c         1x1                 8  double              
  matr      2x3                48  double              

c

c =

    0.5000

diary off
