Curve fitting using different functions

I have a set of data consisting of x and y values for five different cases, where y varies while x remains constant. I have tried using polyfit to obtain polynomial equations for each set of data. However, I want to try fitting the data with other types of functions such as exponential, trigonometric, logarithmic, polynomial and power functions. After trying these different functions, I want to compute the R^2 values and use them to determine the best fit for each set of data.
x = [0.25 0.50 0.75 1 1.25 1.50 1.75 2 2.25 2..50 2.75 3]
y1 = [0.001524 0.00605 0.013592 0.024151 0.037728 0.054321 0.073921 0.096514 0.122102 0.150689 0.182278 0.21687]
y2 = [0.060598 0.14902 0.28793 0.42474 0.57648 0.78663 1.0389 1.6032 2.3667 3.1328 3.8971 4.6297]
y3 = [0.016373 0.0503 0.1896 0.60113 1.2101 1.8134 2.9318 4.0203 4.8728 6.1467 8.1357 10.277]
y4 = [0.11668 0.33853 0.66617 1.2037 1.6292 2.4379 3.6119 4.8274 6.0769 6.4846 8.064 9.6733]
y5 = [0.131518 0.418614 0.793038 1.33235 1.94051 2.54087 4.31947 5.25463 6.33347 7.82779 9.91558 12.4864]
I would like separate equations for all five cases of "y," but also a single equation for all five cases of "y" and "x," taking into account another dimensionless parameter (provided below). Afterwards, using R^2 to determine the best fit, define the best fit functions
y1=0
y2=0.5
y3=1
y4=2
y5=2.5
I can do this easily in excel but I want to do it in matlab. Thanks in advance

Respuestas (1)

Duncan Carlsmith
Duncan Carlsmith el 29 de Abr. de 2023
You are asking about nonlinear fitting and several choices are available including nlinfit. Try can try using Curve Fitter, from the Apps tab. Select for data your x and say y1. The functions you mention are available options. Custom ones are also possible. Pick say exponential and you will see the R^2 at the lower right. Export code and you get the attached as a template for nonlinear fitting. The R^2 value will be found in returns fitresult and gof. You could just build a set of five functions to perform fits for each functional form and then just called them with different y values. Be aware though that nonlinear fitting requires input parameter guesses so such fits can fail. Also, your functions can have different numbers of parameters, e.g. any number for generic polynomial fits. The number of degrees of freedom is important in interpreting R^2.
function [fitresult, gof] = createFit1(x, y1)
%CREATEFIT1(X,Y1)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input: x
% Y Output: y1
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 28-Apr-2023 19:27:20
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( x, y1 );
% Set up fittype and options.
ft = fittype( 'exp1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.00698176628929166 1.20905013311468];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y1 vs. x', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y1', 'Interpreter', 'none' );
grid on

5 comentarios

Raj Arora
Raj Arora el 29 de Abr. de 2023
Thanks Duncan for your prompt reply. I know about about the curve fitter app, but there I think we cannot find a single equation for all 5 cases with 2 variables (x,y1) and another dimensionless parameter that is (0, 0.5, 1, 1.5, 2).
Inshort I want to fit best possible function for a single equation considering 2 variable and third dimensionless parameter. If you can throw some light on this issue it would be really helpful.
Duncan Carlsmith
Duncan Carlsmith el 29 de Abr. de 2023
Assuming all the y "measurements" are independent, just fit ytot=[y1,y2,y3,y4,y5] xtot=[x,x,x,x,x] to whatever function with whatever variables and parameters. I hope that is what you mean.
Raj Arora
Raj Arora el 29 de Abr. de 2023
x = [0.25 0.50 0.75 1 1.25 1.50 1.75 2 2.25 2..50 2.75 3]
y1 = [0.001524 0.00605 0.013592 0.024151 0.037728 0.054321 0.073921 0.096514 0.122102 0.150689 0.182278 0.21687]
y2 = [0.060598 0.14902 0.28793 0.42474 0.57648 0.78663 1.0389 1.6032 2.3667 3.1328 3.8971 4.6297]
y3 = [0.016373 0.0503 0.1896 0.60113 1.2101 1.8134 2.9318 4.0203 4.8728 6.1467 8.1357 10.277]
y4 = [0.11668 0.33853 0.66617 1.2037 1.6292 2.4379 3.6119 4.8274 6.0769 6.4846 8.064 9.6733]
y5 = [0.131518 0.418614 0.793038 1.33235 1.94051 2.54087 4.31947 5.25463 6.33347 7.82779 9.91558 12.4864]
z1=0
z2=0.5
z3=1
z4=2
z5=2.5
I want a single equation that represent all five cases {(x,y1,z1); (x,y2,z2); (x,y3,z3); (x,y4,z4); (x,y5,z5)}, where z is a dimensionless parameter?
for (x & y) it is pretty straightforward but how should I incoperate z (dimensionless parameter having one value for one case) to find a single equation
Duncan Carlsmith
Duncan Carlsmith el 29 de Abr. de 2023
I don't understand and can't help you anymore. You will have to be more clear in asking questions.
Raj Arora
Raj Arora el 29 de Abr. de 2023
Anyways thankyou.

Iniciar sesión para comentar.

Categorías

Más información sobre Linear and Nonlinear Regression en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 28 de Abr. de 2023

Comentada:

el 29 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by