function fit_values=iterative_curve_fit_singledecay_ampanmda(recorded_waveform)

seed_vals=[10 5 100 0 0];   %amplitude_scaler,tau_rise,tau_decay1,y_offset,latency -- time values are in *samples* here
lower_bounds=[4 3 6 -1 0];
latency_limit=1;
upper_bounds=[1000 100 1500 1 latency_limit];
fittingz_this_trial=[];
optionz=optimset('display','off');
[best_fitted_vals,best_resnorm,best_residual]=lsqcurvefit(@lsqfit_template_generator2,seed_vals,[1:length(recorded_waveform)],recorded_waveform',lower_bounds,upper_bounds,optionz);

% for latency_value=1:1:latency_limit
%     [fitted_vals,resnorm,residual]=lsqcurvefit(@lsqfit_template_generator2,[seed_vals(1:4) latency_value],[1:length(trace)],trace,[lower_bounds(1:4) latency_value-1],[upper_bounds(1:4) latency_value+1],optionz);
%     if resnorm<best_resnorm,best_fitted_vals=fitted_vals;best_residual=residual;best_resnorm=resnorm;end
% end
template=lsqfit_template_generator2([best_fitted_vals],[1:length(recorded_waveform)]);
%     figure,plot(trace,'k','linewidth',3),hold on, plot(template,'r','linewidth',3),plot(abs(best_residual),'o','markersize',10,'markerfacecolor','m','markeredgecolor','m')
fitted_PSC=min(template)-best_fitted_vals(4);
fitted_charge=sum(template-best_fitted_vals(4));
fitted_values=[best_fitted_vals';fitted_PSC;fitted_charge;best_resnorm];
template=[template fitted_values'];
fit_values=template;