# Calculate residuals as a single number in lsqnonlin function

14 ビュー (過去 30 日間)
Mar 2018 年 10 月 24 日
コメント済み: Rik 2018 年 10 月 30 日
I solve a nonlinear curve fitting problem using the lsqnonlin Matlab function.
I am interested to check the residual of my fitting each time so I am returning the residual each time I call the lsqnonlin function. I wanted to calculate a single number of the residual to see if the fits gets better after doing some changes in my data. My data are 3D images. Each time I calculated the sum of residuals using the sum() function. I ended up though with very large numbers, e.g. 7.3507e+04. Is there a better way to represent the residual in a single number than calculating the sum?
##### 1 件のコメント表示非表示 なし
Matt J 2018 年 10 月 24 日
It is not clear why you think you need a "better" way. If you want the numbers to be smaller just scale down your objective function, e.g.,
objfun_new =@(x) objfun_old(x)/1e4;

サインインしてコメントする。

### 回答 (2 件)

Rik 2018 年 10 月 24 日
Yes, the sum of squares or the RMS:
SSq=sum(res.^2);
RMS=sqrt(mean(res.^2));
The downside of using the plain sum is that a large negative and a large positive will cancel out, which is clearly not what you want. Whether or not a number is large should not be a factor in your decision, just that you want to minimize it.
##### 1 件のコメント表示非表示 なし
Rik 2018 年 10 月 30 日
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

サインインしてコメントする。

Torsten 2018 年 10 月 24 日
If you use the usual naming conventions of lsqnonlin, the residual is "sum(F.^2)"
Best wishes
Torsten.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by