How to curve fitting for array?

7 Ansichten (letzte 30 Tage)
Ege Tunç
Ege Tunç am 16 Mär. 2019
Bearbeitet: Soumya am 19 Jun. 2025
I have 2 arrays, i've plotted them, now i needed to curve fitting for them. I've checked this page: https://www.mathworks.com/help/curvefit/fit.html
But it wasn't helpful for me.
Here is my code:
U=[10 9.7 9.4 9.1 8.8 8.5 8.2 7.9 7.6 7.3 7.0 6.7 6.4 6.1 5.8 5.5 5.2 4.9 4.6 4.3 4.0];
y1=[0.7 0.75 .8 .85 .9 1.05 1.05 1.1 1.1 1.1 1.15 1.15 1.10 1.1 1.1 1.1 1.1 1.15 1.10 1.15 1.2;0.75 0.75 0.85 0.95 1 1 1.05 1.1 1.1 1.1 1.1 1.15 1.2 1.2 1.25 1.25 1.25 1.3 1.3 1.3 1.35;.7 .75 0.75 0.8 0.85 0.85 0.9 0.95 1 1 1 1 1.05 1.05 1.05 1.1 1.1 1.15 1.15 1.15 1.2];
y2=[1.2 1.3 1.35 1.4 1.4 1.45 1.5 1.55 1.55 1.6 1.6 1.67 1.67 1.75 1.75 1.8 1.9 1.9 2 2.05 2.1; 1.25 1.3 1.4 1.4 1.45 1.45 1.5 1.55 1.6 1.65 1.65 1.7 1.7 1.75 1.85 1.9 1.9 1.95 2 2.05 2.1;1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.5 1.55 1.6 1.6 1.65 1.7 1.7 1.75 1.8 1.85 1.9 1.95 2 2.05];
M1=mean(y1);
M2=mean(y2);
h=6.625.*10.^(-34);
m=9.109.*10.^(-31);
e=1.602.*10.^(-19);
lambda=h./sqrt(2.*m.*e.*U.*1000);
hold on
ylim([0.7 2.1])
xlim([1.22e-11 2.0e-11])
plot(lambda,M1)
plot(lambda,M2)

Antworten (1)

Soumya
Soumya am 19 Jun. 2025
Bearbeitet: Soumya am 19 Jun. 2025
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:
x = lambda';
y1_mean = M1';
y2_mean = M2';
  • 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!

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by