How to define handle for fmincon?

1 view (last 30 days)
Yaser
Yaser on 20 Jan 2018
Commented: Yaser on 22 Jan 2018
I am doing a minimization for a function using fmincon.
please see attached function code and solver script.
The objective function is defined as f in line 200.
The constraints matrix also ready [Aeq] in line 203.
The equality vector [beq] is in line 207.
The main function script is working very well.
The problem is that I cannot run the solver. (The solver refers to function code terms as "Undefined function or variable"). As if the solver can't find the function although the function and the solver are in the same path.
When I tried to fix that I got error "FMINCON only accepts inputs of data type double.".
Musab

Accepted Answer

Matt J
Matt J on 20 Jan 2018
Edited: Matt J on 20 Jan 2018
The main function script is working very well.
Nope. Your cwt.m function is not returning any actual numbers.
>> out=cwt(x0); class(out)
ans =
'sym'
You need to provide actual numeric values for the variables ('delta_a', 'delta_c', 'delta_t', 'Di'). You cannot define them with sym().
Also, Aeq and beq should not be generated inside cwt(). Your cwt_solver() function cannot see them there.
  9 Comments
Matt J
Matt J on 21 Jan 2018
Edited: Matt J on 21 Jan 2018
These are the variables I need the fmincon to find their value. I don't have a value for them. They are like x(1) and x(2) in your example.
If the delta_ variables are the unknowns you are trying to solve for, then they need to form the input vector to the objective function routine cwt(). In other words, cwt.m should have the general structure below. FMINCON will pass various test values for the vector "unknowns" to cwt() until it finds a minimum.
function f = cwt(unknowns)
delta_a=unknowns(1);
delta_c=unknowns(2);
delta_t=unknowns(3);
...
f= ... computations with delta_a,delta_c,delta_t ....
end
Note that in your original code, you do not have this. The input x2 designated in the first line,
function f = cwt(x2)
is never used in the calculation of f, because it gets overwritten in line 185 where you have,
x2 = [delta_a+delta_c; delta_a; delta_a; delta_a+delta_t];
It also appears from line 185 that x2 is not even the thing you are trying to solve for. It is itself some intermediate quantity depending on the unknowns delta_a,c,t. So, x2 should not form the input to cwt().
Yaser
Yaser on 22 Jan 2018
This did solve it.
Thank you so much

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!