# How to fit curves with two variables to equation?

6 views (last 30 days)
Amadeus Wolf on 18 Jan 2023
Commented: Amadeus Wolf on 20 Jan 2023
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,

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.
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.

### Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by