I am using the Opti Toolbox, a free optimization toolbox for Matlab. I am solving a Mixed Integer Nonlinear Program, a MINLP. Inside the Opti Toolbox, the MINLP solver used is SCIP.
I define my own objective as a separate function (fun argument in Opti), and this function needs to call other matlab functions which take double arguments.
The problem is that whenever Opti invokes my function to evaluate the objective, it first calls it using a vector of 'scipvar' objects and then it calls it again using a vector of 'double' objects. My obj function does not work with the scipvar objects, it returns an error.
I tried (just for testing) setting the output of my function for something fixed when the type is 'scipvar', and for the actual real thing when the type is 'double', and this doesn't work, changing the fixed value actually changes the final optimal value.
I basically need to convert a scipvar object to double, is this possible? Or is there any other alternative?
Thank you.
Ok, so after enlightenment by J. Currie, an Opti toolbox developer, I understood the cause of the problem above.
The first call to the objective with a vector of scipvar variables is actually a parser sweeping the objective function to see if it can be properly mapped to something that can be handled by SCIP. I reimplemented the objective function to use only methods allowed by scip - obtained by typing methods(scipvar) in matlab:
abs dot log minus mrdivide norm power rdivide sqrt times
display exp log10 mpower mtimes plus prod scipvar sum uminus
Once the objective could be parsed by scip my problem worked fine.
Related
I am just getting started with MATLAB and I have written a function to produce the binomial expansion of (x-a)^n when given x, a and n. As far as I can tell my code should work, but I do not seem to be using the function variables correctly.
function expand(a,n,x)
f = 0;
for k = 0:1:n
f = f + nchoosek(n,k).*x.^(n-k).*(-a).^k;
end
end
I need to be able to call the function and have it output f as the expanded polynomial in x, for example calling expand(1,3,x) should return x^3-3*x^2+3*x-1, but instead, calling it gives this error:
Unrecognized function or variable 'x'. It seems like it wants me to call the function with x being another number but I in fact need it to be able to be any letter to be used as the variable in the polynomial.
I know in Maple I would specify the variable type in the function to be x::name so I'm assuming there's something similar in MATLAB that I don't yet know.
Thanks for any help.
There are two ways to go about this:
Create x as a symbolic variable. For example, syms x; expand(a,n,x). This gives you the power of using the symbolic toolbox features like simplify(), but comes with a bit of a performance penalty. You should avoid using the symbolic toolbox in intensive calculations.
Return an anonymous function, f=#(X)sum(arrayfun(#(k)nchoosek....,1:n). This has better performance and does not require double(subs(...)) when you want an actual numeric value, but may be too difficult to implement for a beginner.
im using Matlab to trying to solve 2 equations with 2 variables.
I define the 2 functions, f2(n_1,n_2),f3(n_1,n_2) which both depend on f1(n_1,n_2), then I defined the vectorised function G(n_1,n_2) which contain both of them.
Later I defined a the desired stating point, and tried to solve. but when running the code it raise an error which I'm not fully understand.
the above mentioned is displayed in the code below:
the code:
clear, close all; clc
%Const
N0=25;
G1=1;G2=1;
a1=6;a2=3;
k1=1;k2=4;
%main
syms n_1 n_2
X_0=[-5;5];
f1=N0-a1.*n_1-a2.*n_2;
f2=f1.*G1.*n_1-k1.*n_1;
f3=f1.*G2.*n_2-k2.*n_2;
G=#(n_1,n_2) [f2;f3];
s = fsolve(G,X_0);
the error:
Error using fsolve (line 269)
FSOLVE requires all values returned by functions to be of data type double.
Error in Ex1_Q3_DavidS (line 37)
s = fsolve(G,X_0);
thanks
fsolve is a function that uses numerical methods to find the root of a numerical function.
A numerical function is, for example f=#(x)x^2=2;. In MATLAB, you can evaluate f() at any number and it will return a number, but there is no higher order mathematical abstraction to it. This is however the fastest way to do maths in a computer, as it is not a higher intelligence, just a glorified calculator.
Some people however, want to give higher intelligence to computers and coded very complex symbolic toolboxes that with sets of rules try to teach computers to think semi-like humans and solve symbolic equations, as you do in paper. To solve those equations a function called solve is introduced in MATLAB.
You are doing symbolic math, but using the numeric solver. It does not work, just use the symbolic solver for symbolic math.
I'm using scipy.optimize.minimize with the Newton-CG (Newton Conjugate Gradient) method since I have an objective function for which I know the analytical Jacobian and Hessian. However, I need to add a regularization term R=exp(max(s)) based on the maximum value inside the array parameter "s" that being fit. It isn't entirely obvious to me how to implement derivatives for R. Letting the minimization algorithm do numeric derivatives for the whole objective function isn't an option, by the way, because it is far too complex. Any thoughts, oh wise people of the web?
I have a density function f_N which is defined as follows (K_nu(z) is the modified Bessel function):
I want to compute the following integral for each value of N:
The following is the implementation of the above in matlab.
for N=1:100
syms z
f =#(z) (1/(gamma(N)*sqrt(pi))*(z/2).^(N-0.5).*besselk(0.5-N,z));
g = #(z) f(z).*log(f(z));
val=integral(g,0,Inf);
But when I run the above code, it's always returning NaN for varoious values of N with the following warning:
Warning: Infinite or Not-a-Number value encountered
Can someone suggest a simple way to do this or avoid this issue?
I don't think you are doing what you think you are doing. Your declaration of z as a symbol will get overridden by the function handle definition. That means the integral is not a symbolic one, but a numeric one. So what you simply need to do is to drop the function handle notation "#(z)" and do the integral symbolically...
My guess, without thoroughly analysing this, is that one of the points in you integration domain [0,inf] yields a value f (x)=inf, which would destroy a numeric integration technique, but perhaps not a symbolic one.
I'm trying to write a generic function for finding the cosine of a value inputted into the function. The formula for cosine that I'm using is:
n
cosx = sum((-1)^n*x^(2n)/(2n)!)
n=1
I've looked at the matlab documentation and this page implies that the "sum" function should be able to do it so I tried to test it by entering:
sum(x^n, n=1..3)
but it just gives me "Error: The expression to the left of the equals sign is not a valid target for an assignment".
Is summing an infinite series something that matlab is able to do by default or do I have to simulate it using a function and loops?
Well if you want to approximate it to a finite number of terms you can do it in Matlab without toolboxes or loops:
sumCos = #(x, n)(sum(((-1).^(0:n)).*(x.^(2*(0:n)))./(factorial(2*(0:n)))));
and then use it like this
sumCos(pi, 30)
The first parameter is the angle, the second is the number of terms you want to take the series to (i.e. effects the precision). This is a numerical solution which I think is really what you're after.
btw I took the liberty of correcting your initial sum, surely n must start from 0 if you are trying to approximate cos
If you want to understand my formula (which surely you do) then you need to read up on some essential Matlab basics namely the colon operator and then the concept of using . to perform element-wise operations.
In MATLAB itself, no, you cannot solve an infinite sum. You would have to estimate it as you suggested. The page you were looking at is part of the Symbolic Math toolbox, which is an add-on to MATLAB. In particular, you were looking at MuPAD, which is rather similar to Mathematica. It is a symbolic math workspace, whereas MATLAB is more of a numeric math workspace. If you own the Symbolic Math toolbox, you can either use MuPAD as you tried to above, or you can use the symsum function from within MATLAB itself to carry out sums of series.