% tabela sadrzi u prvoj koloni redni broj dana kada je merena koncentracija
% raka, a u drugoj koloni koncentraciju raka izmerenu tog dana

tabela=[1,1.53471436623816
2,2.58021682959233
5,3.06805293513362
6,3.61361696961339
7,3.85439389259151
8,4.07753744390572
9,4.28358656186063
12,4.47733681447821
13,4.56954300834494
14,4.56954300834494
15,4.66343909411207
16,4.74493212836325
19,4.99043258677874
20,5.06890420222023
21,5.06890420222023
22,5.14749447681345
23,5.22035582507832
26,5.36597601502185
27,5.36597601502185
28,5.36597601502185
29,5.36597601502185
30,5.36597601502185
33,5.50125821054473
34,5.29330482472449
35,5.29330482472449
36,5.36597601502185
40,5.36597601502185
41,5.29330482472449
42,5.36597601502185
43,5.36597601502185
44,5.50125821054473
47,5.50125821054473
48,5.50125821054473
49,5.43372200355424
50,5.36597601502185
51,5.29330482472449
54,5.29330482472449
55,5.22035582507832
56,5.36597601502185
57,5.06890420222023
58,5.29330482472449
62,5.22035582507832
63,5.29330482472449
64,5.14749447681345
65,5.22035582507832
68,5.36597601502185
69,5.29330482472449
70,5.29330482472449
71,5.14749447681345
72,5.06890420222023
75,4.99043258677874
76,5.22035582507832
77,5.22035582507832
78,5.22035582507832
79,5.36597601502185];

dan=tabela(:,1)';
volume=tabela(:,2)';
plot(dan, volume,'o')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Trazimo model (krivu) koja najbolje opisuje date podatke
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%proba 1 (eksponencijalni)
F1=@(x,xdata) x(1)*exp(-x(2)*xdata);
[X1, greska1] = lsqcurvefit(F1,[1 1],dan,volume)
%plot(dan, volume,'o',dan,F1(X1,dan))

%proba 2 (logisticki)
F2=@(x,xdata) x(1)./(1+(x(1)/x(2)-1)*exp(-x(3)*xdata));
[X2,greska2] = lsqcurvefit(F2,[5 1 1],dan,volume)
%plot(dan, volume,'o',dan,F1(X1,dan), dan, F2(X2,dan))

% proba 3 (Gompertz)
F3=@(x,xdata) x(1)*exp(x(2)/x(3)*(1-exp(-x(3)*xdata)));
[X3, greska3] = lsqcurvefit(F3,[1.5 1 1],dan,volume)

% Gompertz ima najmanju vrednost greske --> najbolje opisuje podatke

figure(1)
plot(dan, volume,'o',dan,F1(X1,dan), dan, F2(X2,dan), dan, F3(X3,dan))
legend('data set','eksponencijalna', 'logisticka', 'Gompertz')
xlabel('vreme')
ylabel('koncentracija tumora')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Mozemo fitovati i prirastaj
% Trazimo koja funkcija za p(t) najbolje aproksimira prirastaj
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

p=diff(volume);
dan=dan(1:end-1);

%proba 1 (konstanta)
P1=@(x,pdata) x*pdata.^0;
[Y1, greska1] = lsqcurvefit(P1,1,dan,p)

%proba 2 (linearna funkcija)
P2=@(x,pdata) x(1)*pdata+x(2);
[Y2, greska2] = lsqcurvefit(P2,[1 1],dan,p)

%proba 3 (eksponencijalna (Gompertz) funkcija)
P3=@(x,pdata) x(1)*exp(-x(2)*pdata);
[Y3, greska3] = lsqcurvefit(P3,[1 1],dan,p)

figure(2)
plot(dan, p,'o',dan,P1(Y1,dan), dan, P2(Y2,dan), dan, P3(Y3,dan))
legend('data set','konstanta p=c','prava p=ax+b', 'p=eksponencijalna')
xlabel('vreme')
ylabel('prirastaj tumora')
