expression to function using syms

1 次查看(过去 30 天)
In the following code, i am trying to learn how to use syms to convert an expression to function. Is the following manner is correct. Also If i want to perform minimization using fminsearch how it can be done. The expression is =w(r2) * sqrt((x(r1)-a(i))^2 + (y(r1)-b(i))^2) - w(r1) * sqrt((x(r2)-a(i))^2 + (y(r2)-b(i))^2)
x=optimvar('x',1,26);
y=optimvar('y',1,26);
w=optimvar('w',1,26);
f = 0;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
f = f + w(r2) * sqrt((x(r1)-a(i))^2 + (y(r1)-b(i))^2) - w(r1) * sqrt((x(r2)-a(i))^2 + (y(r2)-b(i))^2);
end
x0=0;
y0=0;
w0=10;
%function f=ht(x,y,w)
% x=zeros(1,26);
% y=zeros(1,26);
%w=zeros(1,26);
%end
syms x y w [1 26]
%size(x);
%size(y);
%size(w);
expr_func = matlabFunction(f, 'Vars', {f,x, y,w,a,b,r1,r2});
also for expr_func = matlabFunction(f, 'Vars', {f,x, y,w,a,b,r1,r2}), error coming out is Incorrect number or types of inputs or outputs for function 'matlabFunction'.
How to rectify this
  1 个评论
Torsten
Torsten 2024-1-16
Is there any reason why w(i) = 0 and x,y arbitrary (1<=i<=26) does not solve your optimization ? In this case, f will be 0.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-1-16
Assuming a b reg1 reg2 are known ahead of time:
syms x [1 26]
syms y [1 26]
syms w [1 26]
f = 0;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
f = f + w(r2) * sqrt((x(r1)-a(i))^2 + (y(r1)-b(i))^2) - w(r1) * sqrt((x(r2)-a(i))^2 + (y(r2)-b(i))^2);
end
expr_func = matlabFunction(f, 'Vars', {[x, y, w]});
  1 个评论
Akhil
Akhil 2024-1-17
Can you suggest how to use fmincon inorder to find the values of x,y and w. I have values of a and b

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by