Optimization Problem definition the function

1 次查看(过去 30 天)
Hi everyone,
I am trying to define a nonlinear optimization function with 3 unknown variables which I need to optimize.
The optimization function is very large and needs a for loop to be defined, furthermore there are no contraint.
But I am having difficulty defining the function within the for-loop, what I currently have is:
syms x1
syms x2
syms x3
table = readtable('data.csv');
array = table2array(table);
x = [x1,x2,x3]';
for i=1:100
Afun = @(x)((x(1)*array(i,1)-x(2)*array(i,2)+x(3)*array(i,3))^2);
if i==1
FUN = Afun;
else
FUN = FUN + Afun;
end
end
But I get an error where the functions do not want to add, generally I am not sure if I am approaching this type of problem correctly with this method.
Any thought? Thanks!

采纳的回答

Dana
Dana 2020-9-25
编辑:Dana 2020-9-25
When you do
f = @(x) ...
the variable f isn't a function, but a function handle, i.e., a sort of "pointer" that allows you to refer to the function you've created. As such, it doesn't make sense to add this handle to something.
A better approach to do what it seems you're after is, first of all, do away with the symoblic stuff. You don't need it. Assuming size(array)=[100 3], the following should do what you want:
table = readtable('data.csv');
array = table2array(table);
FUN = @(x) norm(array*x)^2;
Note that the function argument x passed in here should be a 3-element column vector.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by