Plotting a curve with just x values

4 ビュー (過去 30 日間)
Amir mo
Amir mo 2021 年 2 月 4 日
編集済み: dpb 2021 年 2 月 6 日
I want to fit a curve to a vector of x values that I have. I have a vector of x values which contains the frequency of occurence of each value. I mean if the vector is like this:
[3,3,4,5], it means that the height over x = 3 must be double than over x = 4 and x = 5. Now I want to plot the best curve fitting this vector.
My idea was to sort the vector increasingly and count the number of occurrences for each x value and plot the curve but even then I don't know how to make it a continous function.
Thanks!
  3 件のコメント
Amir mo
Amir mo 2021 年 2 月 4 日
suppose we have this vector [0,0,1,2,3,2,-1,2] what i want to plot is a curve like this. The point is that I have the values for a limited range (many points but still limited) but i want to predict the function over the whole x axis
dpb
dpb 2021 年 2 月 4 日
"i want to predict the function...over the whole x axis"
What does that mean, specifically? What do you expect a functional form to be? Extrapolation is an extremely risky proposition.

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

採用された回答

dpb
dpb 2021 年 2 月 4 日
編集済み: dpb 2021 年 2 月 6 日
Something like
x=[3,3,4,5];
n=histc(x,unique(x,'stable'));
v=[1:numel(n)];
m=fitlm(v,n);
bar(n);
xlim(xlim+[1 -1]*0.5)
hold on
line(v,feval(mdl,v),'color','r')
set(get(gca,'YAxis'),'TickLabelFormat','%.1f')
maybe?
v=[0,0,1,2,3,2,-1,2];
x=unique(v);
n=histc(v,x);
stem(x,n)
xlim([x(1) x(end)]+[-0.25 0.25])
ylim([0 max(n)+0.1])
xticks(x)
yticks([0:max(n)])
results in
This idea of predicting this outside the range would appear to be a task for the Crystal Ball Toolbox that has yet to be released.
ADDENDUM: Mayhaps I was reading the original Q? too literally on the expectations...what about
xq=linspace(x(1),x(end));
ns=interp1(x,n,xq,'spline');
nc=interp1(x,n,xq,'pchip');
hold on
hL=plot(xq,ns,'-r',xq,nc,'-c');
ylim([0 ceil(max(nn))+0.1])
yticks([0:ceil(max(nn))])
legend('Data','Spline Interpolant','pchip Interpolant')
which results in
?
  1 件のコメント
Amir mo
Amir mo 2021 年 2 月 5 日
Thanks a lot!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFit Postprocessing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by