expression to function using syms

1 回表示 (過去 30 日間)
Akhil
Akhil 2024 年 1 月 16 日
コメント済み: Akhil 2024 年 1 月 17 日
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 Center および File ExchangeOptimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by