polyfit error: Subscripted assignment dimension mismatch.
1 回表示 (過去 30 日間)
古いコメントを表示
I am trying to fit a relationship between temperature and salinity values for different depth bins. At the moment polyfit is not working. If I use degree=1, the error I get is:
Subscripted assignment dimension mismatch. Error in temp_sal_plot_K_edits_v2 (line 88) p(i,:)=polyfit(T,S,1); % Find polynomial relationship for this (i) depth bin
Plus I always get a warning message of this:
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT. > In polyfit at 75 In temp_sal_plot_K_edits_v2 at 40
If I change the degree to 3, polyfit runs, however the fitted polynomial is very inaccurate. Could someone please let me know where I am going wrong? Here is the code:
for i=300:310 % Loop over m steps in depth
T=[];
S=[];
for j=1:nx % Loop over degC steps in tempearure
if vS(i,j) > 0
T=[T qX(i,j)]; % Temperature = temperature value from query points
S=[S vS(i,j)]; % Salinty = salinity value from interpolated grid
end
end
p(i,:)=polyfit(T,S,1); % Find polynomial relationship for this (i) depth bin
fS(i,:)=polyval(p(i,:),qX(i,:)); % Evaluate fS points (Salinity points) for each query point (qX = temperature points) using the derived polynomial expression for this depth bin.
figure;
plot(S,T,'o',qX(i,:),fS(i,:));
xlabel('Salinity, ppt');
ylabel('Temperature, degC');
end
0 件のコメント
採用された回答
Matt J
2016 年 4 月 21 日
編集済み: Matt J
2016 年 4 月 21 日
The error message isn't saying that polyfit is at fault. It's saying that p(i,:) is not the same size as polyfit(T,S,1) in this line
p(i,:)=polyfit(T,S,1);
Likely, you are are trying to stuff the 1x2 output of polyfit into the rows of a pre-existing M x N matrix p where N is not equal to 2. In fact, my guess is that N=4 since you say you got it to run with cubic polynomials.
As for the warning, there's not much we can say without seeing the contents of the T,S that are triggering it. In all likelihood, your T,S data is just bad and isn't well-fit by a unique polynomial. However, you could try doing as the warning instructs and improve the scaling/centering of the data, or add more points.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!