MATLAB 02

对一组数据作线性回归,并绘图

clear all;
% 输入数据x和y
x = [143 145 146 148 149 150 153 154 157 158 159 160 162 164]';
y = [11 13 14 15 16 18 20 21 22 25 26 28 29 31]';
x = [ones(length(x),1),x];
%线性回归
[b,bint,r,rint,stats] = regress(y,x);
%r2越接近1,F越大,p越小(<0.05),回归效果越显著
r2 = stats(1)
F = stats(2)
p = stats(3)
%绘制原始数据和拟合的直线
z = b(1) + b(2)*x;
subplot(2,1,1);
plot(x,y,'o',x,z,'-');
axis([0 180 0 50]);
%绘制残差图
subplot(2,1,2);
rcoplot(r,rint);

在我执行后并没有如期正常出现结果,报错

BLAS 加载错误:
E:Matlabinwin32atlas_Athlon.dll: 找不到指定的模块。



出错 regress (line 93)
b(perm) = R  (Q'*y);

出错 m03 (line 7)
[b,bint,r,rint,stats] = regress(y,x);

  我查看了百度里的,大家好像没有遇到第93行的报错,至于找不到模块我按一片帖子把环境变量中的一个给删除了,依然没有解决。




创建一个func.m函数,如果输入的 参数只有一个x,则返回x;如果输入参数有两个(x,y),则返回sqrt(x2+y2).

function b = func(x,y)
%距离函数
%假如 nargin = 1,返回 x
%假如 nargin = 2,返回 sqrt(x^2 + y^2)
if nargin==1
    b=x;
else
    b = sqrt(x.^2 + y.^2);
end

结果

>> func([3,5])

ans =

     3     5

>> func(3,5)

ans =

   5.8310e+00
原文地址:https://www.cnblogs.com/zhangzimu/p/8683118.html