To perform cur fitting for the arrays that you have provided, you can utilize the ‘fit’ function in MATLAB. The ‘fit’ function creates the fit to the data in x and y with the model specified by ‘fitType’. The ‘fitType’ can be a character vector, string scalar, string array, cell array of character vectors, anonymous function, or a ‘fittype’ object created by the ‘fittype’ function.
The following code snippets can be added to your script to perform the curve fitting:
- Transpose the data because the fitting functions require input data as column vectors:
- Use the fit function to perform curve fitting by specifying the desired fitType, example: 'poly2':
fit_y1 = fit(x, y1_mean, 'poly2');
fit_y2 = fit(x, y2_mean, 'poly2');
- Plot both the data points and the fitted curve. The same procedure can be applied to y2:
plot(x, y1_mean, 'bo', 'DisplayName', 'y1 Data');
x_fit = linspace(min(x), max(x), 100)';
plot (x_fit, fit_y1(x_fit), 'r-', 'DisplayName', 'y1 Fit');
The following is the received output:
To know more about the ‘fit’ function, you can refer to the following documentation:
I hope this helps!