MATLAB realizes arbitrary function curve fitting (Gaussian curve as an example)

Gaussian curve fitting

The function of matlab to achieve polynomial fitting is relatively simple and easy to remember, but the fitting of complex curves is more complicated, such as Gaussian curves, also known as normal distribution functions. Make a note online for everyone to encourage.

clc;clear;
close all;

y = [1,1,1,3,7,8,7,3,1,1,1];                             %  待拟合向量
len = length(y);                                         %  数据长度
x = 1:1:len;                                             %  时间轴坐标
figure,plot(x,y);
xlabel('Position / s');
ylabel('Intencity / cd');
title('Input Signal');

% 定义初始参数,matlab在此基础上优化,初始参数太差会导致优化效果很差
Amptittude = 7;                                         % 定义初始幅值a              
pos = 6;                                                % 定义中心横坐标b
standerror = 2;                                         % 定义标准差c
Da = 1;                                                 % 定义最低纵坐标d
gaussEqn = 'a*exp(-((x-b)/c)^2)+d';                     % 高斯方程
startPoints = [Amptittude pos standerror,Da];           % 初始参数矩阵
para1 = fit(x',y',gaussEqn,'Start', startPoints);       % 拟合命令(x、y必须是列向量)
hold on,plot(para1,x,y);                                % 绘图命令
a = getfield(para1,'a');                                % 获取指定拟合参数a

The comparison results of curves before and after fitting are as follows.
Data before fitting and curve after fitting

Guess you like

Origin blog.csdn.net/ruredfive/article/details/122996534