%fl 2017/18
clear all
%epsilon maszynowy
t=1;n=0;
 while(t+1>1)
t=t*0.5;n++;
end
t*2
n-1
eps
%epsilon maszynowy single
t=single(1);n=0;
 while(t+1>1)
t=t*0.5;n++;
end
t*2
n-1
eps(single(1))

%wykres (x+1)-a a duze np 5^n
graphics_toolkit("gnuplot");
m=27;
a=1;x=linspace(0,1,200);
for k=1:m,
 a=a*5;
 opis=[";", num2str(k), ";"];
 if(k>15)
  plot(x,(x+a)-a,opis);
  pause(1);
 end 
end 

%wykres (x-2)^4=x^4-8x^3+24x^2-32x +16 na 2 sposoby
%czyli z obu wzorow na [2-e,2+e] e=1e-p p=1,2,3,4
e=0.1;m=4;
for k=1:m,
 x=linspace(2-e,2+e);
 f1=(x-2).*(x-2).*(x-2).*(x-2);
 f2=x.*x.*x.*x- 4*2*x.*x.*x+ 6*4*x.*x - 4*8*x+16; 
 opis=[";(x-2)^4 ", num2str(e), ";"];
 plot(x,f1,opis);pause(1);
 opis=[";x^4-.. ", num2str(e), ";"];
 plot(x,f2,opis);pause(1); 
 e=e*0.1;
end  

%x-\sqrt(x*x+1) na 2 sposoby 
x=[10 1e2 1e3 1e4  1e5 1e6 1e7 1e8 1e9 1e10];

y1=x-sqrt(x.*x+1);
y2=-1.0./(x+sqrt(x.*x+1));
er=y1-y2;
ew=er./y2;
[1:10;er;ew]

%obliczamy exp(x) z erzowiniecia w szereg - porownujemy
%z exp(x) z octave'a

function y=nexp(x,N=1000)
 xx=1;y=0; 
 for k=1:N,
  y+=xx;
  xx=xx.*(x/k); % x^(k+1)/k!=(x/k!)*x/(k+1)  
 end
end
%test x=k*10 k=-10,..,10
x=linspace(-50,50,21);
y=nexp(x);
y1=exp(x);
er=y-y1;
ew=er./y1;
[x;er;ew]'


function y=nexp1(x,N=100)
 xp=(x>=0);
 xm=(x<0);
 x=-x.*xm+x.*xp;   
 xx=1;y=0; 
 for k=1:N,
  y+=xx;
  xx=xx.*(x/k); % x^(k+1)/k!=(x/k!)*x/(k+1)  
 end
 y=1./y.*xm+y.*xp;
end
%test x=k*10 k=-10,..,10
x=linspace(-50,50,21);
y=nexp1(x);
y1=exp(x);
er=y-y1;
ew=er./y1;
[x;er;ew]'
