hold off;
clf;
clear all;


% t=[1 1.1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2];
% f=[8 8.9 9 9.8 10 11 11.5 11.5 12.5 13 13.7 14];
% p=polyfit(t,f,1);
% plot(t,f,'*')
% hold on
% plot([0,3],polyval(p,[0,3]))


% % Peter
% t=0:2:12;
% f=[3 4 5 5.5 6.5  7 8 ];
% plot(t,f,'*')
% axis([min(t)-1,max(t)+1 min(f)-1 max(f)+1])
% p=polyfit(t,f,1);
% hold on
% plot([-1,13],polyval(p,[-1,13]))

% fprintf(1,"t=20 -> %fcm\n",polyval(p,20))
% fprintf(1,"t=%f -> 0cm\n",-p(2)/p(1))
% fprintf(1,"t=%f -> tele\n",(50-p(2))/p(1))


% % zsakos
% t=[1 15 20 28];
% f=[ 980 605 470 250 ];
% plot(t,f,'*')
% p=polyfit(t,f,1);
% hold on
% plot([0,29],polyval(p,[0,29]))
% axis([min(t)-2,max(t)+2 min(f)-2 max(f)+2])
% fprintf(1,"t=0 -> %f kg\n",polyval(p,0))
% fprintf(1,"t=%f -> ures\n",-p(2)/p(1))


% % format rat
% t=[2.1 2.2 2.3 2.3 2.5 2.6 2.8 2.9];
% f=[2.5 2 2.5 2.7 3 4 5.4 7];
% plot(t,f,'d')
% p=polyfit(t,f,2)
% hold on
% xx=linspace(2,3);
% plot(xx,polyval(p,xx))
% axis([min(t)-1,max(t)+1 min(f)-1 max(f)+1])


% t=(0:0.5:2.5)';
% f=[1 -2 -2.5 -0.5 1.25 -1.5]';
% plot(t,f,'*')
% hold on
% A=[ones(size(t)), cos(pi*t), sin(pi*t)];
% x=(A'*A)\(A'*f);

% F=@(t) x(1)+x(2)*cos(pi*t)+x(3)*sin(pi*t);

% xx=linspace(0,2.5);
% plot(xx,F(xx))
% axis([min(t)-0.5,max(t)+0.5 min(f)-1 max(f)+1])


% t=[0 0.5 2 2.5]';
% f=[1 -2 1.25 -1.5]';
% plot(t,f,'*')
% hold on
% A=[ones(size(t)), cos(pi*t)]%, sin(pi*t)];
% x=(A'*A)\(A'*f);
% F=@(t) x(1)+x(2)*cos(pi*t);%+x(3)*sin(pi*t);
% xx=linspace(0,2.5);
% plot(xx,F(xx))
% axis([min(t)-0.5,max(t)+0.5 min(f)-1 max(f)+1])


% % lsqnonlin
% t=[15    46    74    105    135    166    196    227    258    288   319   349]';
% f=[-1.7   0.1   5.2   10.3   15.8   18.9   21.1   20.3   16.1   10.2    4.2    0.5]';
% plot(t,f,'*');
% hold on;
% F=@(x) x(1)+x(2)*cos(2*pi*(t-x(3))/365);
% g=@(x) F(x)-f ;
% x0=[1 1 14]';
% xo=lsqnonlin(g,x0);
% Fo=@(t) xo(1)+xo(2)*cos(2*pi*(t-xo(3))/365);
% xx=linspace(14,350,200);
% plot(xx,Fo(xx))
% hiba=sum((Fo(t)-f).^2)

% A=[ones(size(t)) cos(2*pi*(t-14)/365)];
% xo2=(A'*A)\(A'*f);
% Fo2=@(t) xo(1)+xo(2)*cos(2*pi*(t-14)/365);
% hiba2=sum((Fo2(t)-f).^2)



% finomitas
d=dlmread("annpop.csv");
[r,c]=size(d);
f1=d(1:100,2);
t1=(1:100)'/10;

plot(t1,f1,'.')
hold on
p1=polyfit(t1,f1,1);

f2=d(1:200,2);
t2=(1:200)'/10;
plot(t2,f2,'.')

p2=polyfit(t2,f2,2);

f3=d(1:300,2);
t3=(1:300)'/10;
plot(t3,f3,'.')

xx=linspace(0.1,30);
plot(xx,polyval(p2,xx))
plot([0.1 30],polyval(p1,[0.1 30]))

lf3=log(f3);
p3=polyfit(t3,lf3,1);
p3=[p3(1) exp(p3(2))];
plot(xx, p3(2)*exp(p3(1)*xx),'k')

