How to fit curves with two variables to equation?

21 views (last 30 days)
Hello Matlab-Community,
I am struggling quite a while with the problem of getting a formula out of 3 graphs I obtained from simulation.
Below you see the quantity k over normalized concentration for three different velocities.
The velocities vary in the range of 0.001, 0.01 and 0.1.
Using the fit function in matlab, I get a good approximation for the respective curves regarding k in dependency of c/c0.
f1=fit(c/c0,k,fourier5)
Now, I want to express k as a function of c/c0 and v. k = f(c/c0,v)
I checked several threads but could not manage to obtain an equation.
My problem is also that the three curves are built up of different sized matrices (165x3 double, 80x3 double and 83x3 double) with different interval size.
Do you have any hints?
Best Rgards,
Amadeus

Answers (1)

William Rose
William Rose on 18 Jan 2023
Edited: William Rose on 18 Jan 2023
@Amadeus Wolf, please post the data (the three matrices) which you are trying to fit: k and c/c0 and v.
[edit: Remove reference to fmincon(). Add mention of custon function as a fitType. Fix typos.]
On the plot, I recommend distinguishing between the data and the fit.
If you want to use the fit function, then you are fitting a surrface: k is a function of two variables, c/c0 and k. Therefore you cannot use the fourier5 model, which is for functions of one variable only. You can use lowess or loess or polymn models, where m=1 to 5 and n=1 to 5:
f1=fit([c/c0,v],k,fitType)
where fitType is replaced with loess, lowess, poly11, poly 21, poly12, ..., or poly55.
The diffrent array sizes are not a problem. You can combine your three arrays into a single array with size 245x3, whose columns correspond to c/c0, k, and v.
If none of these fitTypes give a good fit to your three curves, you may have to make your own custom fitType function, and still use fit() to fit it. See here.
  11 Comments
William Rose
William Rose on 20 Jan 2023
I took your file k_c_v_AW.txt and I removed the values that wer obtaibed by Fourier5 extrapolation, because I don;t want to fit data that came from another fit. I only want to fit data from a simulation or from an experiment. The modified file is k_c_v_AWne.txt. (ne for no extraploated data.)
I fitted it with the same model as before, but with a different starting point. The diferent starting point was necessary because the k values and the v values are different by at least an order of magnitude from the values in the initial posted plot.
This resulted in a reasonable custom function fit with four adjustable parameters. The console output is below.
>> kcvFit
Fit 1 (poly14 ): rmse=4.30e-05.
Fit 2 (custom1): rmse=1.54e-05.
Fit 2 parameters: a=1.00147, b=4.09e-05, c=1.71e-04, d=8.25e-03
The plots produced are below.
The data file and script are attached.
Amadeus Wolf
Amadeus Wolf on 20 Jan 2023
thanks for your effort and the code you provided, I really appriciate it.
Regarding the function not working for the surface fit, you answered:
I suspect that it happens because the dot operator is not used in the necessary positions.
That's right. The code is
g2 = fittype(@(a0, a1, a2, b1, x, y) a0./(1+a1*x+a2*x.^2)+b1*y, ...
'independent', {'x', 'y'},'dependent', 'z' );
f1=fit([cc0,v],k,g2)
Forget the files cc0_k_v1_fake.txt, etc., only the k_c_v_AW.txt matters ;-)
Regarding the code you provided, it does the job pretty well. Thanks.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!