matlab fmincon error "Solver stopped prematurely" - matlab

I have a given path for 6 DOF manipulator for given knots. path(dof,knot)
path_f=[-0.5131 -0.6587 -1.0058 -1.4202 -1.7674 -1.9130
-0.8696 -0.6711 -0.1980 0.3667 0.8399 1.0383
-0.8961 -0.7433 -0.3789 0.0560 0.4205 0.5733
1.1714 0.9639 0.4691 -0.1215 -0.6163 -0.8238
-3.1000 -2.5800 -1.3400 0.1400 1.3800 1.9000
-1.1514 -0.9439 -0.4491 0.1415 0.6363 0.8438]
I generated cubic splines between each joints and and each time interval is defined as h(i) and I tried to find the minimum h value which is h(1)+h(2)+h(3)+h(4)+h(5).
to solve that problem I wrote a code:
options=optimset('Algorithm','sqp','Display','iter','DiffMinChange',1e-16,'DiffMaxChange',1e-4,'TolFun',1e-14,'TolX',1e-20,'MaxFunEvals',60000,'MaxIter',1000);
h0=[0.001; 0.001; 0.001; 0.001; 0.001]
h=fmincon(#(h)objecfun(h,path), h0, [], [], [], [], [0; 0; 0; 0; 0] , [], #(h) nonlconstraint(h,Robot,path_f,dof), options);
When I run the code it says:
Solver stopped prematurely.
fmincon stopped because it exceeded the iteration limit,
options.MaxIter = 1000 (the selected value).
After 5th iteration f(x) value doesn't change and the results:
Norm of First-order
Iter F-count f(x) Feasibility Steplength step optimality
0 6 2.500000e-01 2.001e+01 1.000e+00
1 12 3.903637e-01 8.013e+00 1.000e+00 6.374e-02 6.077e-01
2 18 5.115308e-01 2.485e+00 1.000e+00 5.914e-02 3.255e-01
3 24 5.519283e-01 4.258e-01 1.000e+00 2.941e-02 1.712e-01
4 30 5.528961e-01 1.288e-02 1.000e+00 5.382e-03 3.098e-02
5 36 5.530002e-01 4.399e-06 1.000e+00 2.153e-04 2.442e-02
6 42 5.530000e-01 3.403e-12 1.000e+00 1.305e-07 1.828e-06
7 48 5.530000e-01 8.704e-14 1.000e+00 1.050e-13 3.103e-07
8 54 5.530000e-01 8.349e-14 1.000e+00 3.211e-15 1.465e-07
9 65 5.530000e-01 6.573e-14 1.681e-01 2.713e-16 1.319e-07
10 72 5.530000e-01 3.020e-14 7.000e-01 9.286e-16 3.740e-08
I have no idea about what the reason behind it. Could you please help me?

Your f(x) probably is changing but you don't see it in this resolution. Look at the magnitude of your step size. Try scaling your problem or (if reasonable for your case) increasing DiffMinChange.

Related

fmincon with lower bound fails, even though solution is at initial point

I'm trying to minimize a non-linear objective function (my actual function is much more complicated than that, but I found that even this simple function illustrates the point), where I know that minimum is obtained at the initial point x0:
fun = #(x) x(1)^2+x(2)^2;
x0 = [0 0];
lb1 = [0 0];
lb2 = [-1 -1];
[xc1 fvalc1] = fmincon(fun, x0, [],[],[],[], lb1, [Inf Inf])
Which outputs:
>> xc1 = 1.0e-03 * [0.6457 0.6457]
>> fvalc1 = 8.3378e-07
However, both using a different lower bound or using fminsearch instead work correctly:
[xc2 fvalc2] = fmincon(fun, x0, [],[],[],[], lb2, [Inf Inf])
>> xc2 = [0 0]
>> fvalc2 = 0
[xs fvals] = fminsearch(fun, x0)
>> xs = [0 0]
>> fvals = 0
What goes wrong in the first fmincon call?
We can diagnose this using the output output argument as specified in the docs
[xc1, fvalc1, ~, output] = fmincon(fun, x0, [],[],[],[], lb1, [Inf Inf])
The value output.stepsize is the final step size taken in the iterative solving process. In this case:
output.stepsize
>> ans = 6.586e-4
The estimated minima was at x = [6.457e-4, 6.457e-4] and the lower bounds you've permitted are [0 0], so the solver is not permitted to take another step! Another step would give x = [-1.29e-5, -1.29e-5] which is outside of the boundaries.
When you allow the lower bounds to be [-1, -1] the solver can over-shoot the minimum and approach it from all directions.
Moreover, we can use the options input to get even better insight!
options.Display = 'iter';
[xc1, fvalc1, ~, output] = fmincon(fun, x0, [],[],[],[], lb1, [Inf Inf], [], options);
Printed to the command window we see this:
Your initial point x0 is not between bounds lb and ub; FMINCON
shifted x0 to strictly satisfy the bounds.
First-order Norm of
Iter F-count f(x) Feasibility optimality step
0 3 1.960200e+00 0.000e+00 9.900e-01
1 6 1.220345e-02 0.000e+00 8.437e-01 1.290e+00
2 9 4.489374e-02 0.000e+00 4.489e-02 1.014e-01
3 12 1.172900e-02 0.000e+00 1.173e-02 1.036e-01
4 15 3.453565e-03 0.000e+00 3.454e-03 4.953e-02
5 18 1.435780e-03 0.000e+00 1.436e-03 2.088e-02
6 21 4.659097e-04 0.000e+00 4.659e-04 1.631e-02
7 24 2.379407e-04 0.000e+00 2.379e-04 6.160e-03
8 27 6.048934e-05 0.000e+00 6.049e-05 7.648e-03
9 30 1.613884e-05 0.000e+00 1.614e-05 3.760e-03
10 33 5.096660e-06 0.000e+00 5.097e-06 1.760e-03
11 36 2.470360e-06 0.000e+00 2.470e-06 6.858e-04
12 39 8.337765e-07 0.000e+00 8.338e-07 6.586e-04
So your x0 is invalid! This is why the solver doesn't return the result with 1 iteration and lower bounds of [0 0].
fminsearch also works for the same reason - you've not imposed a lower bound on which the solution sits.

Image compression using orthogonal polynomial Transformation [duplicate]

I am working on Image compression based on orthogonal polynomial transformation. My input image is gray scale image of size 256*256. i divide this image into 4 by 4 blocks. and than apply the orthogonal polynomial operator on each block. but I am not getting correct coefficient.
Example:
If I have a block 4x4 of Input image:
I=[5 11 8 10;9 8 4 12; 1 10 11 4;19 6 15 7];
and my polynomial operator for n=4 is:
[M] = [p0 p1 p2 p3]=[1 -3 1 -3;1 -1 -1 9;1 1 -1 -9;1 3 1 3]
After finding the outer product:
[20 -24 24 -16; -24 84 -80 24;24 -80 84 -24;-16 24 -24 20]
when I applied this on input image blocks, my answer is:
[-396 172 88 -104; 1012 -248 -376 616 -972 320 436 -552; 492 -104 4 172]
which is wrong. It should be
[140 0 -6 -10; 32 -112 2 -174; 22 -30 8 -40;34 -54 84 -8]
what I have done wrong?
My MATLAB code to find orthogonal polynomial operator is given below.
clc
clear all
close all
ID=imread('cameraman.tif'); % input image
I=double(ID);
imshow(ID);
[r,c]=size(I);
y1=zeros(r,c);
x=1:4;
n=4;
mu=(n+1)/2;
p0=[1 1 1 1];
p1=x-mu;
p2=(x-mu).^2-(n.^2-1)/12;
p3=(x-mu).^3-((x-mu)*(3*n.^2-7))/20;
P=[p0;p1; p2; p3];
N=[1 1 1 1;-1.5 -0.5 0.5 1.5;1 -1 -1 1;-0.3 0.9 -0.9 0.3];
M=[1 1 1 1;-3 -1 1 3;1 -1 -1 1;-3 9 -9 3]; % after sacling
v=M';
O=M.*v;
T=O'; % Orthogonal polynomial operator
for i=1:4:r
for j=1:4:c
y1(i:i+3,j:j+3)=I(i:i+3,j:j+3).*(double(T));
end
end
figure,imshow(uint8(y1))
y1

How to apply orthogonal polynomial transformation on an image for image compression?

I am working on Image compression based on orthogonal polynomial transformation. My input image is gray scale image of size 256*256. i divide this image into 4 by 4 blocks. and than apply the orthogonal polynomial operator on each block. but I am not getting correct coefficient.
Example:
If I have a block 4x4 of Input image:
I=[5 11 8 10;9 8 4 12; 1 10 11 4;19 6 15 7];
and my polynomial operator for n=4 is:
[M] = [p0 p1 p2 p3]=[1 -3 1 -3;1 -1 -1 9;1 1 -1 -9;1 3 1 3]
After finding the outer product:
[20 -24 24 -16; -24 84 -80 24;24 -80 84 -24;-16 24 -24 20]
when I applied this on input image blocks, my answer is:
[-396 172 88 -104; 1012 -248 -376 616 -972 320 436 -552; 492 -104 4 172]
which is wrong. It should be
[140 0 -6 -10; 32 -112 2 -174; 22 -30 8 -40;34 -54 84 -8]
what I have done wrong?
My MATLAB code to find orthogonal polynomial operator is given below.
clc
clear all
close all
ID=imread('cameraman.tif'); % input image
I=double(ID);
imshow(ID);
[r,c]=size(I);
y1=zeros(r,c);
x=1:4;
n=4;
mu=(n+1)/2;
p0=[1 1 1 1];
p1=x-mu;
p2=(x-mu).^2-(n.^2-1)/12;
p3=(x-mu).^3-((x-mu)*(3*n.^2-7))/20;
P=[p0;p1; p2; p3];
N=[1 1 1 1;-1.5 -0.5 0.5 1.5;1 -1 -1 1;-0.3 0.9 -0.9 0.3];
M=[1 1 1 1;-3 -1 1 3;1 -1 -1 1;-3 9 -9 3]; % after sacling
v=M';
O=M.*v;
T=O'; % Orthogonal polynomial operator
for i=1:4:r
for j=1:4:c
y1(i:i+3,j:j+3)=I(i:i+3,j:j+3).*(double(T));
end
end
figure,imshow(uint8(y1))
y1

matlab : vectorization and fitlm

I have a vectorization problem with nlinfit.
Let A = (n,p) the matrix of observations and t(1,p) the explanatory variable.
For ex
t=[0 1 2 3 4 5 6 7]
and
A=[3.12E-04 7.73E-04 3.58E-04 5.05E-04 4.02E-04 5.20E-04 1.84E-04 3.70E-04
3.38E-04 3.34E-04 3.28E-04 4.98E-04 5.19E-04 5.05E-04 1.97E-04 2.88E-04
1.09E-04 3.64E-04 1.82E-04 2.91E-04 1.82E-04 3.62E-04 4.65E-04 3.89E-04
2.70E-04 3.37E-04 2.03E-04 1.70E-04 1.37E-04 2.08E-04 1.05E-04 2.45E-04
3.70E-04 3.34E-04 2.63E-04 3.21E-04 2.52E-04 2.81E-04 6.25E+09 2.51E-04
3.11E-04 3.68E-04 3.65E-04 2.71E-04 2.69E-04 1.49E-04 2.97E-04 4.70E-04
5.48E-04 4.12E-04 5.55E-04 5.94E-04 6.10E-04 5.44E-04 5.67E-04 4.53E-04
....
]
I want to estimate a linear model for each row of A without looping and avoid the loop
for i=1:7
ml[i]=fitlm(A(i,:),t);
end
Thanks for your help !
Luc
I believe that your probem is about undertanding how fitlm works, for matrix:
Let's work with the hald example for matlab:
>> load hald
>> Description
Description =
== Portland Cement Data ==
Multiple regression data
ingredients (%):
column1: 3CaO.Al2O3 (tricalcium aluminate)
column2: 3CaO.SiO2 (tricalcium silicate)
column3: 4CaO.Al2O3.Fe2O3 (tetracalcium aluminoferrite)
column4: 2CaO.SiO2 (beta-dicalcium silicate)
heat (cal/gm):
heat of hardening after 180 days
Source:
Woods,H., H. Steinour, H. Starke,
"Effect of Composition of Portland Cement on Heat Evolved
during Hardening," Industrial and Engineering Chemistry,
v.24 no.11 (1932), pp.1207-1214.
Reference:
Hald,A., Statistical Theory with Engineering Applications,
Wiley, 1960.
>> ingredients
ingredients =
7 26 6 60
1 29 15 52
11 56 8 20
11 31 8 47
7 52 6 33
11 55 9 22
3 71 17 6
1 31 22 44
2 54 18 22
21 47 4 26
1 40 23 34
11 66 9 12
10 68 8 12
>> heat
heat =
78.5000
74.3000
104.3000
87.6000
95.9000
109.2000
102.7000
72.5000
93.1000
115.9000
83.8000
113.3000
109.4000
This means that you have a matrix ingredients column % of ingredients in a component
>> sum(ingredients(1,:))
ans =
99 % so it is near 100%
and the rows are the 13 measures of the prodcut and the heat vector, the heat at the observation was taken.
>> mdl = fitlm(ingredients,heat)
mdl =
Linear regression model:
y ~ 1 + x1 + x2 + x3 + x4
Estimated Coefficients:
Estimate SE tStat pValue
________ _______ ________ ________
(Intercept) 62.405 70.071 0.8906 0.39913
x1 1.5511 0.74477 2.0827 0.070822
x2 0.51017 0.72379 0.70486 0.5009
x3 0.10191 0.75471 0.13503 0.89592
x4 -0.14406 0.70905 -0.20317 0.84407
Number of observations: 13, Error degrees of freedom: 8
Root Mean Squared Error: 2.45
R-squared: 0.982, Adjusted R-Squared 0.974
F-statistic vs. constant model: 111, p-value = 4.76e-07
So in your case, it not have sense to measure for each observation separately. is simply with t the same number of elements than observations.
take a look here
mdl = fitllm(A,t)
Problem solved using sapply and findgroups !

matlab: interpolation vector with a lot of duplicate values

Maybe my question might seem a little strange.
I have 3 vectors x1, y1, and x2
x1 and x2 have a lot of duplicate values, I would like to get y2 by interpolation with the same lenght of y1
y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1=[10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
(actually have greater length, but always the same for all)
Is it impossible or a non-sense question? (in any case, my problem would remain unsolved)