function [Cell i frames S S0]=modelfit1(Cell,Dcoord,um)
%Function to fit first bacteria run. Takes in struct Cell,which contains
%fields 'coord', 'model','v_frame','model_v','norm_model_v'. Dcoord is
%the coordinate where a change in parameter is being introduced. um is the
%ratio of pixels to micrometers, provided by the microscope magnification.

global fps t

%Initialise Parameters
i=1;
fps=27.9;                   %Frames per second

while Cell.coord(i,1)~=Dcoord(1,1) || Cell.coord(i,2)~=Dcoord(1,2) 
    i=i+1;
end;

S=Cell.coord(1:i,:);
S0=S(1,:);
frames=length(S)-1;
t=0:(1/fps):(frames/fps);   %Time points

%Input number of frames manually, could we write the code such that it is
%automatic? 

A0 = [0.1 0.1; 0.1 0.01; 1 1];

[X,resnorm] = lsqcurvefit(@traj1,A0,t,S);

Cell.A{1,1}=X(1,:);
Cell.B{1,1}=X(2,:);
Cell.alpha{1,1}=X(3,1);
Cell.resnorm{1,1}=resnorm;

[Cell.model(1:i,1) Cell.model(1:i,2)]=traj(Cell.A{1,1},Cell.B{1,1},Cell.alpha{1,1},fps,frames,S0);

subplot(2,1,1);
plot(Cell.model(:,1),Cell.model(:,2))
hold on;
plot(Cell.coord(:,1),Cell.coord(:,2),'r+');
hold off;
xlabel('x-displacement'); ylabel('y-displacement'); title('Cell Trajectory')

for n=1:i
    for m=1:2
        Cell.model_v(n,m)=(Cell.B{1,1}(1,m)-Cell.A{1,1}(1,m))*exp(-Cell.alpha{1,1}*(t(n)))+Cell.A{1,1}(1,m);
    end;
    Cell.norm_model_v(n,1)=um*norm(Cell.model_v(n,:));
end;

subplot(2,1,2);
plot(Cell.norm_model_v);
hold on;
plot(Cell.v_frame,'r*');
xlabel('Frame Number'); ylabel('Velocity'); title('Cell Velocity')

%Generates values to be input into traj2.m
V1=Cell.model_v(i,:)
Cell.alpha{1,1}
S1=Cell.model(i,:)