changing expression to function
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
i have the values for a=[],b=[],reg1=[],reg2=[], i want to change the 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)) into a function.
expr = optimexpr;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
%syms expr x y w
disp(expr)
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));
is the following method is correct:
syms expr x y w
disp(expr)
ht=matlabFunction(expr);
Also, if I want to perform minimization of the 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)), after changing into a function, please suggest a way
0 Kommentare
Antworten (1)
Dyuman Joshi
am 15 Jan. 2024
You will have to define x and y as symbolic variables first -
%Random values for example
a = rand(1,10);
b = rand(1,10);
reg1 = randperm(n);
reg2 = randperm(n);
w = randperm(n);
n = numel(a);
syms x y [1 n]
size(x)
size(y)
expr = 0;
for i = 1:numel(a)
r1 = reg1(i);
r2 = reg2(i);
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));
end
%display the expression
disp(expr)
%Convert the expression to a function handle with vector inputs arguments
F = matlabFunction(expr, "Vars", {x, y})
2 Kommentare
Dyuman Joshi
am 15 Jan. 2024
Do you have the Symbolic Math Toolbox licensed and installed?
Type "ver" and see if you have it installed
ver
If you don't have it installed, run this code and see if you have the Toolbox licensed or not -
[status,errmsg] = license('checkout','Symbolic_Toolbox')
If status is 1, you have it licensed. You can download and install the toolbox from the Add-ons.
If status is 0, you will need to purchase the toolbox.
Siehe auch
Kategorien
Mehr zu Assumptions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!