Filter löschen
Filter löschen

expression to function using syms

2 Ansichten (letzte 30 Tage)
Akhil
Akhil am 16 Jan. 2024
Kommentiert: Akhil am 17 Jan. 2024
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 Kommentar
Torsten
Torsten am 16 Jan. 2024
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Jan. 2024
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 Kommentar
Akhil
Akhil am 17 Jan. 2024
Can you suggest how to use fmincon inorder to find the values of x,y and w. I have values of a and b

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by