changing expression to function and then optimize the function

조회 수: 1 (최근 30일)
Akhil
Akhil 2024년 1월 15일
편집: Matt J 2024년 1월 16일
for the follwing expresson:
expr = expr + 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));
I converted it to function by
syms expr x y w
ht = matlabFunction(expr);
Please suggest hw to minimize the above expression to find the values of multiple values of x,y and w.
I have data for the variable a,b,r1 and r2

답변 (1개)

Matt J
Matt J 2024년 1월 15일
편집: Matt J 2024년 1월 15일
Do not use syms or matlabFunction for a situation like this. Just write a plain function to compute the expression,
function expr=ht(xyw, a,b,r1,r2)
x=xyw(1);
y=xyw(2);
w=xyw(3)
expr=...
end
and then optimize it with, for example,
x0=... %initial guesses
y0=...
w0=...
xyw=fminunc(@(xyw) ht(xyw, a,b,r1,r2) , [x0,y0,w0])
  댓글 수: 2
Akhil
Akhil 2024년 1월 16일
is the following way is correct:
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
and kindly suggest how to perform the minimization
Matt J
Matt J 2024년 1월 16일
편집: Matt J 2024년 1월 16일
Now that I see the whole loop, I don't quite understand what x(r1) refers to. Please give the sizes/dimensions of all the variables. In particular, aren't x,y,w scalar unknowns? If so, what does it mean to index x with r1?

댓글을 달려면 로그인하십시오.

카테고리

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