在matlab中使用normrand进行高斯分布是否成立

问题描述 投票:0回答:1

我想在Matlab中编写简单的线性回归,我编写了这段代码。这是工作,但我对此感觉不对!我认为出了点问题。如果您介意的话,请看一下;


clc,clear

h1 = normrnd(1:10,1);
P_re = normrnd(1:10,1);

n = numel(h1);

y = h1.*P_re;
y1 = sum(y);

sumH = sum(h1);
sumP = sum(P_re);

sumh_2 = sum((h1).^2);
sumh2 = (sum(h1))^2;

beta1 = ((n*y1) - sumH*sumP)/(n*(sumh_2)-(sumh2));
beta0 = mean(P_re) - beta1*mean(h1);

pRe = beta1*h1 + beta0;

plot(h1,P_re,'o')
hold on
plot(pRe,h1)
matlab linear-regression
1个回答
0
投票

您可以简单地使用polyfit来获得阶1的多项式来拟合两个回归系数:polyfit。>>

此外,您可能指的是beta = polyfit(h1, pRe, 1)而不是plot(h1, pRe)

因此,完整的代码变为:

plot(h1, pRe)

clc,clear h1 = normrnd(1:10,1); % x data P_re = normrnd(1:10,1); % y data beta = polyfit(h1, P_re, 1); % fit polynomial of order 1 pRe = beta(1)*h1 + beta(2); % or pRe = polyval(beta, h1) plot(h1,P_re,'o') % plot data hold on plot(h1,pRe) % plot fitted line

© www.soinside.com 2019 - 2024. All rights reserved.