你要知道,你要擬合的是一個分段函式,這本身就不是一般的連續光滑函式。
nlinfit之所以要有初值這一項,就是考慮到在某些變態的情況下,無法找到最小二乘函式的最小值,可能是一個區域性的最小值,也可能根本找不到,所以有必要透過改變迭代初值的方法進行試驗。對於這種分段函式,最好的方法還是分段擬合。
fun1=inline("4.213-300*x/ref(1)-300*(ref(2)+ref(3))+300*ref(3)*exp(-x/(ref(3)*ref(4)))","ref","x");
fun2=inline("4.213-3000/ref(1)-300*ref(3)*exp(-x/(ref(3)*ref(4)))*(exp(10/(ref(3)*ref(4)))-1)","ref","x");
fun=@(ref,t)((t<=10).*fun1(ref,t)+(t>10).*fun2(ref,t));
t=0:0.1:50;
v=[3.969
3.963
3.959
3.955
3.952
3.949
3.947
3.945
3.943
3.941
3.939
3.938
3.936
3.935
3.933
3.932
3.93
3.929
3.928
3.927
3.926
3.925
3.923
3.922
3.921
3.92
3.919
3.918
3.917
3.916
3.915
3.914
3.913
3.912
3.911
3.91
3.909
3.908
3.907
3.906
3.905
3.904
3.903
3.902
3.901
3.9
3.899
3.898
3.897
3.896
3.895
3.894
3.893
3.892
3.891
3.89
3.889
3.888
3.887
3.886
3.885
3.884
3.883
3.882
3.881
3.88
3.879
3.878
3.877
3.876
3.875
3.874
3.873
3.872
3.871
3.87
3.869
4.105
4.108
4.11
4.112
4.114
4.115
4.116
4.117
4.118
4.119
4.12
4.121
4.122
4.123
4.124
4.125
4.126
4.127
4.128
4.129
4.13
4.131
4.132
4.133
4.134
4.135
4.136
4.137
4.138
4.139
4.14
4.141
4.142
4.143
4.144
4.145
4.146
4.147
4.148
4.149
4.15
4.151
4.152
4.153
4.154
4.155
4.156
4.156];
abc=nlinfit(t",v,fun,[51891 0.00083 0.00013 12050]");
plot(t,v);hold on;
f=@(t)(fun(abc,t));
fplot(f,[0,50],"r")
你要知道,你要擬合的是一個分段函式,這本身就不是一般的連續光滑函式。
nlinfit之所以要有初值這一項,就是考慮到在某些變態的情況下,無法找到最小二乘函式的最小值,可能是一個區域性的最小值,也可能根本找不到,所以有必要透過改變迭代初值的方法進行試驗。對於這種分段函式,最好的方法還是分段擬合。
fun1=inline("4.213-300*x/ref(1)-300*(ref(2)+ref(3))+300*ref(3)*exp(-x/(ref(3)*ref(4)))","ref","x");
fun2=inline("4.213-3000/ref(1)-300*ref(3)*exp(-x/(ref(3)*ref(4)))*(exp(10/(ref(3)*ref(4)))-1)","ref","x");
fun=@(ref,t)((t<=10).*fun1(ref,t)+(t>10).*fun2(ref,t));
t=0:0.1:50;
v=[3.969
3.963
3.959
3.955
3.952
3.949
3.947
3.945
3.943
3.941
3.939
3.938
3.936
3.935
3.933
3.932
3.93
3.929
3.928
3.927
3.926
3.925
3.923
3.922
3.921
3.92
3.919
3.918
3.917
3.916
3.915
3.914
3.913
3.912
3.911
3.91
3.909
3.908
3.907
3.907
3.906
3.905
3.904
3.903
3.903
3.902
3.901
3.9
3.9
3.899
3.898
3.898
3.897
3.896
3.896
3.895
3.894
3.893
3.893
3.892
3.891
3.891
3.89
3.889
3.889
3.888
3.888
3.887
3.887
3.886
3.886
3.885
3.884
3.884
3.883
3.882
3.882
3.881
3.88
3.88
3.879
3.879
3.878
3.878
3.877
3.877
3.876
3.876
3.875
3.875
3.874
3.874
3.873
3.873
3.872
3.872
3.871
3.87
3.87
3.869
3.869
4.105
4.108
4.11
4.112
4.114
4.115
4.116
4.117
4.118
4.119
4.119
4.12
4.12
4.121
4.122
4.122
4.123
4.123
4.124
4.124
4.124
4.125
4.125
4.126
4.126
4.126
4.127
4.127
4.128
4.128
4.129
4.129
4.129
4.129
4.129
4.13
4.13
4.13
4.13
4.13
4.131
4.131
4.131
4.131
4.132
4.132
4.132
4.132
4.132
4.132
4.133
4.133
4.133
4.133
4.133
4.134
4.134
4.134
4.134
4.134
4.134
4.135
4.135
4.135
4.135
4.135
4.136
4.136
4.136
4.136
4.136
4.137
4.137
4.138
4.138
4.138
4.139
4.139
4.139
4.139
4.139
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.14
4.141
4.141
4.141
4.141
4.141
4.141
4.141
4.141
4.141
4.141
4.142
4.142
4.142
4.142
4.142
4.142
4.142
4.142
4.142
4.142
4.143
4.143
4.143
4.143
4.143
4.143
4.143
4.143
4.143
4.143
4.143
4.143
4.143
4.144
4.144
4.144
4.144
4.144
4.144
4.144
4.144
4.144
4.144
4.144
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.145
4.146
4.146
4.146
4.146
4.146
4.146
4.146
4.146
4.146
4.146
4.146
4.146
4.146
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.147
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.148
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.149
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.15
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.151
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.152
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.153
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.154
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.155
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156
4.156];
abc=nlinfit(t",v,fun,[51891 0.00083 0.00013 12050]");
plot(t,v);hold on;
f=@(t)(fun(abc,t));
fplot(f,[0,50],"r")