Curve fitting using different functions
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponses (1)
Duncan Carlsmith
le 29 Avr 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 commentaires
Duncan Carlsmith
le 29 Avr 2023
I don't understand and can't help you anymore. You will have to be more clear in asking questions.
Voir également
Catégories
En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!