MATLAB-2: 拟合实例

函数

function [f, g, o, y_fit]=jfit(x,y)

if(size(x,2)>1)
    x=x';
end

if(size(y,2)>1)
    y=y';
end

k=diff(y)./diff(x);

jL=[-max(abs(k)) -max(abs(y))];
jU=[max(abs(k)) max(abs(y))];
jSt=[mean(k) mean(y)];

Method='NonlinearLeastSquares';
Robust='off';

MaxFunEvals=100000; 
MaxIter=100000;
Algorithm='Trust-Region';
TolX=1e-9;
TolFun=1e-9; 

jType={'a*x+b'};

s = fitoptions(  'Method',Method,...  
                'Robust',Robust,...
                'Lower',jL,...
                'Upper',jU,...
                'MaxFunEvals',MaxFunEvals,...
                'MaxIter',MaxIter,...
                'TolFun',TolFun,...
                'Algorithm',Algorithm,...
                'TolX',TolX,...
                'Startpoint',jSt);        

g1=fittype(jType{1},'options',s);

[f, g, o]=fit(x,y,g1);
y_fit=f(x);
plot(x,y_fit,'-');
hold on;


end

调用

clear
close 
clc

x = 0:20;
y = -3*x+10+2*randn(size(x));
figure
plot(x,y,'o');
hold on

[f,g,o] = fit(x',y','poly1');
y_fit = f(x);
plot(x,y_fit,'-');
hold on;

f
r2=g.rsquare
coeff=coeffvalues(f)
confid=confint(f)

输出


f = 

     Linear model Poly1:
     f(x) = p1*x + p2
     Coefficients (with 95% confidence bounds):
       p1 =      -2.994  (-3.127, -2.862)
       p2 =       11.35  (9.805, 12.9)

r2 =

    0.9916


coeff =

   -2.9943   11.3520


confid =

   -3.1267    9.8049
   -2.8620   12.8990
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值