Follow-up questions: Residuals do not match function evaluation

Hello guys,
EDIT: SOLVED! I HAD A RATHER STUPID TYPO IN MY SUBSEQUENT FUNCTION EVALUATION. However, are there suggestions for ways that allow for a more efficient solution with less coding? Thanks.
I want to find coefficients and to determine the following function
.
I have collected ten points in order to create an over-determined system of equations, which are
  1. -30,0791, 0,8550
  2. -30,1300, 0,8653
  3. -28,0569, 0,8410
  4. -28,6991, 0,8294
  5. -41,1462, 0,9072
  6. -41,9320, 0,9089
  7. -55,4840, 0,9619
  8. -54,2756, 0,9573
  9. -36,8350, 0,8940
  10. -36,2153, 0,8849
With lsqnonlin and trust-region-reflective algorithm I find a solutions with with resnorm below 10 and residuals not greater than 1, however, plotting my solution, I get values for the above that differ extremely from the listed above.
1.) How is that possible?
2.) Do you see any mistakes?
See my code below:
options = optimoptions('lsqnonlin','Algorithm', 'trust-region-reflective','StepTolerance',1e-8,'MaxFunctionEvaluations',100000, 'MaxIterations',100000,'FunctionTolerance',1e-8);
resnorm = 5;%Great arbitrary value for initial comparison
X_sol = [];
resnorm_V = [5000]; %Great arbitrary value for initial comparison
AB= 1000;
while resnorm > 1
x_0 = [random('unif',-AB,AB,1,4] ;
[x_sol, resnorm, residual ] = lsqnonlin(@f, x_0, [], [], options);
resnorm
if resnorm < (min(resnorm_V))
VX_sol = [VX_sol, x_sol];
resnorm_V = [resnorm_V, resnorm];
end
end
and the function
function fval = f(x)
constFv = load("constFv.mat"); %loading values
constFv = constFv.constFv;
fi=constFv(3:4,1:length(constFv(1,:))); %separating f_i and t_i
ti=constFv(1:2,1:length(constFv(1,:)));
fval = zeros(numel(constF)/2,2);
for i=1:length(fval(:,1))
for j=1:2
fval(i,j) = -fi(j,i)+x(1)+(x(2)-x(1))*exp(-(ti(j,i)/x(3))^2)+x(4)*ti(j,i);
end
end
As residuals, I receive
-0,373575036474449 -1,92869128304233
-0,524159277852505 1,42976241429415
0,719685412984291 1,11030090112604
-0,353508951663116 -0,0674783477211349
-0,621962750820785 0,614087159122846.
resnorm is 2.90. The best solution for is
-73029,0348456841 -12644,1774701334 1,29248885082932 39784,9692025194
which makes already clear that the vaues that I receive after inserting and differ by factor 10000 and are cleary increasing over t instead of decreasing.
Any suggestions? Thanks a lot.

4 Comments

If you post the data in a way, which can be used by copy&paste directly, it would be easier to check your results. So please use standard Matlab syntax and dots as decimal separators. Thanks.
Loading the data from te disk in each iteration is rather inefficient. Do this once only and provide the data by an anonymous function:
Data = load("constFv.mat"); %loading values
constFv = Data.constFv;
fcn = @(x) f(x, constFv);
...
[x_sol, resnorm, residual ] = lsqnonlin(@fcn, x_0, [], [], options);
What is "Zeros" in
for i=1:length(Zeros(:,1))
? You can replace
fval = zeros(numel(constF)/2,2);
for i=1:length(Zeros(:,1))
for j=1:2
fval(i,j) = -fi(j,i)+x(1)+(x(2)-x(1))*exp(-(ti(j,i)/x(3))^2)+x(4)*ti(j,i);
end
end
by
fval = -fi + x(1) + (x(2) - x(1)) * exp(-(ti / x(3)) .^ 2) + x(4) * ti;
Edit: Great suggestions, Jan. Will keep that in mind! Is there any 'Thank you'-Button from which you will benefit? If not, have a great day.
Sorry for that!
Found the rather stupid typo in my code anyway.
However, on the theoretical side and by looking at this problem, do you have any recommendations for solving this problem in a faster manner (concerning written code not computing time). I have a strong feeling that there must be more efficient functions available.
Thank you anyhow!
What does this mean
length(Zeros(:,1))
?
That was also a typo, resulting from changing the variable names to something supposedly more readable. Corrected!

Sign in to comment.

Answers (0)

Tags

Asked:

on 11 Mar 2019

Edited:

on 11 Mar 2019

Community Treasure Hunt

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

Start Hunting!