向量在MATLAB中的曲线拟合中必须是相同的长度误差

发布于 2025-02-04 04:45:57 字数 676 浏览 2 评论 0原文

我在曲线上遇到问题,使我的随机数据适合函数

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

enter image description here

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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2025-02-11 04:45:57

首先,对您的问题进行了一些调整:

  • 您不是在尝试 curve fitting 。您正在尝试做的(我认为)是覆盖a 概率密度函数直方图通过从相同分布中获取随机点(具有参数的正态分布(MU,Sigma))获得。这两条曲线确实应该覆盖,因为它们代表同一件事,只有一个是分析性的,另一个是数字获得的。

  • hist document 不建议使用,您应该使用直方图

第一步:生成随机数据

知道分布是正态分布,我们可以使用 MATLAB的随机函数做到这一点:

N = 150;
rng('default') % For reproducibility
mu = 5;
sigma = 2;
r = random('Normal',mu,sigma,N,1);

第二步:绘制直方图,

因为我们不仅需要每个bin中的元素计数,还需要一种概率密度函数的感觉,我们可以使用'归一化' 'pdf'参数

Nbins = 25;
f=histogram(r,Nbins,'Normalization','pdf');
hold on

在这里我宁愿指定多个垃圾箱,也不愿意本身指定垃圾箱,因为您永远不会预先知道 距离您的数据的平均值有多远。

最后一步:覆盖直方图上的概率密度函数

已与概率密度函数一致的直方图,这足以覆盖密度函数:

x_ = linspace(min(r),max(r),100);
y_ = (1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); 
plot(x_,y_,'b-');

使用n = 150

< a href =“ https://i.sstatic.net/ubbw4.png” rel =“ nofollow noreferrer”>

使用n = 1500

”

n = 150.000nbins = 50

如果出于某种晦涩的原因,您要使用旧的hist()函数,

旧的hist()函数无法处理归一化,因此您必须手工做到这一点适合您的直方图的功能:

N = 1500;
% rng('default') % For reproducibility
mu = 5;
sigma = 2;
r = random('Normal',mu,sigma,1,N);
Nbins = 50;
[~,centers]=hist(r,Nbins);
hist(r,Nbins); hold on

% Width of bins
Widths = diff(centers);

x_ = linspace(min(r),max(r),100);
y_ = N*mean(Widths)*(1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); 
plot(x_,y_,'r-');

“

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 instead

First step: Generating your random data

Knowing the distribution is the Normal distribution, we can use MATLAB's random function to do that :

N = 150;
rng('default') % For reproducibility
mu = 5;
sigma = 2;
r = random('Normal',mu,sigma,N,1);

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' arguments

Nbins = 25;
f=histogram(r,Nbins,'Normalization','pdf');
hold on

Here 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.

Histogram plot

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:

x_ = linspace(min(r),max(r),100);
y_ = (1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); 
plot(x_,y_,'b-');

With N = 150

N = 150

With N = 1500

N = 1500

With N = 150.000 and Nbins = 50

Last one

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:

N = 1500;
% rng('default') % For reproducibility
mu = 5;
sigma = 2;
r = random('Normal',mu,sigma,1,N);
Nbins = 50;
[~,centers]=hist(r,Nbins);
hist(r,Nbins); hold on

% Width of bins
Widths = diff(centers);

x_ = linspace(min(r),max(r),100);
y_ = N*mean(Widths)*(1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); 
plot(x_,y_,'r-');

BadHist

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文