"Unable to prove literally" error when trying to create a symbolic function based on an existing function - matlab

I need to find inverse of virtual value function of lognormal random variables. This is what I tried to do:
syms flogn(x,p1,p2)
% assume(p2<=0); %Adding this doesn't change the error
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);
glogn = finverse(flogn,x);
But I got the error:
Error using symengine
Unable to prove 'p2 <= 0' literally. Use 'isAlways' to test the statement mathematically.
Error in sym/subsindex (line 810)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in sym/privsubsasgn (line 1085)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 922)
C = privsubsasgn(L,R,inds{:});
Error in logncdf>locallogncdf (line 73)
sigma(sigma <= 0) = NaN;
Error in logncdf (line 47)
[varargout{1:max(1,nargout)}] = locallogncdf(uflag,x,varargin{:});
Error in Untitled5 (line 3)
flogn(x,p1,p2) = x - logncdf(x,p1,p2,'upper')/lognpdf(x,p1,p2);
I also tried with beta distribution, and got a similar error. How can I use logncdf with symbolic variables?

Note that you could have minimalize your sample code even more, like this:
syms flogn(x,p1,p2)
flogn(x,p1,p2) = logncdf(x,p1,p2);
It's shorter, produces the same error and helps us to focus on the source of the error message.
So, the error doesn't come from trying to inverse a function, but from trying to use an existing function for numerical calculations with symbolic variables.
The error comes from the fact that you want to create a symbolic function flogn based on existing function logncdf, and logncdf has a multiple comparisons.
With the command edit logncdf, you can read the source code of the function and see comparison at lines 73 and 76.
% Return NaN for out of range parameters.
sigma(sigma <= 0) = NaN;
% Negative data would create complex values, which erfc cannot handle.
x(x < 0) = 0;
Matlab cannot compare symbols so it throws errors.
Depending on what you really need, you can have different solutions.
Do you really need to symbolize the function flogn? Couldn't you just write it as a function then calculate the inverse of it (if it can be inversed...)?
If you really want to keep the symbolization, you can also rewrite your own function logncdf (with another name) so it does not have the comparisons. But it's still not guaranteed that you will find an inverse.

Related

Why does Matlab factorial function perceives an integer as a non-integer?

I'm trying to build a function in Matlab which generates a Taylor series around 0 for sine and the user can insert x (the value for which the sine is approximated) and a maximum error. Thus I want the function to check the maximum error and from this maximum it generates the amount of elements in the Taylor series.
I get the following error:
Error using factorial (line 20)
N must be an array of real non-negative integers.
Error in maxError (line 19)
y(x) = y(x) + (-1)^(j) * x^(2j+1)/factorial(2j+1)
Below my code.
function [n] = maxError(x,e);
%Computes number of iterations needed for a given absolute error.
n=1;
while abs(x)^(n+1)/factorial(n+1) >= e
n = n+1;
end
if mod(n,2) == 0
n=n+1;
end
y=#(x) x;
j=1;
while j<n
y(x) = y(x) + (-1)^(j) * x^(2j+1)/factorial(2j+1)
j=j+1;
end
return
I think I get the error because the factorial function can only take up integers, but the way I see it, I am feeding it an integer. Since j=1; and then gets larger by one per iteration, I don't see how Matlab can perceive this as something else than a integer.
Any help is appreciated.
You are using j as an indexing variable, which is also the complex number in Matlab, and your are forgetting a * multiply.
You can use j as a variable (not recommended!) but when you are putting a number in front of it, Matlab will stil interpret is as the complex number, and not as the variable.
Adding the multiplication symbol will solve the issue, but using i and j as variables will give you these hard to debug errors. If you had used a, the error would have been easier to understand:
>> a=10;
>> 2a+1
2a+1
↑
Error: Invalid expression. Check for missing multiplication operator, missing or
unbalanced delimiters, or other syntax error. To construct matrices, use brackets
instead of parentheses.

How can I Minimize this function with MATLAB?

I wrote the following function in MATLAB:
function EX_EFFICIENCY=EXERGY_EFFICIENCY_FUNCTION(CR,ER,PC,T0,P0)
I used the following order (ga):
x = ga(#EXERGY_EFFICIENCY_FUNCTION,5)
But it gets the error:
Not enough input arguments.
Error in EXERGY_EFFICIENCY_FUNCTION (line 22)
T7p=T0.*(PC.^((k-1)./k));
Error in createAnonymousFcn>#(x)fcn(x,FcnArgs{:}) (line 11) fcn_handle
= #(x) fcn(x,FcnArgs{:});
Error in makeState (line 47)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in gaunc (line 40) state =
makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 398)
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars, ...
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
How can I minimize this function?
What are the variables you want to minimize over? All five CR,ER,PC,T0,P0? Then you need to tell ga to use a length-5-vector and deal its elements to the input arguments of the function. Like this:
xopt = ga(#(x) EXERGY_EFFICIENCY_FUNCTION(x(1),x(2),x(3),x(4),x(5)), 5);
You can also fix some and optimize over the others of course, like this:
xopt = ga(#(x) EXERGY_EFFICIENCY_FUNCTION(x(1),x(2),PC,T0,P0), 2);
optimizes CR, ER for fixed values of PC,T0, and P0.

MATLAB errors when using Lagrange Multipliers?

I have code that, when run, should correctly use Lagrange Multipliers to find the maximum/minimum of a function here:
clear all
syms x y L;
f = x^4+2*y^4;
g = x^2+5*y^2+2*y^2-10;
firstpart=jacobian(f,[x y])-L*jacobian(g,[x y]);
[Lsoln,xsoln,ysoln]=solve(firstpart,x^2+5*y^2+2*y^2-10);
subs(f,{x,y},{xsoln,ysoln})
% The coordinates that correspond with the greatest and smallest values
% above are the maximum and minimum, respectively.
However, when I run it, I get four errors:
Error using sym.getEqnsVars>checkVariables (line 92) The second
argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 62)
checkVariables(vars);
Error in solve>getEqns (line 450) [eqns, vars] =
sym.getEqnsVars(argv{:});
Error in solve (line 225) [eqns,vars,options] = getEqns(varargin{:});
Could anyone help?
You are passing two equations as individual arguments zu solve, that is not possible. You have to put both into an array
[Lsoln,xsoln,ysoln]=solve([firstpart,x^2+5*y^2+2*y^2-10] );

How to use fmincon to optimize two control vectors of a function

I have a function of 2 different vector. These are the control vector (decision variables) of the function. I want to use fmincon to optimize this function and also get the both control vector results separately.
I have tried to use handle ,#, but I got an error.
The function is:
function f = myFS(x,sv) % x is a vector (5,1)
f = norm(x)^2-sigma*(sv(1)+sv(2));
end
%% I tried to write fmincone to consider both control vectors (x and sv)
[Xtemp(:,h2),Fval, fiasco] = fmincon(#(x,sv)myFS(x,sv)...
,xstart,[],[],[],[],VLB,VUB,#(x,sv)myCon(sv),options);
Here is the error I get:
Error using myFS (line 12) Not enough input arguments.
Error in fmincon (line 564)
initVals.f =
feval(funfcn{3},X,varargin{:});
Error in main_Econstraint (line 58) [Xtemp(:,h2),Fval, fiasco] =
fmincon('myFS',xstart,[],[],[],[],VLB,VUB,#(x,sv)myCon(sv),options);
Thanks
fmincon expects your function to be of a single variable, there is no getting around that, but see:
http://se.mathworks.com/help/optim/ug/passing-extra-parameters.html
for example, if both x, cv are variables of the optimization you can combine them and then split them in the actual objective
for example
x_cv = vertcat(x, cv) and then x = x_cv(1:5); cv = x_cv(6:end)'
if cv is not a variable of the optimization, then 'freeze it' as the link above suggests

"Unable to prove `expr` literally..." error when trying to compare a symbol inside a function

I just started learning MATLAB and I'm trying to normalize a bump function given by
function b = bump(x)
region1 = abs(x) < 1
b(region1) = (exp(-1./(1 - x(region1).^2)))
region2 = abs(x) >= 1
b(region2) = 0
end
To do this, I need to divide by the definite integral from -1 to 1. However, when I input
syms x;
int(bump(x), -1, 1)
I get a long error message, which says
Error using symengine (line 58)
Unable to prove 'abs(x) < 1' literally. To test the statement mathematically, use isAlways.
Error in sym/subsindex (line 1554)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in sym>privformat (line 2357)
x = subsindex(x)+1;
Error in sym/subsref (line 1578)
[inds{k},refs{k}] = privformat(inds{k});
Error in bump (line 3)
b(region1) = (exp(-1./(1 - x(region1).^2)))
I tried replacing abs(x)<1 with what I think is the suggested isAlways(abs(x)<1), and that removes the error, although it gives the wrong answer (it says the integral is zero).
I don't understand what does the error message means.
syms x defines x as a symbolic variable, invoking symbolic computation on x. This probably isn't what you want.
Instead, define x as some kind of input matrix, e.g. x = zeros(3);. Or, to do numeric integration, use the integral function:
integral(#bump, -1, 1)