# How to curve fit using two functions in two different data segment

49 views (last 30 days)
aroj bhattarai on 7 Apr 2022
Commented: William Rose on 8 Apr 2022
Dear Matlab users,
I have two set of data points, first 200 (y, x3) and second 200 (y, x1) to fit using two non-linear functions, f(x1, x2, x3) and g(x1,x2,x3). The figure is attached below.
Fitting cases: if 0.3 < x3 < 1.0, then a non-linear function f(x1,x2,x3) is to be used to fit the data. Here x1 = x2 = 1/sqrt(x3).
If 1.0 < x1,x2 < 1.7, then a combination of non-linear functions f(x1,x2,x3) + g(x1,x2,x3) is to be used, Here x1 = x2 and x3 = 1/(x1x2) and the function f(x1,x2,x3) is same in both except it takes x1 argument.
I have already fit the two data set separately using lsqcurvefit. However, how could it be possible to combine these two fitting procedure in a single Matlab fit so that f(x1, x2, x3) participates to describe both the data points between 0.3 < x < 1.7 and g(x1, x2, x3) participates only when 1.0 < x < 1.7?
Bhattarai
Matt J on 7 Apr 2022
There is no benefit to combining the fits unless the two pieces of the function have common parameters.

William Rose on 8 Apr 2022
Yes, it is possible and not too difficult. YOu just need to write the error function, or cost function, appropriately. However, the problem a yo have described it is not clear. Please post your data. Please specifiy the functions f(x,y,z) and g(x,y,z). Please be sure to distinguih clearly beteeen the independent variable and the adjustable parameters.
You said points are (y, x3), and points 201-400 are (y, x1). You also said
"a non-linear function f(x1,x2,x3) is to be used to fit the data. Here x1 = x2 = 1/sqrt(x3)"
If both of those statements are true, then there are no adjustable parameters for the f() function: x3 is the independent variable, and you cannot change it. x2 and x1 are defined in terms of x3, according to your statement, so you can;t change them either.
You said you use f() if 0<x1,x2<1.3, and want to fit it with f()+g(). And you said the x1, x2, x3 are the same But in th case of f(x1,x2,x3) there is really only one parameter, as I just described, so it is not clear what can be adjusted for g(x1,x2,x3).
You said "if 0.3 < x3 < 1.0, then a non-linear function f(x1,x2,x3) is to be used to fit the data"
and you said
"if 1.0 < x1,x2 < 1.7, then a combination of non-linear functions f(x1,x2,x3) + g(x1,x2,x3) is to be used"
If x1, x2, x3 are separetely adjustable, then you could satisfy both the first and the second conditions simultaneously. In that case, which function or functions should be used?
Please think about how you can restate the pobem more clearly. Post the specific data you want to fit. Specify the functions f() and g(). Distinguish clearly between independent variables (which you cannot adjust) and parameters (which you can adjust to fit the data).
William Rose on 8 Apr 2022
Compression.m: This is a function. Add comments to specify the dimesions of the inputs (x, lamda) and of the output (F). x() is a vector of parameter: x=[Apar,Bpar]. The function assigns values to F(:,1) and F(:,3), but no value is assigned to F(:,2).
Compression_RUN.m. This is a script. It reads in data from spreadsheet "data.xlsx". It uses column lambda3 to compute columns 1 and 2 of array lambda, assumng incompressibility. The script creates array Esig, with column 3=sigma3 from the spreadsheet, and column 1=zeros. Column 2 of Esig is not defined, but I think it defaults to zero. The script calls lsqcurvefit() to estimate Apar and Bpar to fit the data. lsqcurvefit() calls Compression.m. Compression.m (see above) returns array F that should fit the y-data in array Esig. Array lambda (three columns) contains the x-data used for fitting. Finally, the script uses the best fit values of Apar and Bpar to compute fitted Fsig3.
Tension.m: This is a function. Add comments to specify the dimesions of the inputs (x, lamda) and of the output (F). x() is a vector of model parameters. x(1..6)=[Apar,Bpar, k1MUS,k2MUS,k1SM,k2SM], where k1 and k2 are parameters for a model of the aortic wall. The function assigns values to F(:,1) and F(:,2). No value is assigned to F(:,3). Does the final F() have two columns or three?
Tension_RUN.m: This is a script. It reads in data from spreadsheet "data.xlsx". It uses columns lambda1 and lambda 2 to compute lambda3, assuming incompressibility. The script creates array Esig, with two columns: sigma1 and sigma2 from the spreadsheet. The script calls lsqcurvefit() to estimate parameters x[1..6] to fit the stress data. lsqcurvefit() calls Tension.m. Tension.m (see above) returns array F(:,1:2) that should fit the y-data in array Esig. Array lambda (three columns) contains the x-data used for fitting. Finally, the script uses the best fit values of x[] to compute fitted Fsig1, Fsig2.
• Compression fits the measured sigma3, and the computed sigma1, sigma2. Tension fits the measured values sigma1, sigma2, and not the computed values sigma3. This seems like an inconsistency.
• Compression_RUN and Tension_RUN have sections of code, after the fitting has been done, to compute the best fit values, for comparison to the measured values. I would call Tesion.m and Compression.m, with the best fit values of x, to compute the fitted values. This will assure that the fitted values that are computed will be compted the exact same way after the fit as they were computed during the fitting process. This will assure that, if you make a change in Compression.m or Tension.m, the change will automatically be taken into account when displaying the best fit estimates of stresses.
• @aroj bhattarai, I think that you would like to fit x=[Apar,Bpar,k1MUS,k2MUS,k1SM,k2SM] all at once for the compression and the tension experiments. This is cerainly possible. You will need to write a new main script and a new funciton, which will be called by lsqcurvefit(). It is very doable. If you would like further assistance, please contact me at rosewc@udel.edu.

### Categories

Find more on Stress and Strain in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by