How to write objective function with one single vector variable?

Hi.
I'm now working filter design. The objective function takes only one variable, which should be a vector (length around 15). Now I want to know how to write an objective function with only one single vector variable. Also I want to know which solver should I choose. I see that fmincon is a commonly used method but I don't know the format of the objective function for my case.
Here's my objective function:
all = norm((g_etau + g_dev_etau*x),inf);
first = all((g_sample_num*g_pass_area(1)):(g_sample_num*g_pass_area(2)),:);
second = g_lambada_p * (g_lr_pass + g_gradient_lr_pass * x);
third = g_lambada_s*(g_lr_stop + g_gradient_lr_stop*x);
output = norm(first,inf)+norm(second,inf)+norm(third,inf);
You can see that all the x inside are the variables and others are fixed value. How should I reformat this so I can use any solvers to solve this problem?

 Accepted Answer

function output = objective(x, g_etau, g_dev_etau, g_sample_num, g_pass_area, g_lambada_p, g_lr_pass, g_gradient_lr_pass, g_lambada_s, g_lr_stop, g_gradient_lr_stop)
all = norm((g_etau + g_dev_etau*x),inf);
first = all((g_sample_num*g_pass_area(1)):(g_sample_num*g_pass_area(2)),:);
second = g_lambada_p * (g_lr_pass + g_gradient_lr_pass * x);
third = g_lambada_s*(g_lr_stop + g_gradient_lr_stop*x);
output = norm(first,inf)+norm(second,inf)+norm(third,inf);
end
Together with
g_etau = something;
g_dev_etau = something_else;
g_sample_num = a_third_thing;
g_pass_area = a_fourth_thing_which_is_a_vector_of_length_2;
g_lambada_p = a_fifth_thing;
g_lr_pass = a_sixth_thing;
g_gradient_lr_pass = a_seventh_thing;
g_lambada_s = an_eighth_thing;
g_lr_stop = a_nineth_thing;
g_gradient_lr_stop = a_tenth_thing;
x_length = randi([13 17]); %(length around 15)
x_guess = rand(1, x_length);
A = []; b = []; %linear constraints
Aeq = []; beq = []; %equality constraints
lb = []; ub = []; %lower and upper bound constraints
nonlcon = []; %non-linear constraints
options = []; %options
best_x = fmincon( @(x) objective(x, g_etau, g_dev_etau, g_sample_num, g_pass_area, g_lambada_p, g_lr_pass, g_gradient_lr_pass, g_lambada_s, g_lr_stop, g_gradient_lr_stop), x_guess, A, b, Aeq, beq, lb, ub, nonlcon, options);

2 Comments

Thank you so much! So this means that the fmincon can directly handle a vector varibale right?
Sure, no problem.
It can handle array variables too, but you have to know how it thinks about array variables if you are building linear constraints. (Which is to say, it reshapes them to a column)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!