Weighted nonlinear curve fitting

8 views (last 30 days)
friet
friet on 29 Feb 2016
Commented: friet on 1 Mar 2016
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 Comments
Torsten
Torsten on 1 Mar 2016
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 on 1 Mar 2016
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

Sign in to comment.

Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!