Matlab为拟合模型找到最佳常数 [英] Matlab find the best constants for a fitting model

查看:186
本文介绍了Matlab为拟合模型找到最佳常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面的链接中找到数据,或者,如果您可以向我发送您的私人电子邮件,我可以向您发送数据

Please find the data in the link below, or if you can send me your private email, I can send you the data

https://dl.dropboxusercontent.com/u/5353938/test_matlab_lefou.xlsx

在excel工作表中,第一列是y,第二列是x,第三列是t,我希望这会使情况变得更加清楚,并非常感谢您的帮助.

In the excel sheet, the first column is y, the second is x and the third is t, I hope this will make things much more clear, and many thanks for the help.

我需要使用以下模型,因为它是最适合我的数据的模型,但是我不知道如何找到a和b的最佳值,这将使我获得最佳拟合, (如果需要这些值,我可以附加一个文件),我已经有了y,x和t的值:

I need to use the following model because it is the one that fits best my data, but what I don't know is how to find the best values of a and b, that will allow me to get the best fit, (I can attach a file if you need the values), I already have the values of y, x and t:

y = a * sqrt(x).exp(b.t)

y= a*sqrt(x).exp(b.t)

谢谢

推荐答案

在不依赖于曲线拟合工具箱的情况下,也可以使用fminsearch解决此问题.我首先生成一些数据,您已经拥有但没有与我们共享.必须对参数a和b进行初步猜测(p0).然后,我通过使数据和拟合之间的平方误差最小化来进行优化,得到向量p_fit,其中包含a和b的优化参数.最后,结果可视化.

Without the dependency on the curve fitting toolbox, this problem can also be solved by using fminsearch. I first generate some data, which you already have but didn't share with us. An initial guess on the parameters a and b must be made (p0). Then I do the optimiziation by minizmizing the squared errors between data and fit resulting in the vector p_fit, which contains the optimized parameters for a and b. In the end, the result is visualized.

% ----- Generating some data for x, y and t (which you already got)
N = 10; % num of data points
x = linspace(0,5,N);
t = linspace(0,10,N);

% random parameters
a = rand()*5; % a between 0 and 5
b = (rand()-1); % b between -1 and 0
y = a*sqrt(x).*exp(b*t) + rand(size(x))*0.1; % noisy data

% ----- YOU START HERE WITH YOUR PROBLEM -----
% put x and t into a 2 row matrix for simplicity
D(1,:) = x;
D(2,:) = t;

% create model function with parameters p(1) = a and p(2) = b
model = @(p, D) p(1)*sqrt(D(1,:)).*exp(p(2)*D(2,:));
e = @(p) sum((y - model(p,D)).^2); % minimize squared errors
p0 = [1,-1]; % an initial guess (positive a and probably negative b for a decay)
[p_fit, r1] = fminsearch(e, p0); % Optimize 


% ----- VISUALIZATION ----
figure
plot(x,y,'ko')
hold on
X = linspace(min(x), max(x), 100);
T = linspace(min(t), max(t), 100);
plot(X, model(p_fit, [X; T]), 'r--')
legend('data', sprintf('fit: y(t,x) = %.2f*sqrt(x)*exp(%.2f*t)', p_fit))

结果看起来像

在许多评论后更新

您的数据是列向量,我的解决方案使用行向量.当错误函数试图计算列向量(y)和行向量(模型函数的结果)之差时,就会发生错误.容易破解:使它们全部成为行向量,并使用我的方法.结果是:a = 0.5296和b = 0.0013. 但是,优化"取决于最初的猜测p0,您可能需要尝试一下.

Your data are column vectors, my solution used row vectors. The error occured when the errorfunction tryed to compute the difference of a column vector (y) and a row-vector (result of the model-function). Easy hack: make them all to row vectors and use my approach. The result is: a = 0.5296 and b = 0.0013. However, the Optimization depends on the initial guess p0, you might want to play around with it a little bit.

clear variables
load matlab.mat

% put x and t into a 2 row matrix for simplicity
D(1,:) = x;
D(2,:) = t;
y = reshape(y, 1, length(y)); % <-- also y is a row vector, now

% create model function with parameters p(1) = a and p(2) = b
model = @(p, D) p(1)*sqrt(D(1,:)).*exp(p(2)*D(2,:));
e = @(p) sum((y - model(p,D)).^2); % minimize squared errors
p0 = [1,0]; % an initial guess (positive a and probably negative b for a decay)
[p_fit, r1] = fminsearch(e, p0); % Optimize 

% p_fit = nlinfit(D, y, model, p0) % as a working alternative with dependency on the statistics toolbox

% ----- VISUALIZATION ----
figure
plot(x,y,'ko', 'markerfacecolor', 'black', 'markersize',5)
hold on
X = linspace(min(x), max(x), 100);
T = linspace(min(t), max(t), 100);
plot(X, model(p_fit, [X; T]), 'r-', 'linewidth', 2)
legend('data', sprintf('fit: y(t,x) = %.2f*sqrt(x)*exp(%.2f*t)', p_fit))

结果看起来并不令人满意.但这主要是因为您的数据.在这里看看:

The result doesn't look too satisfying though. But that mainly is because of your data. Have a look here:

这篇关于Matlab为拟合模型找到最佳常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆