LSQcurvefit does not yield same result for comparable data sets

I have used the following code to fit my data. Key is that the fit captures the peak at the start of the curve.
For a lot of data sets, the method works (example are x_good,y_good).
For two sets this method does not work adequate enough (x5, y5 and x10, y10)
Anyone who can see if I am doing something wrong? Or give a different method which works better?
clear all
clc
close all
% Working data set
x_good = [0 0.0025 0.020666667 0.0915 0.129 0.169916667 0.204 0.2415 0.299833333 0.359833333 0.409833333 0.459833333 0.589833333 0.674833333];
y_good = [0.000001 -1.791689626 -2.085814283 -1.254192484 -1.130978553 -0.852663995 -0.650156083 -0.605732792 -0.552092535 -0.466707459 -0.381595899 -0.341840176 -0.112360934 -0.107089368];
%Not working data set
x5 = [0 0.0025 0.020666667 0.0915 0.129 0.169916667 0.204 0.299833333 0.359833333 0.509833333 0.589833333 0.674833333];
y5 = [0.000001 -1.512917459 -1.397221246 -0.861543826 -0.678138048 -0.538930943 -0.440640054 -0.253865251 -0.192352332 0.001791084 0.078138742 0.156925367];
%Not working data set
x10 = [0 0.0025 0.020666667 0.0915 0.129 0.169916667 0.204 0.2415 0.299833333 0.359833333 0.409833333 0.459833333 0.589833333 0.674833333];
y10 = [0.000001 -0.909146404 -1.389416499 -0.736431181 -0.767464076 -0.430784784 -0.298350016 -0.477736703 -0.174485909 -0.10975744 -0.038531763 0.009471926 0.133414928 0.178482903];
% LSQ
t = x5;
y =y10;
xspace = linspace(t(1), t(end), 1000);
options = optimoptions('lsqcurvefit', 'MaxFunctionEvaluations', 100e3)
% fit function y = c(1)*exp(-lam(1)*t) + c(2)*exp(-lam(2)*t)
F = @(x,t)(x(1)*exp(-x(2)*t) + x(3)*exp(-x(4)*t));
x4 = [1 1 1 0];
[x,resnorm,~,~,output] = lsqcurvefit(F,x4,t,y, [], [], options)
figure
plot(t,y,'ro')
title("Least squared method ")
hold on
plot(t,F(x,t), 'r')
hold on
plot(xspace, F(x, xspace), '--r')
set(gca, 'YDir','reverse')
legend("data points", "LSQ, resnorm: " + resnorm, "LSQ continous")

 Akzeptierte Antwort

Use
x4 = [-2.1782 5.0283 2.1782 720.8491];
instead of
x4 = [1 1 1 0];
as initial guess for the parameters.

2 Kommentare

Thank you! It works!
Can you tell me how I can best guess x4? Or how you found these values?
From the fit results of y_good against x_good :-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by