changing expression to function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Dyuman Joshi
2024년 1월 15일
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
Dyuman Joshi
2024년 1월 15일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!