简体   繁体   English

将数据拟合到已知函数MATLAB(无曲线拟合工具箱)

[英]Fitting data to a known function MATLAB (without curve fitting toolbox)

I have a function with three parameters and some data that I want to fit. 我有一个带有三个参数的函数和一些我想拟合的数据。 How can I do this optimally? 我怎样才能做到最好? I am not even sure of the range of the three parameters in the equation. 我什至不确定方程式中三个参数的范围。

The function has free parameters alpha , beta and gamma and is given by 该函数具有自由参数alphabetagamma并且由给出

 y = (1 - alpha + alpha./sqrt(1 + 2*beta*(gamma*x).^2./alpha)).^(-1) - 1;

I have arrays of x and y data points (around 50 points in each set) to which I want to find the best fit (defined as minimizing least squares) using any alpha , beta and gamma . 我有xy数据点的数组(每组中约50个点),我想使用任何alphabetagamma来找到最合适的位置(定义为最小化最小二乘)。

The solutions online recommend the curve fitting toolbox, which I do not have on my machine and am unable to install. 在线解决方案建议使用曲线拟合工具箱,该工具箱在我的计算机上没有,并且无法安装。 I only have the barebones MATLAB 2015b version. 我只有准系统MATLAB 2015b版本。

You need an optimization algorithm for smooth, R^n -> R functions. 您需要优化算法来平滑R^n -> R函数。 Since you have only access to barebone Matlab, a good idea is to take an algorithm from File Exchange. 由于您只能访问准系统Matlab,所以一个好主意是从File Exchange中获取一种算法。 For illustration I picked LMFnlsq , which should suffice since you have a small problem, although it seems to be more general and a little bit overkill here. 为了说明LMFnlsq ,我选择了LMFnlsq ,这应该足够了,因为您有一个小问题,尽管这似乎更笼统,并且有点过分。

Download LMFnlsq and add to your Matlab path. 下载LMFnlsq并添加到您的Matlab路径。


Example

For convenience make a function called regr_fun : 为了方便起见,请创建一个名为regr_fun的函数:

function y = regr_fun(par, x)
    alpha   = par(1);
    beta    = par(2);
    gamma   = par(3); 
    y = (1 - alpha + alpha./sqrt(1 + 2*beta*(gamma*x).^2./alpha)).^(-1) - 1;
end

Curve fitting (in the same folder as regr_fun ): 曲线拟合(与regr_fun在同一文件夹中):

%---------------------------------------------------------------------
% DUMMY DATA
%---------------------------------------------------------------------
% Generate data from known model contaminated with random noise
rng(333) % for reproducibility

alpha   = 2;
beta    = 0.1;
gamma   = 0.1;
par     = [alpha, beta, gamma];

xx      = 1:50;
y_true  = regr_fun(par, xx);
yy      = y_true + normrnd(0,1,1,50);

%---------------------------------------------------------------------
% FIT MODEL
%---------------------------------------------------------------------
% intial point of solver
p0      = [1,1,1];

obj_fun = @(p) sum((regr_fun(p, xx) - yy).^2);

% optimization
p_fit   = LMFnlsq(obj_fun, p0);

y_fit   = regr_fun(p_fit, xx);

%---------------------------------------------------------------------
% PLOT
%---------------------------------------------------------------------
plot(xx, yy, 'o')
hold on
plot(xx, y_true)
plot(xx, y_fit, '--')

在此处输入图片说明


Note 注意

Although matlab.codetools.requiredFilesAndProducts lists the symbolic toolbox as well, for this problem it is not needed and the function should run withouth that as well. 尽管matlab.codetools.requiredFilesAndProducts列出了符号工具箱,但对于此问题,它不是必需的,并且函数也应如此运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM