I'm trying to execute following code in MATLAB R2018a. It worked perfectly in MATLAB 2014, but optimset seems to be depreciated, so it has been deleted. What to use instead?
F = [-310 -250 -450 -370];
A = [6 4 10 9];
b = [86];
lb = zeros(4,1);
options = optimset('LargeScale','off','interior-point','on');
[x,fval,exitflag,output,lambda] = linprog(f,A,b,[],[],lb,[],[],options);
That's the error:
Error using optimset (line 249)
Unrecognized parameter name 'interior-point'. Please see the options table in the documentation for a list of acceptable option parameters. Note that some parameters are only supported by OPTIMOPTIONS. Link to options table
Error in Untitled (line 5)
options = optimset('LargeScale','off','interior-point','on');
optimset still works, but the available options have changed. Nonetheless you should probably update the code to use optimoptions. There is a discussion about Choose Between optimoptions and optimset in the doc.
options = optimoptions('linprog');
options.Algorithm = 'interior-point';
The available options for linprog can be found here
Related
In the Excel VBA Editor's Immediate Window, I can do the following:
?ActiveSheet.Range("C3:D4").Range("C3:D4").Address
$E$5:$F$6
According to some simple tests, this doesn't seem to respond the same in Matlab. Here is the code to set up the COM interface for the tests:
excel = actxserver('Excel.Application');
excel.Visible=1;
wbks = excel.Workbooks;
wbks.Add
sht = wbks.Item(1).Sheets.Item(1)
%
% Run some range tests
%
try
excel.DisplayAlerts = 0; % Forgo save prompt on Close
end; try
wbk.Close
end; try
excel.Quit % Excel process still present
end; try
delete(excel) % Excel process disappears
end % try
Now, with sht being a Worksheet object, I get the following error instead:
K>> o=sht.Range('C3:D4')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o.Range('C3:D4').Address
Cannot find an exact (case-sensitive) match for 'Range'
The closest match is: range in C:\Program Files\MATLAB\Single_R2015b\toolbox\stats\stats\range.m
Did you mean: K>> o.range('C3:D4').Address
This is the wrong function, since range with an uncapitalized r is the internal Matab function. Hence, I pressed Ctrl-C to break out (otherwise, it complains about the incompatible argument).
To troubleshoot why Range is not recognized, check whether is is a method or property. When accessed via COM, Range is a method rather than a property:
K>> methods(o)
Methods for class Interface.Microsoft_Excel_14.0_Object_Library.Range:
<...snip...>
PrintPreview Table
Range TextToColumns
RemoveDuplicates UnMerge
<...snip...>
This further shows that Range is not a property (even though it is in VBA):
K>> get(o)
<...snip...>
QueryTable: 'Error: Object returned error code: 0x800A03EC'
Resize: [1x1 Interface.Microsoft_Excel_14.0_Object_Library.Range]
Row: 3
<...snip...>
Since properties are listed alphabetically, Range would show up after QueryTable if it was recognized as a property. However, it isn't listed in the above results.
As an alternative diagnostic step, I tried accessing Range using the dot notation (o.Range). Unfortunately, Matlab seems to got for its own native function range, which has nothing to do with an Excel Range.
So after all that diagnostic work....
THE QUESTION
For a given Range object, how does one access the Range method (as recognized by COM) or the Range property (as it is described in the VBA documentation)?
AFTERNOTE
There seem to be many discrepancies with in accessing Range properties and methods via the COM interface. In the Immediate Window, one can use the Offset property (it is desribed as a property):
? ActiveSheet.Range("C3:D4").Offset(2,3).Address
$F$5:$G$6
Over COM, not so much, even though get(o) shows Offset to be a valid property that returns a range:
K>> o=sht.Range('C3:D4')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o=o.Offset(2,2)
Index exceeds matrix dimensions.
Here is a solution I've reached by trial and error, refined with some help with Matlab technical support:
Invoke get as a method, then supply the desired property name. It works for property Offset.
K>> o = sht.Range('C3:D4')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o.get('Offset',2,3).Address
ans = $F$5:$G$6
It works for Range, too.
K>> o = sht.Range('C3:E5')
o = Interface.Microsoft_Excel_14.0_Object_Library.Range
K>> o.get('Range','B2:C3').Address
ans = $D$4:$E$5
The strange thing is that Range is a method according to COM (but a property according to the Excel VBA documentation). Despite being seen as a method when accessed via COM, it needs to be invoked using get rather than invoke. The latter yields an error:
K>> o.invoke('Range','B2:C3').Address
Error using Interface.Microsoft_Excel_14.0_Object_Library.Range/invoke.
Cannot find an exact (case-sensitive) match for 'Range'.
The closest match is: range in C:\Program Files\MATLAB\Single_R2015b\toolbox\stats\stats\range.m
I'd like to create a simple Simulink model containing a "MATLAB Function" block programmatically -- i.e. using Matlab code.
Thanks to this guide, I've managed to create a new model containing the block:
open_system(new_system('my_system'))
add_block('simulink/User-Defined Functions/MATLAB Function', 'my_system/my_func')
Usually, in order to edit the "MATLAB Function" block's code, one has to "open" the block by double-clicking on it then entering the new code.
However, I would like to set that code programmatically using e.g. set_param() or any relevant function.
For instance, to set the following as the block's code:
function y = fcn(v)
%#codegen
y = 2 * u;
I would like to use something like:
set_param('my_system/my_func', 'Script',...
'function y = fcn(u)\n%#codegen\n\ny = 2 * u;'...
);
I've looked at the output of get_param('my_system/my_func', 'ObjectParameters') and tried to guess which parameter might be used to set the block's function code: So far, I couldn't find any. Therefore, my question is:
Q: Is it possible, using only Matlab commands, to set the code of a "MATLAB Function" block in Simulink?
(As requested by #Ander Biguri, I've moved a solution that worked for me to a separete answer post. If anyone has an alternative/better approach, please feel free to post it as well)
Well, it seems that this question was asked here before. (perhaps formulated differently though?) I've managed to solve my issue using the following code:
sf = sfroot()
block = sf.find('Path','my_system/my_func','-isa','Stateflow.EMChart');
block.Script = sprintf('function y = fcn(u)\n%%#codegen\n\ny = 2 * u;')
I am using optimoptions function in MATLAB R2015a. The syntax is as follows :
options = optimoptions('fmincon','Display','iter','MaxFunEvals',3000000);
fx = #(x)modifiedLogLikelihood(x,len,ET,counta,vals,INT);
parameters = fmincon(fx,x0,[],[],[],[],lb,ub,[],options);
I would like to run the same code on R2011a, but I am getting the following error since optimoptions was introduced after R2011a.
Undefined function or method 'optimoptions' for input arguments of type 'char'.
I referred to this document, that suggested me to use optimset.
I tried to take their advice and wrote:
options = optimset('fmincon','Display','iter',3000000);
fx = #(x)modifiedLogLikelihood(x,len,ET,counta,vals,INT);
parameters = fmincon(fx,x0,[],[],[],[],lb,ub,[],options);
But it yields the following error:
Error using ==> optimset at 198
Unrecognized parameter name 'fmincon'. Please see the optimset reference page in the
documentation for a list of acceptable option parameters. Link to reference page.
How can I use optimset to get the equivalent set of parameters as I was able to obtain with optimoptions. I would be very grateful for your help.
It's helpful if you read the documentation page for a function when trying to use it.
With optimset you want to get the default parameters using just the 'fmincon' string as input and then use optimset again to modify just the parameters that you would like to change from their default value.
options = optimset('fmincon');
options = optimset(options, 'Display', 'iter', 'MaxFunEvals', 3000000);
I am getting the following error while running a program.
??? Undefined function or method 'struct2table' for input arguments of type 'struct'.
Error in ==> cellarray at 13
T=struct2table(parameter,'AsArray',true);
The program is as under
a=10;
b=15;
parameter(a).alpha_star=0;
parameter(b).gamma_star=0;
x=5;
for j=1:b
for i=1:a
parameter(i).alpha_star=x+i;
end
parameter(j).gamma_star = x^2+j;
end
T=struct2table(parameter,'AsArray',true);
Can you please tell me, where am I wrong?
I am guessing you have a matlab older than R2013b, and since do not have this function (you can check that via which struct2table).
you can try to work around it by using struct2array and reshape.
you can also use
alp=[];
gam =[];
for i=1:length(parameter)
if ~isempty(parameter(i).alpha_star)
alp(end+1)=(parameter(i).alpha_star);
end
if ~isempty(parameter(i).gamma_star)
gam(end+1)=(parameter(i).gamma_star);
end
end
to extract the values to seperate arrays and
fieldnames(parameter);
to get the fieldnames, this is dirty but if it is only needed for presentation...
So far from what I have read this error can be caused by confusing or redundant naming within the program but I don't think that is the issue here since everything is declared clearly. From what I can see in this my issue is coming from the declaration of piecewise that is then being run through integration below and therefor the program is attempting to access a array cell that doesn't exist. If this is the case I have so far been stumped at how to fix this issue. Any assistance with this problem would be greatly appreciated.
syms t k n
fct = #(t)evalin(symengine,['subs(piecewise([0 <= t and t < 2,',...
'sin((Pi*t^2)/4)],[t <= 2 and t < 3, 5*t-t^2-6], [t <=3 and t < 4, 0],',...
'[Otherwise, t-4]),t=',regexprep(mat2str(x),' ',','),')']);
evalin(symengine,'assume(k,Type::Integer)');
a = #(fct,t,k) int(fct*cos(k*pi*t/4)/4,t,-2,8);
b = #(fct,t,k) int(fct*sin(k*pi*t/4)/4,t,-2,8);
FourierSeries = #(fct,t,n) a(fct,t,0)/4 + ...
symsum(a(fct,t,k)*cos(k*pi*t/4) + b(fct,t,k)*sin(k*pi*t/4),k,1,n);
pretty(FourierSeries(t,25,1))
ezplot(FourierSeries(t,25,1),-2,8)
hold on
ezplot(fct,-2,8)
hold off
title('Partial sum with n=25')
The complete error text is as follows:
??? Attempt to reference field of non-structure array.
Error in ==> sym.int at 56
r = mupadmex('symobj::intdef',f.s,x.s,a.s,b.s);
Error in ==> #(fct,t,k)int(fct*cos(k*pi*t/4)/4,t,-2,8)
Error in ==>
#(fct,t,n)a(fct,t,0)/4+symsum(a(fct,t,k)*cos(k*pi*t/4)+b(fct,t,k)*sin(k*pi*t/4),k,1,n)
Error in ==> FourierProgram at 16 pretty(FourierSeries(t,25,1))
This was asked long ago, but I'll provide an answer since one was never given.
As your error indicates, the issue is with this line and how the anonymous function a is called:
a = #(fct,t,k) int(fct*cos(k*pi*t/4)/4,t,-2,8);
The sym/int function expects the second argument (the variable with which the integration is performed with respect to) to be a symbolic variable. However, you're calling FourierSeries(t,25,1), which passes the value 25 as the integration variable.
This bit of code should replicate the issue in your version of Matlab (back in 2011 when this was asked):
syms t k;
int(t*cos(k*pi*t/4)/4,25,-2,8)
However, in R2015a I now get a different (and a bit clearer) error message:
Cannot integrate with respect to ''. The integration variable must be a symbolic variable.