How to fit data with custom function with two variables?

조회 수: 15 (최근 30일)
Marek Balko
Marek Balko 2021년 4월 25일
Hi,
I would ask How to fit data with custom function with two variables? I have data from measurements in two vectors, let's say Ra and Pr. I need to fit this data to equation: a*(Ra^(b))*(Pr^(c)) and extract the coeficients value. How can I do that? Thank you

답변 (1개)

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2021년 4월 25일
You can minimize the error of your model to the data you want by optimizing the parameters. One alternative for it is, for example, by using fminsearch.
Ra = randn(100,1)*5; % Data you said you have
Pr = randn(100,1);
% I'm generating some example data
a = 0.5;
b = 0.1;
c = 0.35;
dataToFitModel = a*(Ra.^(b)).*(Pr.^(c)); % You didn't mention this data but you should have,
% otherwise it doesn't make sense to talk about fit
% Here you create a function to minimize the error between measurements and your model
fun = @(x) rms(dataToFitModel- ( x(1)*(Ra.^( x(2) )).*(Pr.^( x(3) )) ) )
fun = function_handle with value:
@(x)rms(dataToFitModel-(x(1)*(Ra.^(x(2))).*(Pr.^(x(3)))))
firstGuess = [0,0,0]; % Depending of your data you may need a start point close to the real answer
[x,fval] = fminsearch(fun,firstGuess);
aFromData = x(1)
aFromData = 0.5000
bFromData = x(2)
bFromData = 0.1000
cFromData = x(3)
cFromData = 0.3500
fval
fval = 4.6583e-05

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by