fitting using lsqcurve fit always giving initial value or one of the boundary conditions vale

8 views (last 30 days)
I am working on fitting a function with 5 parameters. After giving the initial and boundary condition values, the lsqcurve fit function always gives either the initial value of the boundary values for the parameters. How can this be avoided and why does it happen ?
  5 Comments
Matt J
Matt J on 5 Jun 2014
Please highlight your code and apply the
toolbar button to format it. Please also attach your x,y in a .mat file so that we have the exact data.
Sahithya S Iyer
Sahithya S Iyer on 10 Jun 2014
clc
clear all
hold on;
y_tot=[
-3.497458577973105
-3.422672959194234
-3.334843843563475
-3.228646460386137
-3.097941203805021
-2.935215102618057
-2.733090162927215
-2.483701318876909
-2.193254124181070
-1.872215056983354
-1.548347821734015
-1.252567035390769
-0.998499577686843
-0.794119627382786
-0.636861783802723
-0.516695298177893
-0.425872830644377
-0.356490471923106
-0.302358266913845
-0.260317229611995
-0.228615827053601
-0.203095348981863
-0.180955142570347
-0.163439577226996
-0.150576733922086
-0.137981420111499
-0.129705004841609
-0.121459333418920
-0.114092870358963
];
q=[
-3.498555531811177
-3.423624056709905
-3.335433396853431
-3.229149366019616
-3.098444615640528
-2.935611121616155
-2.732635543386978
-2.484124329747534
-2.192343141143736
-1.871961931004800
-1.549022861809153
-1.251541670523438
-0.998326644944361
-0.794941572898976
-0.637258704637672
-0.517033078376788
-0.425708767366708
-0.356046505438913
-0.302453351208873
-0.260785926207760
-0.228026577976551
-0.201985307313793
-0.181066611360143
-0.164098912643618
-0.150213340442235
-0.138758498278120
-0.129240590393676
-0.121281175280005
-0.114587134782998
];
x=[
0.179209183673469
0.267848128991614
0.355849669341040
0.443218601718076
0.529959740475643
0.616077913733323
0.701577959993571
0.786464724953524
0.870743058502428
0.954417811895343
1.037493835093891
1.119975974265811
1.201869069435025
1.283177952274469
1.363907444034638
1.444062353600521
1.523647475670831
1.602667589052675
1.681127455066209
1.759031816053450
1.836385393985747
1.913192889164972
1.989458979013596
2.065188316949052
2.140385531337671
2.215055224524629
2.289201971935686
2.362830321246400
2.435944791616654
];
format long
v=10;
l0=5*10^4;
u0=40*10^2;
data=29;
total_obs=data;
v_upper=1400+total_obs-1;
v1=[1400:v_upper];
i=[1:total_obs];
lt=(1-(1-v./v1).^i)*l0;
ut=((1-v./v1).^i)*u0;
less_data=total_obs-1;
f=@(ini,x)model1_fitng_func(ini,lt(:),ut(:),v1(:),v,l0,total_obs,Optimset());
ini0=[0.005 1 -3.76];
lb=[ 0.003 0 -6];
ub=[ 0.009 2 0];
param=3;
sum=0;
sq_resi=(y_tot-q).*(y_tot-q);
for i = 1:data
sum=sum + sq_resi(i:i);
end
rmssr=sqrt(sum/(data-param));
sd=1.5*rmssr;
mean=0;
sim_num=1000;
for j = 1:sim_num
R=normrnd(mean,sd,[data,1]);
y1=q+R;
f=@(ini,x)model1_fitng_func(ini,lt(:),ut(:),v1(:),v,l0,total_obs);
[ini resnorm residual]=lsqcurvefit(f,ini0,x,y1,lb,ub);
new_p(j,:)=ini;
new_error(j,:)=resnorm;
end
fid1=fopen('confi1','w');
fid2=fopen('confi2','w');
fid3=fopen('confi3','w');
for z = 1:sim_num
new_p(z,:)
new_error(z,:);
fprintf(fid1,'%f\n',new_p(z,1));
fprintf(fid2,'%f\n',new_p(z,2));
fprintf(fid3,'%f\n',new_p(z,3));
end
fclose(fid1);
fclose(fid2);
fclose(fid3);
--------------------------------------------------------------------------
The function is --
function q = model1_fitng_func(ini,lt,ut,v1,v,l0,total_obs)
function nl = nl_func()
function b = b_func()
b = (-1)*(ini(1).*(lt+ut.*ini(2))+1);
end
function a = a_func()
a=ini(1);
end
function c = c_func()
c = ini(1).*lt.*ut.*ini(2);
end
nl=(((-1)*b_func())-sqrt((b_func().*b_func())-(4.*a_func().*c_func())))./(2.*a_func());
end
n1=nl_func();
less_data=total_obs-1;
q1 = ini(3).*v1(2:total_obs).*(n1(2:total_obs)-n1(1:less_data).*(1-v./v1(2:total_obs)))./(l0.*v);
q=[0;q1];
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!