function fresult = sampleproblem4(tmax,initial_guess); % fresult = sampleproblem4(tmax,initial_guess); % % add noise to f1 and try to recover parameters in expression % f1(t) = 15exp(-t)-5exp(-5t) % f1(t) = Aexp(Bt)+Cexp(Dt) % % initial_guess = [A,B,C,D]; % tmax = length of time interval of integration for input f1 data % %EXAMPLES %fresult = sampleproblem4(5,[15,-1,-5,-5]); %good guess...spot on %fresult = sampleproblem4(5,[10,-1,+1,-10]); %good guess...qualitatively similar to the plot %fresult = sampleproblem4(5,[1,-1,1,-5]); %bad guess...garbage! %fresult = sampleproblem4(5,[15,0,-5,-3]); %bad guess %fresult = sampleproblem4(5,[15,0,-5,-3]); %bad guess % %Try other guesses as well %It is critical for convergence that the initial guess is good % %If B = D during the iterative process, then the two expressions %Aexp(Bt) and Cexp(Dt) are not linearly independent--this means that the %there are an infinite number of choices for A and C to get the same output %(this leads to fit failure) % %Be able to recognize garbage! %If the fit of the two decaying rates (in B and D) are close together, %it is like using a SINGLE exponential to fit the data % [t,f,fa] = sampleproblem1(tmax); f1 = f(:,1); f1noisy = f1 + 0.1*randn(size(f1)); model = fittype('A*exp(B*t)+C*exp(D*t)','ind','t'); %initial_guess = [15,-1,-5,-5]; options = fitoptions(model); set(options,'StartPoint',initial_guess); fresult = fit(t,f1noisy,model,options); figure(1); plot(t,f1,t,f1noisy,t,fresult(t)); legend('f1','noisy f1','model fit'); fresult return;