Capítulo
1: REPRESENTACIÓN NUMÉRICA Y TEORÍA DE ERRORES
Índice:
Conversión
de un número real a binario IEEE754:
x=123.456,signo=(sign(x)==-1);x=abs(x);xent=floor(x);xdec=x-xent;
%% Parte entera a binario:
res=mod(xent,2);xentb=mod(xent,2);xent=(xent-xentb)/2;
while(xent>1)res=mod(xent,2);xentb=[mod(xent,2),xentb];
xent=(xent-xentb(1))/2;end; if(xent>0)xentb=[xent,xentb];end;
%% Parte decimal a binario:
xdecb=[];for i=1:1075, xdec=xdec*2;xdecb=[xdecb,floor(xdec)];
xdec=xdec-floor(xdec); end;
%%Numero en binario y calculo del exponente:
xbi=[xentb,xdecb,1];
nzero=0;while(xbi(1)==0)xbi=xbi(2:length(xbi));nzero=nzero+1;end;
expo=length(xentb)-1-nzero; if(expo>-1023)mantisa=xbi(2:53);
elseif(x==0)mantisa=zeros(1,52);expo=-1023;fprintf('cero\n');
else expo=-1023;fprintf('underflow\n');end;
%% Exponente a binario:
expo=expo+1023;res=mod(expo,2);expob=mod(expo,2);expo=(expo-expob)/2;
while(expo>1)res=mod(expo,2);expob=[mod(expo,2),expob];
expo=(expo-expob(1))/2;end; if(expo>0)expob=[expo,expob];end;
nexpo=length(expob); if(nexpo<11) expob=[zeros(1,11-nexpo),expob];
elseif(nexpo>11)fprintf('overflow\n');expob=ones(1,11);
mantisa=zeros(1,52);end; IEEE754=[signo,expob,mantisa]
IEEE754=[0 1 0 0 0 0 0 0 0 1 0 1 1
1 1 0 1 1 0 1 1 1 0 1 0 0 1 0 1 ...
1 1 1 0 0 0 1 1 0 1 0 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 0 1 1 1]
xexpo=sum(IEEE754(2:12).*2.^(10:-1:0))-1023;
xnum=(-1)^IEEE754(1)*sum([1,IEEE754(13:64)].*2.^((0:-1:-52)+xexpo));
if(sum(IEEE754(2:12))==0)xnum=0; if(sum(IEEE754(13:64))==0)
fprintf('cero\n');else fprintf('underflow\n');end;end;
if(sum(IEEE754(2:12))==11)xnum=Inf;
if(sum(IEEE754(13:64))==0) fprintf('Infinito\n');
else xnum=NaN;fprintf('NaN\n');end;end; xnum
x=(-3:0.1:3)';Ex=exp(x);Y=[];E=[];P=0*x;
for n=0:10;P=P+x.^n./factorial(n);Y=[Y,P];E=[E,abs(Ex-P)];end;
figure,plot(x,Y(:,[1,2,3,5,11])),hold on,plot(x,Ex,'+b')
figure,semilogy(x,E(:,[1,2,3,5,11]))
figure,semilogy(x,E(:,[1,2,3,5,11])./abs([Ex,Ex,Ex,Ex,Ex]))
syms x, f=exp(x), taylor(f,x)
syms x,f=exp(x),fder=diff(f,3,x),ezplot(fder,[0,0.1]),grid
Ir al inicio
Aproximación
2D de Taylor de exp(x+2*y):
hold off;ezmesh('exp(x+2*y)',[-.5 .5 -.5 .5]);
hold on;ezmesh('1+x+2*y',[-.5 .5 -.5 .5])
figure; ezmesh('exp(x+2*y)',[-.5 .5 -.5 .5]);hold on;
ezmesh('1+x+2*y+x?2/2+2*x*y+2*y?2',[-.5 .5 -.5 .5])
syms x y t;f=exp(x+2*y);a=0+t*(0.1-0);b=0+t*(0.01-0);tt=(0:0.005:1)';
Dfxx=diff(f,x,2);Dfxy=diff(diff(f,x),y);Dfyy=diff(f,y,2);
Dfxx=subs(Dfxx,x,a);Dfxx=eval(subs(Dfxx,y,b));
Dfxy=subs(Dfxy,x,a);Dfxy=eval(subs(Dfxy,y,b));
Dfyy=subs(Dfyy,x,a);Dfyy=eval(subs(Dfyy,y,b));
Dfxx=subs(Dfxx,t,tt);Dfxy=subs(Dfxy,t,tt);
Dfyy=subs(Dfyy,t,tt);
figure,plot(tt,[Dfxx,Dfxy,Dfyy]);grid
Dfx3y0=diff(f,x,3);Dfx2y1=diff(diff(f,x,2),y,1);
Dfx1y2=diff(diff(f,x,1),y,2);Dfx0y3=diff(f,y,3);
Dfx3y0=subs(Dfx3y0,x,a);Dfx3y0=eval(subs(Dfx3y0,y,b));
Dfx2y1=subs(Dfx2y1,x,a);Dfx2y1=eval(subs(Dfx2y1,y,b));
Dfx1y2=subs(Dfx1y2,x,a);Dfx1y2=eval(subs(Dfx1y2,y,b));
Dfx0y3=subs(Dfx0y3,x,a);Dfx0y3=eval(subs(Dfx0y3,y,b));
Dfx3y0=subs(Dfx3y0,t,tt);Dfx2y1=subs(Dfx2y1,t,tt);
Dfx1y2=subs(Dfx1y2,t,tt);Dfx0y3=subs(Dfx0y3,t,tt);
figure,plot(tt,[Dfx3y0,Dfx2y1,Dfx1y2,Dfx0y3]);grid
syms x y; taylor(exp(x+2*y),[x,y],'ExpansionPoint',[0 0],'Order',3)
syms x y;
taylor([exp(x+2*y);sin(x*y)],[x,y],'ExpansionPoint',[0 0],'Order',3)
fv=@(x)(x-sin(x))./x.^3, fplot(f,[-0.1,0.1])
x=linspace(-1.e-7,1.e-7,201);yd=f(x);plot(x,yd)
x=linspace(-5.e-3,5.e-3,201);ys=f(single(x));plot(x,ys)
syms x,taylor((x-sin(x))/x^3,x,'Order',5)
Ir al iniciot=0.1, n=1:10, e=n/10-n*t
Ir al iniciox=0.2, while 1+x>1, x=x/2, end
x=1, while x+x>x,x=20*x, end
x=1, while x+x>x,x=x/20, end
Ir al iniciofunction s=sinusA(x)
s=0; t=x; n=1;
while s+t~=s
s=s+t;
t=-x.^2/((n+1)*(n+2)).*t;
n=n+2; end, n
function s=sinusB(x)
x0=floor(x/(2*pi))*2*pi;s=0;
t=x-x0; n=1;
while s+t~=s, s=s+t;
t=-(x-x0).^2/((n+1)*(n+2)).*t;
n=n+2; end, n