向量在MATLAB中的曲线拟合中必须是相同的长度误差
我在曲线上遇到问题,使我的随机数据适合函数
N = 100;
mu = 5; stdev = 2;
x = mu+stdev*randn(N,1);
bin=mu-6*stdev:0.5:mu+6*stdev;
f=hist(x,bin);
plot(bin,f,'bo'); hold on;
x_ = x(1):0.1:x(end);
y_ = (1./sqrt(8.*pi)).*exp(-((x_-mu).^2)./8);
plot(x_,y_,'b-'); hold on;
Error using plot
Vectors must be the same length.
并且已知标准偏差。
图:
I'm having problems in curve fitting my randomized data for the function
Here is my code
N = 100;
mu = 5; stdev = 2;
x = mu+stdev*randn(N,1);
bin=mu-6*stdev:0.5:mu+6*stdev;
f=hist(x,bin);
plot(bin,f,'bo'); hold on;
x_ = x(1):0.1:x(end);
y_ = (1./sqrt(8.*pi)).*exp(-((x_-mu).^2)./8);
plot(x_,y_,'b-'); hold on;
It seems like I'm having vector size problems since it is giving me the error
Error using plot
Vectors must be the same length.
Note that I simplified y_ since mu and the standard deviation is known.
Plot:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,对您的问题进行了一些调整:
您不是在尝试 curve fitting 。您正在尝试做的(我认为)是覆盖a 概率密度函数直方图通过从相同分布中获取随机点(具有参数的正态分布
(MU,Sigma)
)获得。这两条曲线确实应该覆盖,因为它们代表同一件事,只有一个是分析性的,另一个是数字获得的。如 hist document 不建议使用,您应该使用直方图
第一步:生成随机数据
知道分布是正态分布,我们可以使用 MATLAB的随机函数做到这一点:
第二步:绘制直方图,
因为我们不仅需要每个bin中的元素计数,还需要一种概率密度函数的感觉,我们可以使用
'归一化'
'pdf'
参数在这里我宁愿指定多个垃圾箱,也不愿意本身指定垃圾箱,因为您永远不会预先知道 距离您的数据的平均值有多远。
最后一步:覆盖直方图上的概率密度函数
已与概率密度函数一致的直方图,这足以覆盖密度函数:
使用
n = 150
< a href =“ https://i.sstatic.net/ubbw4.png” rel =“ nofollow noreferrer”>
使用
n = 1500
n = 150.000
和nbins = 50
如果出于某种晦涩的原因,您要使用旧的hist()函数,
旧的
hist()
函数无法处理归一化,因此您必须手工做到这一点适合您的直方图的功能:Well first of all some adjustments to your question:
You are not trying to do curve fitting. What you are trying to do (in my opinion) is to overlay a probability density function on an histogram obtained by taking random points from the same distribution (A normal distribution with parameters
(mu,sigma)
). These two curve should indeed overlay, as they represent the same thing, only one is analytical and the other one is obtained numerically.As seen in the hist documentation,
hist
is not recommended and you should use histogram insteadFirst step: Generating your random data
Knowing the distribution is the Normal distribution, we can use MATLAB's random function to do that :
Second step: Plot the histogram
Because we don't just want a count of the elements in each bin, but a feel of the probability density function, we can use the
'Normalization'
'pdf'
argumentsHere I'd rather specify a number of bins than specifying the bins themselves, because you never know in advance how far from the mean your data is going to be.
Last step: overlay the probability density function over the histogram
The histogram being already consistent with a probability density function, it is sufficient to just overlay the density function:
With
N = 150
With
N = 1500
With
N = 150.000
andNbins = 50
If for some obscure reason you want to use old hist() function
The old
hist()
function can't handle normalization, so you'll have to do it by hand, by normalizing your density function to fit your histogram: