Clear Filters
Clear Filters

Optimization Problem definition the function

1 view (last 30 days)
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!

Accepted Answer

Dana on 25 Sep 2020
Edited: Dana on 25 Sep 2020
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.

More Answers (0)


Find more on Sparse Matrices 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!