Weighted nonlinear curve fitting

조회 수: 5 (최근 30일)
friet
friet 2016년 2월 29일
댓글: friet 2016년 3월 1일
I have the following code that do non-linear curve fiting. However i want to do weighted curve fitting so that it fits well when the value of x is above 45.
clear all
clc
x = [36.78 ,37.53 ,38.28 ,39.06 ,39.85 ,40.65 ,41.47 ,42.31,43.17 ,44.04 ,44.93 ,45.84 ,46.76 ,46.89 ,47.30 ,48.20 ];
y = [0.01 ,0.0152 ,0.023 ,0.035,0.0536 ,0.081 ,0.12 ,0.1891 ,0.287 ,0.438 ,0.66 ,1.01494 ,1.544 ,2.35,3.578 ,5.445 ];
f=0:0.2:3;
fun = @(params) [x - exp((params(2)-1)*f-1)/(params(2)-1)^2, log(y) - log(params(1)) - (params(2)+1)*f];
lb = [1e-10,-1+1e-10]; % Set lower bounds
ub = [inf,inf]; % Set upper bounds
params0 = [0.001,1.7]; % Set initial point.
options.Algorithm = 'levenberg-marquardt';
params = lsqnonlin(fun,params0,lb,ub,options)
figure(1)
plot(x,y,'ko',exp((params(2)-1)*f-1)/(params(2)-1)^2,params(1)*exp((params(2)+1)*f),'b-')
legend('Data','Best fit')
grid on
I tried this
W = [1 1 1 1 1 1 1 1 1 1 1 5 5 5 5 5]';
params = lsqnonlin(fun,params0,lb,ub,'Weights',W);
But it didnt work. Any help will be appreciated.
Thanks
  댓글 수: 4
Torsten
Torsten 2016년 3월 1일
Since you have a (1x32) vector to be fitted ([x,y]), your vector of weights also must have 32 entries.
I think after this change you will be able to use lsqnonlin for your purpose.
Best wishes
Torsten.
friet
friet 2016년 3월 1일
it is not working. Can you please have a look at my code
clear all
clc
x = [36.78 ,37.53 ,38.28 ,39.06 ,39.85 ,40.65 ,41.47 ,42.31,43.17 ,44.04 ,44.93 ,45.84 ,46.76 ,46.89 ,47.30 ,48.20 ];
y = [0.01 ,0.0152 ,0.023 ,0.035,0.0536 ,0.081 ,0.12 ,0.1891 ,0.287 ,0.438 ,0.66 ,1.01494 ,1.544 ,2.35,3.578 ,5.445 ];
f=0:0.2:3;
w=ones(1,32);
fun = @(params) [x - exp((params(2)-1)*f-1)/(params(2)-1)^2, log(y) - log(params(1)) - (params(2)+1)*f];
lb = [1e-10,-1+1e-10]; % Set lower bounds
ub = [inf,inf]; % Set upper bounds
params0 = [0.001,1.7]; % Set initial point.
options.Algorithm = 'levenberg-marquardt';
params = lsqnonlin(fun,params0,lb,ub,options,'Weight',w)
figure(1)
plot(x,y,'ko',exp((params(2)-1)*f-1)/(params(2)-1)^2,params(1)*exp((params(2)+1)*f),'b-')
legend('Data','Best fit')
grid on

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by