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 CenterFile Exchange에서 Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by