MATLAB: Curve fit exactly the function I enter

cftool

I have some data points and a known function with 4 parameters. But when I tried to use the 'cftool', part of the data points aren't fitting the curve. The function is 2 exponential terms but I enter it as customized function.

Best Answer

So why do you think that a sum of two exponentials MUST fit this data? Looking at the data, that hardly seems a good conclusion.
However, if also seems that it is still very probable that the curve fitting process has not converged to a viable solution, because of several reasons.
1. The starting values used were poor. This is almost always the case for exponential models, and especially sums of exponentials. They are already problem children of the curve fitting world. So first, you MAY need to learn how to provide intelligent starting values to the curve fitting toolbox. That may well mean you need to learn how to gain a good set of starting values.
2. The curve that you show is a classically poor one for curve fitting, because much of the data lies on a vertical line. That means that regression techniques will tend to fail miserably, UNLESS you can provide incredibly better starting values than the random ones that fit will use.
3. The curve that you show is NOT one that looks terribly like the sum of two exponentials. That is to say, it exhibits significant lack of fit. So I will seriously question why it is that you think this is a good choice of model.
4. I note that you have created the variable B1 using a log transform, but then never use it.
Option # 3 above is, I feel, the most telling here. AN exponential model will show a smoothly rising shape. But an exponential model will not have a curve shape that is asymptotic to a straight line, as you seem to have here.
So the lack of fit that you see here is symptomatic, indicating that you have very possibly chosen the wrong model form. Or it may simply be an artifact of the plot that you used, because you created B1, but never actually use that variable.
Just wanting a specific model to fit any set of data is not sufficient. You need to make an intelligent choice of model. One way that I can help here is if you provide the data itself, as an attachment. Just use the paper clip to attach a .mat file that has the data in it. Then I might be able to make some suggestions as to a better choice of model, or how to choose better starting values. That may involve a good choice of how to better transform your data, BEFORE the curve fit.
So if you seriously want help, then you need to provide the data. As I said, just save A and B into a single .mat file. Then attach it to a comment, or to your original question.