# How can I create a curve that will fit my data?

1 ビュー (過去 30 日間)
Elham 2022 年 6 月 23 日

The following is my code:(2x2 dataset imported from an excel file, 21 values for y and 21 values for x)
x=dataset1(:,1);
y=dataset1(:,2);
plot(x,y,'Marker','.','MarkerSize',20);
xlabel('Photon Energy(eV)')
ylabel('Harmonic Intensity(a.u.)')
axis square
I am getting this displayed:
But I am trying to plot it like this:
*I tried using the fit and lsqcurvefit but those only gave me best fit lines(unless I used them incorrectly). The closest thing I've got to was inputting S = x(:)\y(:), but that didn't give the same results as the figure.
It's my frist time using MATLAB, let alone programming. Any help would be appreciated.
##### 1 件のコメント表示非表示 なし
Alex Sha 2022 年 6 月 24 日
post out your data file if possible

サインインしてコメントする。

### 採用された回答

Image Analyst 2022 年 6 月 24 日
fitnlm in the Statistics and Machine Learning Toolbox, since 2013b, though you should probably upgrade your antique 8 year old version.
Even if you are working on one curve, you don't have enough data points to get a really good curve. The best you can do (or maybe not the best but ONE thing you can do) is to fit a spline through them with the spline function or interp1 function. Attached is a demo.

サインインしてコメントする。

### その他の回答 (1 件)

Image Analyst 2022 年 6 月 24 日
I can see from what you did plot that there are not enough points in "dataset" to get the three colored curves that you are hoping to get. You'll need a lot more points.
But if you did, do you have a model for your curve? Perhaps an exponential decay times a cosine wave added to a downward ramp?
y = a1 * exp(a2 * x) .* cos(a3 * x) + a4 * x + a5;
You can use fitnlm for that.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
##### 1 件のコメント表示非表示 なし
Elham 2022 年 6 月 24 日
@Image Analyst, I don't have a model for the curve and I'm not sure how I would add one to my code. I tried using fitnlm and I keep getting an error that reads Undefined function 'fitnlm' for input arguments of type 'double'. Do I have to install some type of toolbox? I am using MATLAB R2014a.
Also, I am just working on one of the curves. Once I can get one curve plotted, then I plan on combining the other data into one plot.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by