Optimization Problem definition the function

1 回表示 (過去 30 日間)
Emma-B 2020 年 9 月 25 日
コメント済み: Emma-B 2020 年 9 月 28 日
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;
FUN = FUN + Afun;
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 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.
  1 件のコメント
Emma-B 2020 年 9 月 28 日
Thanks so much, it was very helpfull!


その他の回答 (0 件)


Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by