Tuesday, October 3, 2017

14ma Clase Métodos Numéricos

Dados los siguientes datos:
X=-4:0.5:5;
Y=[0.1;0.2;0.3;0.6;1.3;2.5;3.8;3.6;2.5;1.6;1.1;0.8;0.6;0.5;0.4;0.3;0.3;0.2;0.2];

a)
Graficar los puntos

>> X=-4:0.5:5;
>> Y=[0.1;0.2;0.3;0.6;1.3;2.5;3.8;3.6;2.5;1.6;1.1;0.8;0.6;0.5;0.4;0.3;0.3;0.2;0.2];
>> plot(X,Y,'*')
>>
f1.png










b) Hallar a,b,c,p y q/ F(X)=(A*e^(-X^2)/2 + bx + C ) / px^2+qx+1=Y

>> A=[exp(-X.^2) X X.^0 -Y.*X.^2 -Y.*X];
>> a=pinv(A)*Y

a =

    0.5146
    0.3656
    2.0646
    0.5010
    0.9714

>> F=@(X) (a(1)*exp(-X.^2 /2) + a(2)*X + a(3))./(a(4)*X.^2 + a(5)*X + 1);
>> norm(Y-F(X))/norm(Y)

ans =

    0.0274

>> plot(X,Y,'*',X,F(X));
f2.png







c) comparar en el grafico el polinomio de grado 10 y hallar p(x) =a1x^10 + a2*X^9 + …
>> p=polyfit(X,Y,10);
>> Fp=@(X)polyval(p,X);
>> plot(X,Y,'*',X,Fp(X));
>> norm(Y-Fp(X))/norm(Y)

ans =

    0.0644

>>

d) Estimar los valores de y para X=-3.3 X=-3.8 X=4.9 X=-7 X=8 usando F(X) y p(X)

>> F(-3.3)

ans =

    0.2646

>> Fp(-3.3)

ans =

    0.3420

>> F(-3.8)

ans =

    0.1487

>> Fp(-3.8)

ans =

  -0.1387

>> F(4.9)

ans =

    0.2168

>> Fp(4.9)

ans =

  -0.0364

>> F(-7)

ans =

  -0.0264

>> Fp(-7)

ans =

 7.5590e+003

>> F(8)

ans =

    0.1222

>> Fp(8)

ans =

 6.3579e+003

>>


APUNTE:
Planteemos una resolución de cocientes donde numerador y denominador son polinomios

Armamos una función que nos va a dar los coeficientes de los polinomios del cociente:


Function [P,Q]=padefit(X,Y,n,m)
A=[]
For
A=[X^n]
A=[X^n X^n-1]
End
For
A=[X^n … X^0 -YX^m]
A=[X^n … X^0 -YX^m -YX^m-1]
End
coef=pinv(A)*Y;`p=coef(1,n+1)
P=coef(1,n+1)
Q=coef(n+2;end)


Por otro lado tenemos una funcion similar a polyval, que llamaremos padeval

Function Y=padeval(P,Q,X);
Y=polyval(P,X)./polyval([Q;1],X);


Link a carpeta con funciones


>> A=[X X.^0 -Y.*X.^2 -Y.*X];
>> coef=pinv(A)*Y

coef =

    0.4147
    2.4551
    0.6172
    1.0688


Podremos entonces reemplazar:
>> g=@(t) (coef(1)*t + coef(2))./(coef(3)*t.^2+coef(4)*t + 1);

Con nuestra función Padeval:
>> g=@(t)padeval(P,Q,t);
>>


EJERCICIO:
a) Graficar los datos
>> X=(-4:0.5:6)';
>> Y=[27.4;21.2;16.1;12.1;9.1;6.8;5.6;6.9;2;3;3.8;4.9;6.4;8.2;10.3;12.8;15.4;18.4;21.5;24.9;28.5];
>> plot(X,Y,'*')
>>
f3.png
b) Hallar el modelo de Padé de grados P y Q lo menor posible tales que el error relativo sea menor a o.o3 (3%)
>> [n,m]=busca_grados_pade(X,Y,0.03)

n =

    3


m =

    2

>> [P,Q]=padefit(X,Y,n,m)

P =

    2.5487
    0.9917
    8.4346
    2.2246


Q =

    0.1691
    2.5568

>> g=@(X)padeval(P,Q,X);
>>norm(Y-g(X))/norm(Y)
ans =

    0.0270

>>


c) Hallar el p(X) pol de menor grado con error relativo de 3% y graficar. ¿Quien tiene menos parámetros, g o p?

>> cp=polyfit(X,Y,k);
>> p=@(t)polyval(cp,t);
>> ep=norm(Y-p(X))/norm(Y);
>> while ep>0.03
k=k+1;
cp=polyfit(X,Y,k);
p=@(t)polyval(cp,t);
ep=norm(Y-p(X))/norm(Y);
end
Warning: Polynomial is badly conditioned. Add points with distinct X
         values, reduce the degree of the polynomial, or try centering
         and scaling as described in HELP POLYFIT.
> In polyfit at 80
Warning: Polynomial is badly conditioned. Add points with distinct X
         values, reduce the degree of the polynomial, or try centering
         and scaling as described in HELP POLYFIT.
> In polyfit at 80
Warning: Polynomial is badly conditioned. Add points with distinct X
         values, reduce the degree of the polynomial, or try centering
         and scaling as described in HELP POLYFIT.
> In polyfit at 80
Warning: Polynomial is badly conditioned. Add points with distinct X
         values, reduce the degree of the polynomial, or try centering
         and scaling as described in HELP POLYFIT.
> In polyfit at 80
Warning: Polynomial is badly conditioned. Add points with distinct X
         values, reduce the degree of the polynomial, or try centering
         and scaling as described in HELP POLYFIT.
> In polyfit at 80
Warning: Polynomial is badly conditioned. Add points with distinct X
         values, reduce the degree of the polynomial, or try centering
         and scaling as described in HELP POLYFIT.
> In polyfit at 80
>> k

k =

    18

>> 

No comments:

Post a Comment