zoukankan      html  css  js  c++  java
  • matlab多项式拟合以及指定函数拟合

    clc;
    clear all;
    close all;
    %% 多项式拟合指令;
    % X = [1 2 3 4 5 6 7 8 9 ];
    % Y = [9 7 6 3 -1 2 5 7 20];
    % P= polyfit (X,Y,3);
    %
    % x = 0:2:10;
    % y = polyval(P,x);
    % plot(x,y,X,Y,'r*');
    %% 指定函数拟合
    x=[ 0;0.4;1.2; 2;2.8;3.6;4.4;5.2; 6;7.2; 8;9.2;10.4;11.6;12.4;13.6;14.4;15];
    y=[ 1;0.85;0.29;-0.27;-0.53;-0.4;-0.12;0.17;0.28;0.15;-0.03;-0.15;-0.071;0.059; 0.08;0.032;-0.015;-0.02];
    plot(x,y,'r*')
    hold on
    f = fittype('a*cos(k*t)*exp(w*t)','independent','t','coefficients',{'a','k','w'});
    cfun = fit(x,y,f);
    xi = 0:1:20;
    yi = cfun(xi);
    plot(xi,yi,'b-')
    %程序中,函数fittype是自定义拟合函数;cfun=fit( x, y, f)是根据自定义的拟合函数f 来拟合数据 x

    %% 关于拟合小列子,以logistic曲线模型
    % 读入人口数据(1971-2000)
    Y=[33815 33981 34004 34165 34212 34327 34344 34458 34498 34476 34483 34488 34513 ...
    34497 34511 34520 34507 34509 34521 34513 34515 34517 34519 34519 34521 34521 ...
    34523 34525 34525 34527];
    % 读入时间变量数据(t=年份-1970)
    T=[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
    27 28 29 30];
    % 线性化处理
    for i = 1:length(T)
    y(i) = 1/Y(i);
    x(i) = exp(-T(i));
    end
    %计算并输出回归系数B
    for i = 1:length(T)
    c = ones(30,1);
    X = [c,x'];
    b = inv(X'*X)*X'*y';
    end
    for i = 1: length(T)
    z(i) = b(1) + b(2)*x(i); %计算拟合值;
    s(i) = z(i) - sum(y)/30; %计算离差值;
    w(i) = z(i) - y(i) %计算误差;
    end
    %计算计算离差平方和
    S = s*s';
    %计算计误差差平方和
    Q = w * w';
    % 计算回归平方和U
    U=S-Q;
    % 计算,并输出F检验值
    F=28*U/Q;
    % 计算非线性回归模型的拟合值
    for j=1:30,
    Y(j)=1/(b(1,1)+b(2,1)*exp(-j));
    end
    % 输出非线性回归模型的拟合曲线(Logisic曲线)
    plot(T,Y)

  • 相关阅读:
    分布式锁的三种实现方式
    sharding-jdbc
    MySQL Proxy 实现 MySQL 读写分离提高并发负载
    python 使用流式游标 读取mysql怎么不会内存溢出
    数据仓库方案
    MySQL percona-toolkit工具详解
    percona-toolkit 主从工具 master-slave
    MySQL sql join 算发
    MySQL5.7.6 general tablespace
    MySQL Data Directory -- Creating file-per-table tablespaces outside the data directory
  • 原文地址:https://www.cnblogs.com/Kermit-Li/p/4325614.html
Copyright © 2011-2022 走看看