zoukankan      html  css  js  c++  java
  • MATLAB拟合正态分布

    clear;clc;close all
    format compact
    %% 正态分布的拟合
    % 生成随机数
    num = 50;
    y = randn(1000,1);
    x = 1:num;
    y = hist(y,num);
    xx = x(:);
    yy = y(:);
    % Set up fittype and options.
    ft = fittype('y0+(a/(w*sqrt(pi/2)))*exp(-2*((x-xc)/w).^2)', 'independent', 'x', 'dependent', 'y');
    opts = fitoptions(ft);
    opts.Display = 'Off';
    opts.Lower = [0 0 0 0];
    opts.StartPoint = [1.1 1.1 1.1 1.1];
    % Fit model to data.
    [fitresult, gof] = fit( xx, yy, ft, opts );
    % Plot fit with data.
    figure;
    plot(fitresult)
    hold on
    plot(xx, yy,'b*');
    legend('原始数据', '拟合曲线', 'Location', 'NorthEast');
    title(['正态分布拟合,num=',num2str(num)])
    xlabel('x');
    ylabel('y');
    grid on
    saveas(gcf,'pic.png')
    %% 输出拟合参数
    a = fitresult.a
    w = fitresult.w
    xc = fitresult.xc
    y0 = fitresult.y0
    %% 计算均方误差
    yyy = y0+(a/(w*sqrt(pi/2)))*exp(-2*((xx-xc)/w).^2);
    rmse = 1/length(yyy)*norm(yyy-yy);
    fprintf('num = %d, rmse = %.2f
    ',num,rmse)
    

      

    得到对应的参数为

    a =
    992.6775
    w =
    14.3208
    xc =
    28.0562
    y0 =
    0.1646
    

    均方误差为

    mse = 0.69
    

      

  • 相关阅读:
    Nim or not Nim? hdu3032 SG值打表找规律
    Maximum 贪心
    The Super Powers
    LCM Cardinality 暴力
    Longge's problem poj2480 欧拉函数,gcd
    GCD hdu2588
    Perfect Pth Powers poj1730
    6656 Watching the Kangaroo
    yield 小用
    wpf DropDownButton 源码
  • 原文地址:https://www.cnblogs.com/liutongqing/p/6613745.html
Copyright © 2011-2022 走看看