expression to function using syms
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Torsten
le 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.
Réponse acceptée
Walter Roberson
le 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]});
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Equation Solving dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!