Ejercicios Del Lab.N°6 15-06-22

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

% Ejercicio No.

1
% x=[1 2 4 5]'
% y=[3 8 2 7]'
% Interpolacion de Lagrange paso a paso
clc
clear all
x=[1 2 4 5]'
y=[3 8 2 7]'
syms z
L1=((z-2)*(z-4)*(z-5))/((1-2)*(1-4)*(1-5))
L2=((z-1)*(z-4)*(z-5))/((2-1)*(2-4)*(2-5))
L3=((z-1)*(z-2)*(z-5))/((4-1)*(4-2)*(4-5))
L4=((z-1)*(z-2)*(z-4))/((5-1)*(5-2)*(5-4))
p=L1*y(1)+L2*y(2)+L3*y(3)+L4*y(4)
p=expand(p)
ev1=subs(p,x)
xx=min(x):0.1:max(x);
yy=double(subs(p,xx));
plot(x,y,'d',xx,yy),grid
legend('Data','Polinomio interpolante')

%L1 = -((z - 2)*(z - 4)*(z - 5))/12

%L2 = ((z - 1)*(z - 4)*(z - 5))/6

%L3 = -((z - 1)*(z - 2)*(z - 5))/6

%L4 = ((z - 1)*(z - 2)*(z - 4))/12

%p = (7*(z - 1)*(z - 2)*(z - 4))/12 - ((z - 1)*(z - 2)*(z - 5))/3 +


(4*(z - 1)*(z - 4)*(z - 5))/3 - ((z - 2)*(z - 4)*(z - 5))/4

%p = (4*z^3)/3 - 12*z^2 + (95*z)/3 - 18

%ev1 =
3
8
2
7
% Ejercicio 2:
% Modicar para la regresion cuadratica
clc
clear all, format long
x=[0.1 0.4 0.5 0.7 0.7 0.9]
y=[0.61 0.92 0.99 1.52 1.47 2.03]
plot(x,y,'d'), grid
M=[sum(x.^4) sum(x.^3) sum(x.^2);sum(x.^3) sum(x.^2)
sum(x);sum(x.^2) sum(x) length(x)]
N=[sum((x.^2).*y) sum(x.*y) sum(y)]'
p1=M\N
fprintf('Ajuste cuadratico y=(%10.6f)*x^2+(%10.6f)*x+(%10.6f)
\n',p1(1),p1(2),p1(3))
p2=inv(M)*N
p3=polyfit(x,y,2)
xx=min(x):0.1:max(x);
yp=polyval(p1,xx);
ym=mean(y)
ys=polyval(p1,x);
r2=sum((ys-ym).^2)/sum((y-ym).^2)
plot(x,y,'dr',x,ys,'*m',xx,yp,'g'), grid
legend('Data','ys','Funcion de ajuste')
%M =
1.224500000000000 1.605000000000000 2.210000000000000
1.605000000000000 2.210000000000000 3.300000000000000
2.210000000000000 3.300000000000000 6.000000000000000

%N =
3.510200000000000
4.843999999999999
7.539999999999999

%p1 =
1.729547488175895
0.059091780646806
0.587116195832802

%Ajuste cuadratico y=( 1.729547)*x^2+( 0.059092)*x+( 0.587116)

%p2 =
1.729547488175885
0.059091780646815
0.587116195832799

%p3 = 1.729547488175890 0.059091780646813 0.587116195832800

%ym = 1.256666666666667

%r2 = 0.994948280819552
% Ejercicio 3: modificar para
% y=c1*x+c2*exp(-x)+c3*x^2
% ajuste por minimos cuadrados
% Ecuacion Normal
clc
clear all, format long
x=[0.1 0.4 0.5 0.7 0.7 0.9]'
y=[0.61 0.92 0.99 1.52 1.47 2.03]'
M=[x exp(-x) x.^2]
P=M'*M
Q=M'*y
c=P\Q
fprintf('Funcion de ajuste y=(%6.4f)*x+(%6.4f)*exp(-x)+(%6.4f)*x^2
\n',c(1),c(2),c(3))
ys=c(1)*x+c(2)*exp(-x)+c(3)*x.^2
xx=min(x):0.01:max(x);
yy=c(1)*xx+c(2)*exp(-xx)+c(3)*xx.^2;
plot(x,y,'d',x,ys,'*',xx,yy), grid
legend('Data','ys','Funcion de ajuste')
ym=mean(y)
r2=sum((ys-ym).^2)/sum((y-ym).^2)

%M =
0.100000000000000 0.904837418035960 0.010000000000000
0.400000000000000 0.670320046035639 0.160000000000000
0.500000000000000 0.606530659712633 0.250000000000000
0.700000000000000 0.496585303791410 0.490000000000000
0.700000000000000 0.496585303791410 0.490000000000000
0.900000000000000 0.406569659740599 0.810000000000000

%P =
2.210000000000000 1.723009209148681 1.605000000000000
1.723009209148681 2.294431974471445 1.083907268579687
1.605000000000000 1.083907268579687 1.224500000000000

%Q =
4.843999999999999
4.079237088079961
3.510200000000000

%c =
0.606410517147584
0.590838120407182
1.548793292539667
%Funcion de ajuste y=(0.6064)*x+(0.5908)*exp(-x)+(1.5488)*x^2

%ys =
0.610741423986609
0.886421769736334
1.048765016662649
1.476797602861692
1.476797602861692
2.040508885965680

%ym = 1.256666666666667

%r2 = 0.994917915525594
% Ejercicio 4
% Realizar un ajuste exponencial y=a*exp(b*x)
% Ajuste Potencial
% y=a*e^xb
% Linealizando
% ln(y)=ln(a)+b*x
% Y=ln(y) ln(a)=A
% X=x b=B
% Y=A+B*X
clc
clear all
x=[1 10 50 100]
y=[1 5 10 20]
X=x
Y=log(y)
p=polyfit(X,Y,1) % p(1)*x+p(2)
A=p(2)
B=p(1)
a=exp(A)
b=B
ys=a*exp(x*b)
xx=min(x):1:max(x);
yy=a*exp(xx*b);
subplot(1,2,1)
plot(x,y,'d',x,ys,'*',xx,yy)
legend('data','ys','ajuste potencial'), grid
XX=min(X):0.1:max(X);
YY=polyval(p,XX);
subplot(1,2,2)
YS=polyval(p,X)
plot(X,Y,'d',XX,YY,X,YS,'*'), grid
legend('Data','Ajuste Lineal')
% Hallar los factores de regresion r^2 y R^2
YM=mean(Y)
ym=mean(y)
R2=sum((YS-YM).^2)/sum((Y-YM).^2)
r2=sum((ys-ym).^2)/sum((Y-ym).^2)

%X = 1 10 50 100

%Y = 0 1.609437912434100 2.302585092994046 2.995732273553991

%p = 0.024957678577039 0.722392257019715

%A = 0.722392257019715
%B = 0.024957678577039

%a = 2.059353826255526

%b = 0.024957678577039

%ys = 2.111397257342369 2.643143802056795 7.172657201392860

24.982113647870673

%YS = 0.747349935596754 0.971969042790105 1.970276185871664

3.218160114723614

%YM = 1.726938819745534

%ym = 9

%R2 = 0.772184170599958

%r2 = 1.600860854673139
% Ejercicio 5 : Aplicar spline natural agregar (5,20)
clc
clear all
x=[1 2 4 5]
y=[2 10 7 20]
% x en [1,2] S0(x)=a0*x^3+b0*x^2+c0*x+d0
% x en [2,4] S1(x)=a1*x^3+b1*x^2+c1*x+d1
% x en [4,5] S2(x)=a2*x^3+b2*x^2+c2*x+d2

% Cond. Interpolacion
% S0(1)=2 S0(1)=a0*1^3+b0*1^2+c0*1+d0=2 (1)
% S0(2)=10 S0(2)=a0*2^3+b0*2^2+c0*2+d0=10 (2)
% S0(2)=S1(2) a0*2^3+b0*2^2+c0*2+d0=a1*2^3+b1*2^2+c1*2+d1 (3)
% S1(4)=7 S1(4)=a1*4^3+b1*4^2+c1*4+d1=7 (4)
% S1(4)=S2(4) a1*4^3+b1*4^2+c1*4+d1=a2*4^3+b2*4^2+c2*4+d2 (5)
% S2(5)=20 S2(5)=a2*5^3+b2*5^2+c2*5+d2=20 (6)

% Continuidad de la 1era derivada S0'(2)=S1'(2) // S1'(4)=S2'(4)


% 3*a0*2^2+2*b0*2+c0=3*a1*2^2+2*b1*2+c1 (7)
% 3*a1*4^2+2*b1*4+c1=3*a2*4^2+2*b2*4+c2 (8)

% Continuidad de la 2da derivada S0"(2)=S1"(2) // S1"(4)=S2"(4)


% 6*a0*2+2*b0=6*a1*2+2*b1 (9)
% 6*a1*4+2*b1=6*a2*4+2*b2 (10)

% Eleginos spline natural


% s0"(1)=0 6*a0*1+2*b0=0 (11)
% S2"(5)=0 6*a2*5+2*b2=0 (12)

M=[1 1 1 1 0 0 0 0 0 0 0 0
8 4 2 1 0 0 0 0 0 0 0 0
8 4 2 1 -8 -4 -2 -1 0 0 0 0
0 0 0 0 64 16 4 1 0 0 0 0
0 0 0 0 64 16 4 1 -64 -16 -4 -1
0 0 0 0 0 0 0 0 125 25 5 1
12 4 1 0 -12 -4 -1 0 0 0 0 0
0 0 0 0 48 8 1 0 -48 -8 -1 0
12 2 0 0 -12 -2 0 0 0 0 0 0
0 0 0 0 24 2 0 0 -24 -2 0 0
6 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 30 2 0 0]
N=[2 10 0 7 0 20 0 0 0 0 0 0]'
c=M\N % a0 b0 c0 d0 a1 b1 c1 d1 M*c=N
syms z
S0=c(1)*z^3+c(2)*z^2+c(3)*z+c(4)
S1=c(5)*z^3+c(6)*z^2+c(7)*z+c(8)
S2=c(9)*z^3+c(10)*z^2+c(11)*z+c(12)
% Aproximar y(1.2)
xi=1.2
yi=double(subs(S0,xi))
% Verificando condiciones
% Interpolacion y continuidad de S
e1=subs(S0,1)
e2=subs(S0,2)
e3=subs(S1,2) % e2=e3, por continuidad
e4=subs(S1,4)
% continuidad de S'
e5=subs(diff(S0),2) % S0'(2)
e6=subs(diff(S1),2) % S1'(2) e5=e6
% continuidad de S"
e7=subs(diff(diff(S0)),2) % S0"(2)
e8=subs(diff(diff(S1)),2) % S1"(2)
% e7=e8
% Condicion de spline natural S"=0 en los extremos
e9=subs(diff(diff(S0)),1) % S0"(1)=0
e10=subs(diff(diff(S1)),4) % S1"(4)=0
% Graficando
x1=1:0.1:2; x2=2:0.1:4; x3=4:0.1:5
y1=subs(S0,x1); y2=subs(S1,x2); y3=subs(S2,x3)
plot(x1,y1,'m',x2,y2,'c',x3,y3,'r'), grid

You might also like