Tema 2

Descargar como pdf o txt
Descargar como pdf o txt
Está en la página 1de 17

UTILIDADES DEL MODELO LINEAL:

1. Verificar la existencia de la relación lineal.

2. Estimar (contrastar) la (una) relación lineal concreta (estructural).- supone actuar sobre los
coeficientes de la relación lineal.

3. Predecir la variable y en función de x o (x1x2 ... xm )

MODELO LINEAL SIMPLE:

1. Consideraremos inicialmente una sola x

2. Es decir, a partir del conjunto de datos


(xi , yi ) i = 1,..., n
, buscamos la función
yˆ = ax + b

que mejor aproxime a estos datos.


3. O sea, cuánto valen ¿a? ¿b?
Para calcular el valor de a y b debemos calcular primero el error, que se define de la siguiente
manera:

1. Definir el error en cada punto

2. Definir un criterio de error:


n n
E (a, b) =  ei =  ( yi − (axi + b)) 2
2

i =i i =i

Nota: Se pueden utilizar otros criterios de error.

3. Derivar respecto de a y de b, e igualar a 0.

𝑛 𝑛 𝑛

∑(𝑎, 𝑏) = ∑ 𝑒𝑖 = ∑(𝑦𝑖 − (𝑎𝑥𝑖 + 𝑏)2 ) = 0


2

𝑖=1 𝑖=1 𝑖=1

𝐴𝑝𝑙𝑖𝑐𝑎𝑚𝑜𝑠 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑜𝑠 𝑛𝑜𝑡𝑎𝑏𝑙𝑒𝑠,

∑(𝑦𝑖 2 − 2𝑦𝑖 (𝑎𝑥𝑖 + 𝑏) + (𝑎𝑥𝑖 + 𝑏)) = 0


𝑖=1

∑(𝑦𝑖 2 − 2𝑦𝑖 (𝑎𝑥𝑖 + 𝑏) + (𝑎2 𝑥𝑖 2 + 2𝑎𝑥𝑖 + 𝑏 2 )) = 0


𝑖=1

𝐴ℎ𝑜𝑟𝑎 𝑎𝑝𝑙𝑖𝑐𝑎𝑚𝑜𝑠 𝑙𝑎 𝑑𝑒𝑟𝑖𝑣𝑎𝑑𝑎 𝑝𝑎𝑟𝑐𝑖𝑎𝑙 𝑑𝑒 𝑎 𝑦 𝑑𝑒 𝑏 𝑝𝑎𝑟𝑎 𝑝𝑜𝑑𝑒𝑟 𝑜𝑏𝑡𝑒𝑛𝑒𝑟 𝑎𝑚𝑏𝑜𝑠 𝑝𝑎𝑟á𝑚𝑒𝑡𝑟𝑜𝑠.

E (a, b ) 
= 0
a 

E (a, b )
= 0
b 
𝑛 𝑛
𝜕𝐸(𝑎, 𝑏)
⟹ = ∑ −2𝑦𝑖 𝑥𝑖 + 2𝑎𝑥𝑖 2 + 2𝑥𝑖 𝑏 = 0 → 2 ∗ ∑ −𝑦𝑖 𝑥𝑖 + 𝑎𝑥𝑖 2 + 𝑥𝑖 𝑏 = 0
𝜕𝑎
𝑖=1 𝑖=1

𝑛 𝑛 𝑛
2 2
→ ∑ −𝑦𝑖 𝑥𝑖 + 𝑎𝑥𝑖 + 𝑥𝑖 𝑏 = 0 → 𝑅𝑒𝑜𝑟𝑑𝑒𝑛𝑎𝑚𝑜𝑠 𝑦 ∑ 𝑎𝑥𝑖 + 𝑥𝑖 𝑏 = ∑ 𝑦𝑖 𝑥𝑖
𝑖=1 𝑖=1 𝑖=1

𝑛 𝑛 𝑛
𝜕𝐸(𝑎, 𝑏)
⟹ = ∑ −2𝑦𝑖 + 2𝑎𝑥𝑖 + 2𝑏 = 0 → 2 ∗ ∑ −𝑦𝑖 + 𝑎𝑥𝑖 + 𝑏 = 0 → ∑ −𝑦𝑖 + 𝑎𝑥𝑖 + 𝑏 = 0
𝜕𝑏
𝑖=1 𝑖=1 𝑖=1
𝑛 𝑛

→ 𝑅𝑒𝑜𝑟𝑑𝑒𝑛𝑎𝑚𝑜𝑠 𝑦 ∑ 𝑎𝑥𝑖 + 𝑏 = ∑ −𝑦𝑖


𝑖=1 𝑖=1

Una 𝑣𝑒𝑧 𝑜𝑏𝑡𝑒𝑛𝑖𝑑𝑎𝑠 𝑙𝑎𝑠 𝑑𝑒𝑟𝑖𝑣𝑎𝑑𝑎𝑠, 𝑙𝑜 𝑝𝑎𝑠𝑎𝑚𝑜𝑠 𝑎 𝑓𝑜𝑟𝑚𝑎 𝑚𝑎𝑡𝑟𝑖𝑐𝑖𝑎𝑙 𝑝𝑎𝑟𝑎 𝑝𝑜𝑑𝑒𝑟 𝑡𝑟𝑎𝑏𝑎𝑗𝑎𝑟 𝑚𝑒𝑗𝑜𝑟
𝑐𝑜𝑛 𝑙𝑎𝑠 𝑒𝑐𝑢𝑎𝑐𝑖𝑜𝑛𝑒𝑠.

a b

𝑛 𝑛 𝑛

Primera ecuación ∑ 𝑥𝑖2 ∑ 𝑥𝑖 ∑ 𝑥𝑖


𝑖=1 𝑖=1 𝑎 𝑖=1
𝑛 ∗( )= 𝑛
𝑏
Segunda ecuación ∑ 𝑥𝑖 𝑛 ∑ −𝑦𝑖
( 𝑖=1 ) ( 𝑖=1 )

𝑨 𝒔𝒐𝒍 𝒕

𝑨 ∗ 𝒔𝒐𝒍 = 𝒕 → 𝑨−𝟏 𝑨 ∗ 𝒔𝒐𝒍 = 𝑨−𝟏 𝒕 → 𝒔𝒐𝒍 = 𝑨−𝟏 𝒕


Otros modelos:

Tomando logaritmos:
y = Ae Bx ln ( y ) = Bx + ln ( A)
Y hacemos el cambio de variables:
yˆ = ln ( y ) C = ln ( A)

yˆ = Bx + C
Tomando logaritmos:
y = C  xA ln(𝑦) = ln(𝐶) + 𝐴 ∗ ln (𝑥)

Y hacemos el cambio de variables:

𝐲̂ = 𝐀𝐱̂ + 𝐁

Tomando logaritmos:
y = C  x  e Ax ln(𝑦) = ln(𝐶) + ln(𝑥) + 𝐴𝑥 ∗ ln (𝑒)
1

ln(𝑦) − ln (𝑥) = 𝐴𝑥 + ln(𝐶)

𝑦
ln ( ) = 𝐴𝑥 + ln(𝐶)
𝑥

𝐲̂ = 𝐀𝐱 + 𝐁
Cambio de variables:
𝑦
𝑦 = ln (𝑥 ); 𝐵 = ln(𝐶) → 𝐶 = 𝑒 𝐵
Dado los siguientes datos (x = peso e y = pulso del corazón);

data = [90 62
86 45
67 40
89 55
81 64
75 53];
x = data(:,1);
y = data(:,2);
Sx2 = sum(x.*x);
Sx = sum(x);
Sxy = sum(x.*y);
Sy = sum(y);

A = [Sx2 Sx
Sx length(y)];

t = [Sxy
Sy];

coefs = inv(A)*t;

a = coefs(1); %a = 0.5947
b = coefs(2); %b = 4.7990

xi = [66 91];
yi = a*xi + b;

plot(x,y,'o');
hold on;
plot(xi, yi);
hold off;

Predice el pulso de una persona de 70kg,


persona_kg = 70;
y_persona = a*persona_kg + b;
Compute this fitting
A = [x.^2 x ones(length(x),1)];
sol = pinv(A)*y;
%sol = pinv(A) * y;
xi = 65:1:90;
estimacion = xi.^2*sol(1) + xi*sol(2) + sol(3);
plot(x,y,'o');
hold on;
plot(xi,estimacion);
hold off;
Compute this fitting

A = [x.^3 x.^2 x ones(length(x),1)];


sol = pinv(A)*y;
%sol = pinv(A) * y;
xi = 65:1:90;
estimacion = xi.^3*sol(1) + xi.^2*sol(2) + xi*sol(3) + sol(4);
plot(x,y,'o');
hold on;
plot(xi,estimacion);
hold off;

Calculate the operation to obtain a new vector and compute a new fitting

yp = 1./(y.*y);
A = [x ones(length(x),1)];
y2 = log(yp);
sol = pinv(A)*y2;
a = exp(sol(2));
b = sol(1);
xi = 65:1:90;
estimacion = a*exp(b*xi);
plot(x,yp,'o');
hold on;
plot(xi,estimacion);
hold off;

Calculate the operation ; and compute a new fitting . What are

the values of C and A? Does it seems to C = 5 and A = 4? Why or why not?


x2 = x./100;
y2p = 5.*x2.*exp(4.*x2);
plot(x2,y2p,'o');

A = [x2 ones(length(x2),1)];
y2 = log(y2p./x2);
sol = pinv(A)*y2;

a = exp(sol(2));
b = sol(1);
xi = linspace(min(x2),max(x2));
estimacion = a.*xi.*exp(b*xi);

hold on;
plot(xi,estimacion);
hold off;
MODELO LINEAL GENERALIZADO
Es una generalización del modelo de la regresión lineal ordinaria, salvo que tenemos más de una
entrada.

Luego A se construiría de la siguiente forma:

Siendo N = número de datos y M = 1 + Número de características.

Una vez explicado el modelo, ¿Cómo obtendríamos los coeficientes?

1. Definir el error en cada punto

ei = yi − yˆ ( xi ) = yi − ( a1 xi1 + a2 xi 2 + + am xim + a0 )
2. Definir un criterio de error

 e1 
 
n
e 
E =  ei = (e1 e2  en )   2  = r t  r
2


i =i
 
e 
 n

E = r t  r = (b − Ax ) (b − Ax ) =
t

b t b − b t ( Ax ) − ( Ax ) b + ( Ax ) ( Ax ) =
t t

b t b − b t Ax − x t At b + x t At Ax

Como 𝑏 𝑡 𝐴𝑥 es de dimensiones 1x1, se cumple:

bt Ax = (bt Ax ) = xt At b
t

Por lo que:

E = bt b − 2 x t At b + x t At Ax
3. Derivando el error respecto del vector x de parámetros:

    
E  bt b − 2 x t At b + x t At Ax  bt b  2 x t At b  x t At Ax
= = − +
  
x x x x x

Para poder seguir derivando, es preciso saber cómo se deriva respecto de un vector:

Sea z un vector columna y M una matriz, se cumple que:

z t Mz z t M t b
z t M
z
=M
z
= M + Mt z ( ) z
= M tb
Demostración:

z t M
=M
z

Sean Z y M,

𝑍1
𝑀11 … 𝑀1𝑛
𝑍2
𝑍= ( ) → 𝑍 𝑡 = (𝑍1 𝑍2 … 𝑍𝑛 )1𝑥𝑛 ; 𝑀= ( … … … )

𝑀𝑛1 … 𝑀𝑛𝑛 𝑛𝑥𝑛
𝑍𝑛 𝑛𝑥1

𝒁𝒕 𝑴 =
(𝒁𝟏 𝑴𝟏𝟏 + 𝒁𝟐 𝑴𝟐𝟏 + ⋯ + 𝒁𝒏 𝑴𝒏𝟏 𝒁𝟏 𝑴𝟏𝟐 + 𝒁𝟐 𝑴𝟐𝟐 + ⋯ + 𝒁𝒏 𝑴𝒏𝟐 … 𝒁𝟏 𝑴𝟏𝒏 + 𝒁𝟐 𝑴𝟐𝒏 + ⋯ + 𝒁𝒏 𝑴𝒏𝒏 )

Ahora derivaremos la matriz 𝑍𝑡 𝑀 con respecto del vector Z. Para poder derivar respeto de un vector,
tenemos que derivar con cada uno de los elementos que conforman el vector:

∂z t M
∂z1
𝑀11 𝑀12 𝑀1𝑛
∂z t M
𝑀21 𝑀22 𝑀2𝑛
∂z2 = ( ⋮ ⋮ ⋮
)
⋮ 𝑀𝑛1 𝑀𝑛2 𝑀𝑛𝑛
∂z t M
( ∂zn )

∂zt M
De aquí deducimos que ∂z𝑛
=𝑀
z t Mz
z
(
= M + Mt z )
Sean Z y M,

𝑀11 𝑀12 𝑀13 … 𝑀1𝑛


𝑍1
𝑀21 𝑀22 𝑀23 … 𝑀2𝑛
𝑍2
𝑍= ( ) → 𝑍 𝑡 = (𝑍1 𝑍2 𝑍3 … 𝑍𝑛 )1𝑥𝑛 ; 𝑀 = 𝑀31 𝑀32 𝑀33 … 𝑀
𝑍3 ⋮ …3𝑛
⋮ ⋮ ⋮
𝑍4 𝑛𝑥1 … 𝑀𝑛𝑛 )
𝑀
( 𝑛1 𝑀𝑛2 𝑀𝑛3 𝑛𝑥𝑛

𝑍 𝑡 ∗ 𝑀 = ((𝑍1 𝑀11 + 𝑍2 𝑀21 + 𝑍3 𝑀31 + ⋯ + 𝑍𝑛 𝑀𝑛1 ) , (𝑍1 𝑀12 + 𝑍2 𝑀22 + 𝑍3 𝑀32 + ⋯ + 𝑍𝑛 𝑀𝑛2 ),
(𝑍1 𝑀13 + 𝑍2 𝑀23 + 𝑍3 𝑀33 + ⋯ + 𝑍𝑛 𝑀𝑛3 ), … , (𝑍1 𝑀1𝑛 + 𝑍2 𝑀2𝑛 + 𝑍3 𝑀3𝑛 + ⋯ + 𝑍𝑛 𝑀𝑛𝑛 )

𝑍 𝑡 ∗ 𝑀 ∗ 𝑍 = ((𝑍1 𝑀11 +𝑍2 𝑀21 + 𝑍3 𝑀31 + … + 𝑍𝑛 𝑀𝑛1 ) ∗ 𝑍1 + (𝑍1 𝑀12 +𝑍2 𝑀22 + 𝑍3 𝑀32 + … +
𝑍𝑛 𝑀𝑛2 ) ∗ 𝑍2 + (𝑍1 𝑀13 +𝑍2 𝑀23 + 𝑍3 𝑀33 + … + 𝑍𝑛 𝑀𝑛3 ) ∗ 𝑍3 + … + (𝑍1 𝑀1𝑛 +𝑍2 𝑀2𝑛 + 𝑍3 𝑀3𝑛 +
… + 𝑍𝑛 𝑀𝑛𝑛 ) ∗ 𝑍𝑛 )

𝜕𝑧 𝑡 𝑀𝑧
= 2𝑀11 𝑍1 + 𝑀21 𝑍2 + 𝑀31 𝑍3 + … + 𝑀𝑛1 𝑍𝑛 + 𝑀12 𝑍2 + 𝑀13 𝑍3 + ⋯ 𝑀1𝑛 𝑍𝑛
𝜕𝑧1
= 2𝑀11 𝑍1 + (𝑀21 + 𝑀12 )𝑍2 + (𝑀31 + 𝑀13 )𝑍3 + … + (𝑀𝑛1 + 𝑀1𝑛 )𝑍𝑛

𝜕𝑧 𝑡 𝑀𝑧
= 𝑀21 𝑍1 + 𝑀12 𝑍1 + 2𝑀22 𝑍2 + 𝑀32 𝑍3 + ⋯ + 𝑀𝑛2 𝑍𝑛 + 𝑀23 𝑍3 + ⋯ + 𝑀2𝑛 𝑍𝑛
𝜕𝑧2
= (𝑀12 + 𝑀21 )𝑍1 + 2𝑀22 𝑍2 + (𝑀23 + 𝑀32 )𝑍3 + ⋯ + (𝑀2𝑛 + 𝑀𝑛2 )𝑍𝑛


𝜕𝑧 𝑡 𝑀𝑧
= 𝑀𝑛1 𝑍1 + 𝑀𝑛2 𝑍2 + 𝑀𝑛3 𝑍3 + ⋯ + 𝑀1𝑛 𝑍1 + 𝑀2𝑛 𝑍2 + 𝑀3𝑛 𝑍3 + ⋯ + 𝑀𝑛𝑛 𝑍𝑛
𝜕𝑧𝑛
= (𝑀𝑛1 + 𝑀1𝑛 )𝑍1 + (𝑀𝑛2 + 𝑀2𝑛 )𝑍2 + (𝑀𝑛3 + 𝑀3𝑛 )𝑍3 + ⋯ + 2𝑀𝑛𝑛 𝑍𝑛

De aquí obtenemos que:

∂z t M
∂z1
2𝑀11 𝑍1 + (𝑀21 + 𝑀12 )𝑍2 + (𝑀31 + 𝑀13 )𝑍3 + … + (𝑀𝑛1 + 𝑀1𝑛 )𝑍𝑛
∂z t M
(𝑀12 + 𝑀21 )𝑍1 + 2𝑀22 𝑍2 + (𝑀23 + 𝑀32 )𝑍3 + ⋯ + (𝑀2𝑛 + 𝑀𝑛2 )𝑍𝑛 )
∂z2 = ( ⋯
⋮ (𝑀 + 𝑀 )𝑍 + (𝑀 + 𝑀 )𝑍 + (𝑀𝑛3 + 𝑀3𝑛 )𝑍3 + ⋯ + 2𝑀𝑛𝑛 𝑍𝑛
𝑛1 1𝑛 1 𝑛2 2𝑛 2
∂z t M
( ∂zn )

Aquí deducimos que (𝑀 + 𝑀𝑡 )𝑍


z t M t b
= M tb
z

Sean Z y M,

𝑍1
𝑀11 … 𝑀𝑛1
𝑍
𝑍 = ( 2) 𝑡
→ 𝑍 = (𝑍1 𝑍2 … 𝑍𝑛 )1𝑥𝑛 ; 𝑡
𝑀 = ( … … … )

𝑀1𝑛 … 𝑀𝑛𝑛 𝑛𝑥𝑛
𝑍𝑛 𝑛𝑥1

𝒁𝒕 𝑴 =
(𝒁𝟏 𝑴𝟏𝟏 + 𝒁𝟐 𝑴𝟏𝟐 + ⋯ + 𝒁𝒏 𝑴𝟏𝒏 𝒁𝟏 𝑴𝟐𝟏 + 𝒁𝟐 𝑴𝟐𝟐 + ⋯ + 𝒁𝒏 𝑴𝟐𝒏 … 𝒁𝟏 𝑴𝒏𝟏 + 𝒁𝟐 𝑴𝒏𝟐 + ⋯ + 𝒁𝒏 𝑴𝒏𝒏 )

𝒁𝒕 𝑴𝒃 = 𝒚𝟏 (𝒁𝟏 𝑴𝟏𝟏 + 𝒁𝟐 𝑴𝟏𝟐 + ⋯ + 𝒁𝒏 𝑴𝟏𝒏 ) + 𝒚𝟐 (𝒁𝟏 𝑴𝟐𝟏 + 𝒁𝟐 𝑴𝟐𝟐 + ⋯ + 𝒁𝒏 𝑴𝟐𝒏 ) + ⋯ + 𝒚𝒏 (𝒁𝟏 𝑴𝒏𝟏 +
𝒁𝟐 𝑴𝒏𝟐 + ⋯ + 𝒁𝒏 𝑴𝒏𝒏 )

Ahora derivaremos la matriz 𝑍𝑡 𝑀𝑏 con respecto del vector Z. Para poder derivar respeto de un vector,
tenemos que derivar con cada uno de los elementos que conforman el vector:

∂z t Mb
= 𝑴𝟏𝟏 𝒚𝟏 + 𝑴𝟐𝟏 𝒚𝟐 + ⋯ + 𝑴𝒏𝟏 𝒚𝒏
∂z1

∂z t Mb
= 𝑴𝟏𝟐 𝒚𝟏 + 𝑴𝟐𝟐 𝒚𝟐 + ⋯ + 𝑴𝒏𝟐 𝒚𝒏
∂z2

∂z t Mb
= 𝑴𝟏𝒏 𝒚𝟏 + 𝑴𝟐𝒏 𝒚𝟐 + ⋯ + 𝑴𝒏𝒏 𝒚𝒏
∂z𝑛

∂z t Mb
∂z1
∂z t Mb 𝑴𝟏𝟏 𝒚𝟏 + 𝑴𝟐𝟏 𝒚𝟐 + ⋯ + 𝑴𝒏𝟏 𝒚𝒏
∂z2 = ( 𝑴𝟏𝟐 𝒚𝟏 + 𝑴𝟐𝟐 𝒚…𝟐 + ⋯ + 𝑴𝒏𝟐 𝒚𝒏 )
⋮ 𝑴𝟏𝒏 𝒚𝟏 + 𝑴𝟐𝒏 𝒚𝟐 + ⋯ + 𝑴𝒏𝒏 𝒚𝒏
t
∂z Mb
( ∂zn )

∂zt Mb
De aquí deducimos que ∂Z
= 𝑀𝑡 𝑏
Una vez obtenidos tenemos que,

      
E  bt b − 2 x t At b + x t At Ax  bt b  2 x t At b  x t At Ax
= = − +

x x x x x

 
 bt b
=0


x 

 2x A b
t t

= 2 At b


E
= 0 − 2 At b + 2 At Ax
x  x

 x A Ax
t t
 ( ) 
= At A + (At A) x = 2 At Ax 
t

x 
Igualamos a 0,

−2 At b + 2 At Ax = 0
At b = At Ax
Es la y
MODELO NO LINEAL:

MÍNIMO DE UNA FUNCIÓN 2D:

Anteriormente, en el modelo lineal calculábamos el error para así obtener una pendiente que se
acercara a nuestros datos. Por ejemplo, aquí nuestro error sería algo mayor:

Y en este por ejemplo nuestro error sería menor, como podemos observar se acerca mucho a
nuestros datos:
Que el error se acerque a 0 es lo que intentamos conseguir, para obtener una mayor precisión. Bien
pues con el modelo no lineal tenemos que conseguir los mismo, obtener un error pequeño, y ¿Cómo
lo hacemos?

Este sería nuestro error, luego tendríamos que encontrar el mínimo error en ella.
Si lo dibujamos observamos lo siguiente:

Uno de los menores errores se situará por aquí.


Un ejemplo para entenderlo sería como el recorrido de una pelota.

Si la lanzamos desde arriba caerá hasta el menor punto de la curva.


¿Cómo se consigue esto?
Pues a través de la derivada parcial de f sobre x, y la derivada parcial de f sobre y.
Esto nos devolverá dos pendientes, o dos rectas tangentes a la curva, de tal manera que podemos ver
en hacia donde se dirige la recta con respecto de x e y. Nos interesa que la pendiente sea negativa, así
encontramos el menor punto de la función de error dada.

Esto en la vida real nos puede servir para programar por ejemplo una red neuronal. Dado un ajuste
de ella, buscaremos la que menor error nos devuelva.

El algoritmo para determinar el mínimo es el siguiente:


1. Elegir un punto inicial cualquiera 𝑥0
2. Dirección = la del gradiente negativo
3. Longitud del paso = 𝛼𝑘
4. Modificar x: 𝑥𝑘+1 = 𝑥𝑘 + 𝛼𝑘 𝑝𝑘
5. Volver al paso 2

[x,y]=meshgrid(-3:0.1:3,-4:0.1:4);
z = (x.^4 - 3*x.^2 + x + y.*y + x.*y);
mesh(x,y,z)
while(1),
close all
pcolor(x,y,z),shading flat;hold on;
sol = ginput(1)';
for i=1:100, % Condición de parada
x=sol(1,i);
y=sol(2,i);
dx = 4 * x.^3 - 6*x + 1 + y;
dy = 2 * y + x;
sol(:,i+1) = sol(:,i) - 0.1 * [dx;dy]; % Siguiente interación
plot([sol(1,i) sol(1,i+1)],[sol(2,i) sol(2,i+1)],'r', 'LineWidth',3)
pause
end;
end;
Dada la función determina el mínimo.

Marco en rojo los simbólicos


%Hacemos x e y simbólicos para que podamos asignarles una variable después
syms X Y;
[x,y]=meshgrid(-3:0.1:3,-4:0.1:4);
z = (x.^4 - 3*x.^2 + x + y.*y + x.*y);
Z = (X.^4 - 3*X.^2 + X + Y.*Y + X.*Y);

while(1),
close all
pcolor(x,y,z),shading flat;hold on;
sol = ginput(1)’;
for i=1:100,
%subs asigna un valor a una variable
%diff devuelve la derivada parcial respecto de una variable
dx = subs(diff(Z,X),{X Y},{sol(1,i) sol(2,i)});
dy = subs(diff(Z,Y),{X Y},{sol(1,i) sol(2,i)});
sol(:,i+1) = sol(:,i) - 0.1 * [dx;dy];
plot([sol(1,i) sol(1,i+1)],[sol(2,i) sol(2,i+1)],'r', 'LineWidth',3)
pause
end;
end;
MÉTODO DE NEWTON:

Es parecido al anterior método salvo que esta vez tenemos que realizar dos derivadas parciales.
[x,y]=meshgrid(-3:0.1:3,-4:0.1:4);
z = (x.^4 - 3*x.^2 + x + y.*y + x.*y);

while(1),
close all
pcolor(x,y,z),shading flat;hold on;
sol = ginput(1)';
for i=1:100,
x=sol(1,i);
y=sol(2,i);
dx = 4 * x.^3 - 6*x + 1 + y;
dy = 2 * y + x;
dxx = 12 * x.^2 - 6; dxy = 1;
dyx = 1; dyy = 2;
H = [dxx dxy;dyx dyy];
sol(:,i+1) = sol(:,i) - 0.1 * inv(H) * ([dx;dy]);
plot([sol(1,i) sol(1,i+1)],[sol(2,i) sol(2,i+1)],'r')
pause
end;
end;

Dada la función determina el mínimo usando el método de newton


tomando el punto inicial (10,10).

close all
syms X Y;
%x=-1:0.01:1;
%y=-1:0.01:1;
[x,y]=meshgrid(-10:0.01:10,-10:0.01:10);
a=100*(x.^2 - y.^2) + (1+x).^2;
A=100*(X.^2 - Y.^2) + (1+X).^2;
mesh(a); xticklabels([-1,-0.5,0,0.5,1]); yticklabels([-1,-0.5,0,0.5,1])
pcolor(x,y,a),shading flat;hold on;
sol = [10 ; 10];
finded = false;
i = 1;
while ~finded,

dx = subs(diff(A,X),{X Y},{sol(1,i) sol(2,i)});


dy = subs(diff(A,Y),{X Y},{sol(1,i) sol(2,i)});
dxx = subs(diff(diff(A,X),X),{X Y},{sol(1,i) sol(2,i)});
dxy = subs(diff(diff(A,X),Y),{X Y},{sol(1,i) sol(2,i)});
dyx = subs(diff(diff(A,Y),X),{X Y},{sol(1,i) sol(2,i)});
dyy = subs(diff(diff(A,Y),Y),{X Y},{sol(1,i) sol(2,i)});
H = [dxx dxy;dyx dyy];
sol(:,i+1) = sol(:,i) - 0.1 * inv(H) * ([dx;dy]);
plot([sol(1,i) sol(1,i+1)],[sol(2,i) sol(2,i+1)],'r', 'LineWidth',3)
if sqrt(sum((sol(:,i) - sol(:,i+1)).^2)) <= eps
finded = true;
end
i = i + 1;
end;

También podría gustarte