Is the implementation of fmincon different in R2012a and R2014a? - matlab

on my PC, I use R2014a and my script runs fine. The simulation server of my university runs R2012a and there a get an error running my script.
Here is the error:
Warning: The default trust-region-reflective algorithm does not solve
problems with the constraints you have specified. FMINCON will use the
active-set algorithm instead. For information on applicable
algorithms, see Choosing the Algorithm in the documentation.
Error using svd Input to SVD must not contain NaN or Inf.
Error in pinv (line 29) [U,S,V] = svd(A,0);
Error in qpsub (line 463)
projSD = pinv(projH)*(-Zgf);
Error in nlconst (line 619)
[SD,lambda,exitflagqp,outputqp,howqp,ACTIND] ...
Error in fmincon (line 794)
[X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in simulation_coop (line 85)
[q, fval] = fmincon(neg_aux_fun, q0, [], [], [], [], [], ub, #(q)constraints( q, a_t, b_t, W, p_cell_lin, ss_lin,
h_cell2bs(:,:,n),
h_trans2bs(:,:,n), rate ),options);
With R2014a I get neither the warning nor the error. So do you have any idea what the problem might be?

This answer is more of a how-to:
First of all, check that you run your scripts on exactly the same data. Different data (type, size, values) might yield different results.
Check the release notes of the Optimization Toolbox. If there were any significant changes in it between R2012a and R2014a, you'll find those mentioned in the one/several of the release notes of the toolbox for R2012b, R2013a, R2013b or R2014b versions. Here is the list: http://de.mathworks.com/help/optim/release-notes.html
Later edit
As you may see in the page linked above, R2012b changes the default algorithm for fmincon.

Related

CPLEX Error 5002: %s is not convex - Least Square Problem with binary formulation

first post on SO.
I'm working on a quadratic optimization problem.
I'm getting the following error:
Aeq = beq = sostype =sosind = soswt = [] % for my specific problem
The others paramers look like correctly define.
Error using cplexmiqp
CPLEX Error 5002: %s is not convex.
Error in MIP_CPLEX_OptL2_CstrL0 (line 78)
[x,z,exitflag,output] = cplexmiqp(Hprim'*Hprim,(-y'*Hprim)',Aineq,bineq,Aeq,beq,sostype,sosind,soswt,lb,ub,ctype,z0,solveur);
I've already checked the properties of the matrix (it's semi-PSD).
The error only appears for a dimension greater than 600 variables (continuous and binary -> MIQP).
Below this value, the model works correctly.
Anybody got any ideas? Thx a lot :)
As the error message states, the problem is that your objective function is not convex. By default, CPLEX only handles convex objective functions in quadratic programs. You can however switch some parameters to make CPLEX accept (and solve for) non-convex objective functions.
You can find the details in the user manual in section CPLEX > User's Manual for CPLEX > Continuous optimization > Solving problems with a quadratic objective (QP).
In order to solve models with a non-convex quadratic objective, you have to set parameter Cplex.Param.optimalitytarget to 3 (find global optimum).

Why does the nanmean function error when running an example from the documentation?

I'm trying to calculate the mean value of a matrix ignoring NaN values with the nanmean function in MATLAB. However when I test the code that I copied from the MATLAB documentation, MATLAB throws an error saying:
Error using sum
Invalid option. Option must be 'double', 'native', 'default', 'omitnan' or 'includenan'.
Error in mean (line 111)
m = sum(x, dim, flag, 'omitnan');
Error in nanmean (line 17)
y = mean(varargin{:},'omitnan');
This is really confusing. Why should I see an error after using the code from the MATLAB documentation?
The 'official' code which produces this error is listed below:
X = reshape(1:30,[2 5 3]);
X([10:12 25]) = NaN
y = nanmean(X,'all')
I am using MATLAB version R2017a.
The 'all' option was introduced in the newest MATLAB release, R2018b. You are using an older version that doesn't have this feature.
Instead, reshape your matrix to a column:
y = nanmean(X(:))
If you have a MATLAB license, you can access documentation for your version of MATLAB here: https://www.mathworks.com/help/releases/R2017a/matlab/functionlist.html
You can also access the documentation for a function by typing doc nanmean in MATLAB. Then you always get to see the documentation corresponding to the version of MATLAB you are using.

Error when using cellfun to do regression on a cluster

I'm using Matlab 2016a on my university's cluster. My local PC has Matlab 2016b installed.
I'm attempting to do a very large regression using cellfun. The drought resistance and predictor layers are 2044x1572. The internal dimensions of "drought resistance" is 100x1 while the internal dimensions of "predictors" is 100x6, representing the different predictors for each cell.
d_regress=cellfun(#(x,y)regress(x,y),Drought_resistance,predictors,'UniformOutput',false);
When I run this code with sample data on my local PC I get no errors.
However, when I run this code on the cluster I get the error:
Error using *
Inner matrix dimensions must agree.
Error in regress (line 93)
b(perm) = R \ (Q'*y);
Error in #(x,y)regress(x,y)
So then I fix the inner matrix dimensions so that they are 1x100 and 100x6.
Then I get the following error, which makes sense because of how regress works.
Error using |
Matrix dimensions must agree.
Error in regress (line 66)
wasnan = (isnan(y) | any(isnan(X),2));
Error in #(x,y)regress(x,y)
I've been having to use mldivide on the cluster in order to do my regressions (mldivide gives me no errors), but I would prefer to use regress due to its additional functionality. Is there any way I can modify my code in order to use regress?

MATLAB error when solving simultaneous equations

I am running a MATLAB code which solves a set of non-linear simultaneous equations. The code can be found here. The data input for the code (in excel) can be found here.
I encounter the following error:
Error using sym/subsasgn (line 733)
Indexed assignment to empty SYM objects is supported only in the 0-by-0 case.
Error in Solution (line 69)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
Does anyone have any idea how I can resolve this?
When declaring a symbolic variable, you have to specify the dimensions, otherwise it's a scalar. Instead of syms x; use sym and set the dimension argument:
x=sym('x',[3,4])
Replace [3,4] with the dimensions you want.

Partial derivative with Matlab Symbolic Toolbox for Lagrangian equations of motion

I'm trying to derive Lagrangian equations of motion in Matlab using the symbolic toolbox. This involves partial derivatives of a function and your coordinates, but matlab seems to not accept this.
So I would do this in Matlab:
syms t x(t) % t: time, x(t) position dependent on time
m = sym('m'); % mass, a constant parameter
T = m/2*diff(x,t)^2; % kinetic energy
dTdx = diff(T,x);
ddTdxDotdt = diff( diff(T,diff(x,t)), t);
But as soon as I try to differentiate anything in x (or diff(x,t)), Matlab complains:
Error using mupadmex
Error in MuPAD command: The variable is invalid. [stdlib::diff]
Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));
Does anyone know the proper way of handling this?
Matlab ought to be able to do this as you have it written, but I think that it doesn't like taking derivatives with respect to a symfun. Type whos in the command window and you'll see that x is listed as a symfun while t is just a sym. The help for diff kind of indicates this limitation. It won't event try to take the derivative of a constant with respect to x(t): diff(1,x) "complains" just the same. Unless newer versions of Matlab fix this (I'm on R2012b) I think you only option may be to come up with a scheme using two instances of x.