Tspan = linspace(0,30,500);

k=1;
%for c_O2 = [0 0.2:0.1:0.5]
   c_O2 = 2/3 
   [t u] = ode15s('diff', Tspan, [1-c_O2 c_O2 0 0]);
   ntot = sum(u')';
   HCHO(k)    = u(length(Tspan),1)./ntot(length(Tspan),1);
   O2(k)      = u(length(Tspan),2)./ntot(length(Tspan),1);
   HCOOH(k)   = u(length(Tspan),3)./ntot(length(Tspan),1);
   HCOOCH3(k) = u(length(Tspan),4)./ntot(length(Tspan),1);
   k=k+1;
   %end
   

%c) i)
figure(2)
c = [0 0.2:0.1:c_O2];
plot(c,HCHO,c,O2,c,HCOOH,c,HCOOCH3)
xlabel('n_O__2 /n_t_o_t   (%)')
ylabel('n/n_t_o_t')
legend('HCHO','O_2','HCOOH', 'HCOOCH3')
grid

%c) ii)
figure(3)
title('c) ii)')
plot(c,HCOOCH3./HCOOH)
xlabel('n_O__2 /n_t_o_t   (%)')
ylabel('[HCOOCH_3]/[HCOOH]')
grid

%b)
figure(1)
ntot = sum(u')';
U(:,1) = u(:,1)./ntot;
U(:,2) = u(:,2)./ntot;
U(:,3) = u(:,3)./ntot;
U(:,4) = u(:,4)./ntot;
plot(t,U)
xlabel('Tid (min)')
ylabel('Koncentration n/n_t_o_t')
legend('HCHO','O_2','HCOOH', 'HCOOCH3')
grid

%d) i)
figure(2)
K11 = 1./(U(:,1).*U(:,2).^0.5).*gradient(U(:,3),t);
K21 = U(:,1).^(-2).*gradient(U(:,4),t);
plot(t,K11,t,K21)
grid

%d) Minstakvadratmetoden
%k11 = 20.4055  exp(-1.0111e3)
%k22 = 126.3822 exp(-1.6099e3)
%xlabel('Temperatur (K)')
%title('Temperaturberoendet hos hastighetskonstanterna')

T = [200	300	400	500	600];	
k11 = [	0.13	0.70	1.64	2.70	3.77];	
k22 =[	0.04	0.6	2.27	5.00	8.61];	

A = [ones(5,1) 1./T'];
b = log(k22)';
x = A\b
k = exp(x(1))
E = x(2)