Está en la página 1de 8

Escuela Politcnica Nacional

Software de Simulacin
Dennys Alexander Singo Salazar
06 de enero de 2015

Regresin.
function [a0,a1,syx,r2] = regresion(x,y)
sumx = 0;
sumxy = 0;
st = 0;
sumy = 0;
sumx2 = 0;
sr = 0;
n = length(x);
for i = 1:n
sumx = sumx + x(i);
sumy = sumy + y(i);
sumxy = sumxy + x(i)*y(i);
sumx2 = sumx2 + x(i)*x(i);
end
xm = sumx/n;
ym = sumy/n;
a1 = (n*sumxy - sumx*sumy)/(n*sumx2 - sumx*sumx);
a0 = ym - a1*xm;
for i = 1:n;
st = st + (y(i) - ym)^2;
sr = sr + (y(i) - a1*x(i) - a0)^2;
end
syx = (sr/(n-2))^0.5;
r2 = (st - sr)/st;
end

Ejercicio de Aplicacin 1.
clear all
clc
x = [6, 7, 11, 15, 17, 21, 23, 29, 29, 37, 39];
y = [29, 21, 29, 14, 21, 15, 7, 7, 13, 0, 3];
plot(x,y,'ro','markersize',8,'markerfacecolor','r')
[a0,a1,syx,r2] = regresion(x,y)
Xc = x
Yc = a0 + a1*x
hold on
plot (Xc,Yc)
% Al aadir una nueva medicion de x = 10, y = 10, tomando en cuenta el
% error estandar, este punto estaria alejado de la recta que se ajusta
% a los otros puntos.

Ejercicio de Aplicacin 2.
clear all
clc
x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [1, 1.5, 2, 3, 4, 5, 8, 10, 13];
subplot(211)
plot(x,y,'ro','markersize',8,'markerfacecolor','r')
[a0,a1,syx,r2] = regresion(x,y)
Xc = x
Yc = a0 + a1*x
hold on
plot (Xc,Yc)
%%
%b
x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [1, 1.5, 2, 3, 4, 5, 8, 10, 13];
subplot(212)
plot(x,y,'ro','markersize',8,'markerfacecolor','r')
p = polyfit(x,y,2)
yc = polyval(p,x)
hold on
plot(x,yc)

Ejercicio de Aplicacin 3.
clear all
clc
x = [2.5, 3.5, 5, 6, 7.5, 10, 12.5, 15, 17.5, 20];
y = [13, 11, 8.5, 8.2, 7, 6.2, 5.2, 4.8, 4.6, 4.3];
subplot(221)
plot(x,y,'ro','markersize',6,'markerfacecolor','r')
Xp = log10(x);
Yp = log10(y);
subplot (222)
plot(Xp,Yp,'ro','markersize',6,'markerfacecolor','r')
[a0p, a1p] = regresion(Xp, Yp)
a2 = 10^a0p
b2 = a1p
Xc = x;
Yc = a2 * x.^b2;
subplot(212)
plot(x,y,'ro','markersize',6,'markerfacecolor','r')
hold on
plot (Xc, Yc)
xlabel('x')
ylabel('y')
title('Regresin potencial')
x=9
y = a2*x.^b2

Ejercicio de Aplicacin 4.
clear all
clc
x = [0.4, 0.8, 1.2, 1.6, 2, 2.3];
y = [800, 975, 1500, 1950, 2900, 3600];
subplot(211)
semilogy(x,y,'ro','markersize',2,'markerfacecolor','r')
[a0p a1p]=regresion(x,log(y));
subplot(212)
hold on
plot(x,y,'ro','markersize',8,'markerfacecolor','r')
x=linspace(min(x),max(x),100);
y=exp(a0p(1))*exp(x*a1p(1));
plot(x,y,'b')
xlabel('x')
ylabel('y')
title('Regresin exponencial')

Ejercicio de Aplicacin 6.
clear all
clc
x = [3,4,5,7,8,9,11,12];
y = [1.6,3.6,4.4,3.4,2.2,2.8,3.8,4.6];
p=polyfit(x,y,3)
%grficos
hold on
plot(x,y,'ro','markersize',8,'markerfacecolor','r')
x=linspace(min(x),max(x),50);
y=polyval(p,x);
plot(x,y,'b')
xlabel('x')
ylabel('y')
title('Polinomio aproximador')
hold of

Ejercicio de Aplicacin 7.
clear all
clc
x1 = [0;1;1;2;2;3;3;4;4];
x2 = [0;1;2;1;2;1;2;1;2];
y = [15.1; 17.9; 12.7; 25.6; 20.5; 35.1; 29.7; 45.4; 40.2];
X = [ones(size(x1)) x1 x2 x1.*x2]
b = regress(y,X)
scatter3(x1,x2,y,'filled')
hold on
X1 = linspace(0,4,100)
X2 = linspace(0,2,10)
[X1FIT X2FIT] = meshgrid(X1,X2);
YFIT = b(1) + b(2)*X1FIT + b(3)*X2FIT + b(4)*X1FIT.*X2FIT;
mesh(X1FIT,X2FIT,YFIT)
view(50,10)

Ejercicio de Aplicacin 8.
clear all
clc
x = [10, 20, 30, 40, 50, 60, 70, 80];
y = [25, 70, 380, 550, 610, 1220, 830, 1450];
subplot(211)
plot(x,y,'ro','markersize',6,'markerfacecolor','r')
[a0,a1,syx,r2] = regresion(x,y)
Xc = x
Yc = a0 + a1*x
hold on
plot (Xc,Yc)
%%
Xp = log10(x);
Yp = log10(y);
[a0p, a1p] = regresion(Xp, Yp)
a2 = 10^a0p
b2 = a1p
Xc = x;
Yc = a2 * x.^b2;
subplot(212)
plot(x,y,'ro','markersize',6,'markerfacecolor','r')
hold on
plot (Xc, Yc)
%%
[a0p a1p]=regresion(x,log(y));
subplot(313)

hold on
plot(x,y,'ro','markersize',8,'markerfacecolor','r')
x=linspace(min(x),max(x),100);
y=exp(a0p(1))*exp(x*a1p(1));
plot(x,y,'b')
xlabel('x')
ylabel('y')
title('Regresin exponencial')

También podría gustarte