Undefined operator '>' for input arguments of type 'msspoly' (optimization software) - matlab

I know there are many similar questions like the title ask here.
However, I still believe no of these discussions can solve my problem.
I am testing the following software "SPOTless" developed by MIT:
https://github.com/spot-toolbox/spotless
This is a software which transform the polynomial optimization problem to LP or SOCP. And then call the other software "mosek" to solve it.
This example is from the tutorial in:
https://github.com/spot-toolbox/spotless/tree/master/doc (please see the last example)
n = 2 ;
d = 4 ;
x = msspoly('x',n ) ;
basis = monomials(x , 0 : d ) ;
p = randn(length(basis))'*basis ;
g = 1 - x'*x ;
prog = spotsosprog ;
prog = prog.withIndeterminate(x) ;
[prog,r] = prog.newFree(1) ;
[prog,f] = prog.newFreePoly(x,monomials(x,0:d-2)) ;
prog = prog.withSOS(r-p-f*g) ;
sol = prog.minimize(r) ;
However, when run it, Matlab shows the following error:
The line 401 and the related part is the following:
function [pr,poly,coeff] = newFreePoly(pr,basis,n)
if nargin < 3, n = 1; end
if ~isempty(basis) && n > 0 % line 401
if ~pr.isPolyInIndet(basis)
error('Basis must be polynomial in the indeterminates.');
end
[pr,coeff] = pr.newFree(length(basis)*n);
poly = reshape(coeff,n,length(basis))*basis;
else
poly = [];
end
end
$n$ is an integer, so $n > 0$ is valid obviously.
I ask the related professor (not the author of this software); he told me he has not seen this problem before. The problem comes from one .m file in the software.
I am not sure if someone can help me test this software and this example and let me know the result or how to solve this error.
Note: if you really test this software (easy to install), please also install "mosek", which is a famous optimization software.
Note: if you do not want to install "mosek", please cross out the last two lines in the code.

Related

How do I define a variable from another function?

I have a multi-fuction script that is supposed to ask the user for 4 different cars and weigh them based on ratings to give the user the best car to purchase.
What I want to do is have a prompt for every car the user inputs so the user can put in data for each variable the user decides to use. However, when titling the prompt I want to use the cars name in the prompt. It seems impossible to me and Im not sure what to do, im very new to coding.
Main Script
prompt1 = {'How Many Cars (4): '};
title1 = 'Cars';
answer1 = inputdlg(prompt1, title1, [1 40]);
Q1 = str2double(answer1{1});
[N] = Group_Function1(Q1);
Car1 = N(1); %Stores the names of the cars
Car2 = N(2);
Car3 = N(3);
Car4 = N(4);
prompt2 = {'How Many Variables (4): '};
title2 = 'Variables';
answer2 = inputdlg(prompt2, title2, [1 50]);
fprintf('This code can accept costs between 0-100000\n');
fprintf('This code can accept top speeds between 0-200\n');
fprintf('This code can also accept the terms none, some, & alot\n');
fprintf('This code can accept safety ratings between 0-5\n');
Q2 = str2double(answer2{1});
[V,W] = Group_Function2(Q2);
W1 = W(1); %Stores the weights of the varibles
W2 = W(2);
W3 = W(3);
W4 = W(4);
for h=1:Q1
[H] = Group_Function3(V);
Weights(h,:)=H;
end
Group_Function1
function [N] = Group_Function1(Q1)
for Q = 1:Q1
prompt = {'Name of Car:'};
title = 'Car Name';
answer = inputdlg(prompt,title, [1 80])';
N(Q) = answer(1);
end
Group_Function2
function [V,W] = Group_Function2(Q2)
for Q=1:Q2
prompt = {'Variable? (Negative Variables First):','weights in decimal
form?'};
title = 'Variables and Weights';
answer = inputdlg(prompt,title, [1 80])';
V(Q)=answer(1);
W(Q)=str2double(answer{2});
s=sum(W);
end
if s~=1
fprintf('Weights do not add up to 1. Try Again!\n');
Group_Function2(Q2);
end
end
Group_Function3 (Where the problem occurs)
function [H] = Group_Function3(V)
prompt = {V};
title = ['Variable Ratings For' Group_Function1(answer{1})];
h = inputdlg(prompt, title, [1 80])';
end
The Problem
For 'Group_Function3' I want the prompt to include the users inputs from 'Group_Function1' so that when the prompt comes up to input the answers I know which vehicle I am entering for.
Each function runs in its own workspace, it means it does not know the state or content of variables outside of it. If you want a function to know something specific (like the name of a car), you have to give that to the function in the input parameters. A function can have several inputs parameters, you are not limited to only one.
Before going into the Group_Function3 , I'd like to propose a new way for Group_Function1.
Group_Function1 :
You run a loop to ask independantly for each car name. It is rather tedious to have to validate each dialog boxe. Here is a way to ask for the 4 car names in one go:
replace the beginning of your script with:
title1 = 'Cars';
prompt1 = {'How Many Cars (4): '};
answer1 = inputdlg(prompt1, title1 );
nCars = str2double( answer1{1} );
CarNames = getCarNames(nCars) ; % <= use this function
% [N] = Group_Function1(Q1); % instead of this one
and replace Group_Function1 with:
function CarNames = getCarNames(nCars)
title = 'Car Names';
prompt = cellstr( [repmat('Name of car #',nCars,1) , sprintf('%d',(1:nCars)).'] ) ;
CarNames = inputdlg( prompt, title, [1 80] ) ;
end
Now CarNames is a cell array containing the name of your 4 cars (as your variable N was doing earlier. I recommend sligthly more explicit variable names).
You can run the rest of your code as is (just replace N with CarNames, and Q1 with nCars).
Group_Function3 :
when you get to the Group_Function3, you have to send the current car name to the function (so it can use the name in the title or prompt). So replace your Group_Function3 as following (we add an input variable to the function definition):
function H = Group_Function3( V , thisCarName )
prompt = {V};
title = ['Variable Ratings For' thisCarName];
H = inputdlg(prompt, title, [1 80])';
end
and in your main script, call it that way:
for h = 1:nCars
thisCarName = carNames{h} ;
H = Group_Function3( V , thisCarName ) ;
% ...
% anything else you want to do in this loop
end

Julia Method Error converting Complex{Float64}

I'm novice to Julia and I have the following code with this error:
MethodError(convert,(Complex{Float64},[-1.0 - 1.0im])).
I would like to know the source of the error and how to optimize this piece of code for speed.
This is my code:
function OfdmSym()
N = 64
n = 1000
symbol = convert(Array{Complex{Float64},2},ones(n,64)) # I need Array{Complex{Float64},2}
data = convert(Array{Complex{Float64},2},ones(1,48)) # I need Array{Complex{Float64},2}
const unused = convert(Array{Complex{Float64},2},zeros(1,12))
const pilot = convert(Array{Complex{Float64},2},ones(1,4))
const s = convert(Array{Complex{Float64},2},[-1-im -1+im 1-im 1+im])# QPSK Complex Data
for i=1:n # generate 1000 symbols
for j = 1:48 # generate 48 complex data symbols whose basis is s
r = rand(1:4,1) # 1, 2, 3, or 4
data[j] = s[r]
end
symbol[i,:]=[data[1,1:10] pilot[1] data[1,11:20] pilot[2] data[1,21:30] pilot[3] data[1,31:40] pilot[4] data[1,41:48] unused]
end
end
As it's the first day programming in Julia, I tried very hard to reveal the source of the error without success. I also tried to optimize and initialize arrays as I could but when I time the code I realize that it is far from optimal. I appreciate your help.
Try this much simpler code
function OfdmSym()
N = 64
n = 1000
symbol = ones(Complex{Float64}, n, 64)
data = ones(Complex{Float64}, 1, 48)
unused = zeros(Complex{Float64}, 1, 12)
pilot = ones(Complex{Float64}, 1, 4)
s = [-1-im -1+im 1-im 1+im]
for i=1:n # generate 1000 symbols
for j = 1:48 # generate 48 complex data symbols whose basis is s
r = rand(1:4) # 1, 2, 3, or 4
data[j] = s[r]
end
symbol[i,:]=[data[1,1:10] pilot[1] data[1,11:20] pilot[2] data[1,21:30] pilot[3] data[1,31:40] pilot[4] data[1,41:48] unused]
end
end
OfdmSym()
I wouldn't worry too much about optimizing things until you have got it working correctly. The way you have it set up now seems like it'd be kinda inefficient due to all the slicing of arrays - it'd be better to try to build symbol directly.

I got error message about simulink "Output argument is not assigned on some execution paths"

In simulink, I made some model using "MATLAB function"block
but I met error message here.
here is code and error message.
function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)
if mode == 1
VTAS = initialVTAS + (a * t) ;
postVTAS = VTAS;
elseif mode == 2
datasize = length(preVTAS);
lastvalue = preVTAS(datasize);
VTAS = lastvalue + 0;
postVTAS = VTAS;
end
end
Output argument 'VTAS' is not assigned on some execution paths.
Function 'MATLAB Function' (#36.25.28), line 1, column 26:
"fcn"
Launch diagnostic report.
I think there is no problem about output "VTAS"
please teach me what is a problems.
As the compiler tells you, under some circumstances there is no output value assigned to VTAS. The reason is that you only assign values to that output if mode is 1 or 2. The compiler doesn't know what values are feasible for mode. To remedy this, you need to make sure that VTAS is assigned under any and all circumstances.
This could be accomplished by, e.g. adding an else construct, like so:
function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)
if mode == 1
VTAS = initialVTAS + (a * t) ;
postVTAS = VTAS;
elseif mode == 2
datasize = length(preVTAS);
lastvalue = preVTAS(datasize);
VTAS = lastvalue + 0;
postVTAS = VTAS;
else
VTAS = NaN;
postVTAS = NaN;
end
end
Edit:
Additionally, it would be good practice for the else case to throw an error. This would be helpful for debugging.
As a minor note, for every case, postVTAS is equal to VTAS, so essentially it is superfluous to return both from the function.

Error attempting to use initial conditions

I'm doing a quick problem in Maple with a differential equation and a few initial conditions, but I'm getting an error message that I can't seem to understand given the context. Can anyone quickly elaborate on what's going on here? How do I fix this issue?
> KVLl2 := -4*(i2(t)-2)-12*(i2(t)-i3(t)) = 0;
-16 i2(t) + 8 + 12 i3(t) = 0
> KVLl3 := -12*(i3(t)-i2(t))-4*i3(t)-3.5*(diff(i3(t), t)) = 0;
/ d \
-16 i3(t) + 12 i2(t) - 3.5 |--- i3(t)| = 0
\ dt /
> mySoln := dsolve({KVLl2, KVLl3, i2(0) = 1, i3(0) = 1}, i2, i3);
Error, (in dsolve) found the following equations not depending
on the unknowns of the input system: {1 = 1}
Thanks in advance
Maple doesn't know what to do with i2 and i3 you provided as target functions. If you look at the help of dsolve (?dsolve), you see that it requires its target functions to be specified in terms of their variables (t in this case) and as a list. Try using this
dsolve({KVLl2, KVLl3, i2(0) = 1, i3(0) = 1}, {i2(t), i3(t)});
No errors here but no solution either (this might be related to your equation)

Matlab function calling basic

I'm new to Matlab and now learning the basic grammar.
I've written the file GetBin.m:
function res = GetBin(num_bin, bin, val)
if val >= bin(num_bin - 1)
res = num_bin;
else
for i = (num_bin - 1) : 1
if val < bin(i)
res = i;
end
end
end
and I call it with:
num_bin = 5;
bin = [48.4,96.8,145.2,193.6]; % bin stands for the intermediate borders, so there are 5 bins
fea_val = GetBin(num_bin,bin,fea(1,1)) % fea is a pre-defined 280x4096 matrix
It returns error:
Error in GetBin (line 2)
if val >= bin(num_bin - 1)
Output argument "res" (and maybe others) not assigned during call to
"/Users/mac/Documents/MATLAB/GetBin.m>GetBin".
Could anybody tell me what's wrong here? Thanks.
You need to ensure that every possible path through your code assigns a value to res.
In your case, it looks like that's not the case, because you have a loop:
for i = (num_bins-1) : 1
...
end
That loop will never iterate (so it will never assign a value to res). You need to explicitly specify that it's a decrementing loop:
for i = (num_bins-1) : -1 : 1
...
end
For more info, see the documentation on the colon operator.
for i = (num_bin - 1) : -1 : 1