changing expression to function

1 Ansicht (letzte 30 Tage)
Akhil
Akhil am 15 Jan. 2024
Bearbeitet: Walter Roberson am 15 Jan. 2024
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

Antworten (1)

Dyuman Joshi
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)
ans = 1×2
1 10
size(y)
ans = 1×2
1 10
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)
expr = 
%Convert the expression to a function handle with vector inputs arguments
F = matlabFunction(expr, "Vars", {x, y})
F = function_handle with value:
@(in1,in2)sqrt((in1(:,2)-8.014959555768938e-1).^2+(in2(:,2)-7.559548740512954e-1).^2).*4.0-sqrt((in1(:,5)-8.014959555768938e-1).^2+(in2(:,5)-7.559548740512954e-1).^2).*3.0+sqrt((in2(:,5)-4.61498021717956e-1).^2+(in1(:,5)-3.474030646560082e-1).^2).*8.0-sqrt((in2(:,9)-4.61498021717956e-1).^2+(in1(:,9)-3.474030646560082e-1).^2).*4.0-sqrt((in2(:,2)-9.29217944804744e-1).^2+(in1(:,2)-2.704418800038694e-1).^2).*7.0+sqrt((in2(:,3)-9.29217944804744e-1).^2+(in1(:,3)-2.704418800038694e-1).^2).*3.0-sqrt((in1(:,3)-4.377482537830102e-2).^2+(in2(:,3)-8.593346111369374e-1).^2).*6.0+sqrt((in1(:,6)-4.377482537830102e-2).^2+(in2(:,6)-8.593346111369374e-1).^2).*7.0+sqrt((in1(:,9)-6.910514959968014e-1).^2+(in2(:,9)-5.800488220855226e-1).^2).*5.0-sqrt((in1(:,10)-6.910514959968014e-1).^2+(in2(:,10)-5.800488220855226e-1).^2).*8.0-sqrt((in2(:,6)-4.29276521870975e-1).^2+(in1(:,6)-3.633048837974125e-1).^2)+sqrt((in2(:,8)-4.29276521870975e-1).^2+(in1(:,8)-3.633048837974125e-1).^2).*6.0-sqrt((in2(:,8)-4.666644464857098e-1).^2+(in1(:,8)-8.109265130783774e-1).^2).*5.0+sqrt((in2(:,10)-4.666644464857098e-1).^2+(in1(:,10)-8.109265130783774e-1).^2)
  2 Kommentare
Akhil
Akhil am 15 Jan. 2024
when i am modifying the above code as per my requirement:
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
syms x y w [1 26]
size(x);
size(y);
size(w);
% Define the expression as a function handle
expr_func = matlabFunction(expr, 'Vars', {x, y,w});
error is coming which says:Incorrect number or types of inputs or outputs for function 'matlabFunction'.
Dyuman Joshi
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
----------------------------------------------------------------------------------------------------- MATLAB Version: 23.2.0.2489961 (R2023b) Update 6 MATLAB License Number: 0 Operating System: Linux 5.4.233-0504233-generic #202302250651 SMP Sat Feb 25 12:26:27 UTC 2023 x86_64 Java Version: Java 1.8.0_292-b10 with AdoptOpenJDK OpenJDK 64-Bit Server VM mixed mode ----------------------------------------------------------------------------------------------------- MATLAB Version 23.2 (R2023b) Simulink Version 23.2 (R2023b) 5G Toolbox Version 23.2 (R2023b) Aerospace Blockset Version 23.2 (R2023b) Aerospace Toolbox Version 23.2 (R2023b) Antenna Toolbox Version 23.2 (R2023b) Audio Toolbox Version 23.2 (R2023b) Automated Driving Toolbox Version 23.2 (R2023b) Bioinformatics Toolbox Version 23.2 (R2023b) Bluetooth Toolbox Version 23.2 (R2023b) Communications Toolbox Version 23.2 (R2023b) Computer Vision Toolbox Version 23.2 (R2023b) Control System Toolbox Version 23.2 (R2023b) Curve Fitting Toolbox Version 23.2 (R2023b) DO Qualification Kit Version 23.2 (R2023b) DSP System Toolbox Version 23.2 (R2023b) Database Toolbox Version 23.2 (R2023b) Datafeed Toolbox Version 23.2 (R2023b) Deep Learning Toolbox Version 23.2 (R2023b) Econometrics Toolbox Version 23.2 (R2023b) Embedded Coder Version 23.2 (R2023b) Filter Design HDL Coder Version 23.2 (R2023b) Financial Instruments Toolbox Version 23.2 (R2023b) Financial Toolbox Version 23.2 (R2023b) Fixed-Point Designer Version 23.2 (R2023b) Fuzzy Logic Toolbox Version 23.2 (R2023b) Global Optimization Toolbox Version 23.2 (R2023b) HDL Coder Version 23.2 (R2023b) HDL Verifier Version 23.2 (R2023b) IEC Certification Kit Version 23.2 (R2023b) Image Acquisition Toolbox Version 23.2 (R2023b) Image Processing Toolbox Version 23.2 (R2023b) Industrial Communication Toolbox Version 23.2 (R2023b) Instrument Control Toolbox Version 23.2 (R2023b) LTE Toolbox Version 23.2 (R2023b) MATLAB Compiler Version 23.2 (R2023b) MATLAB Compiler SDK Version 23.2 (R2023b) MATLAB Report Generator Version 23.2 (R2023b) Mapping Toolbox Version 23.2 (R2023b) Mixed-Signal Blockset Version 23.2 (R2023b) Model Predictive Control Toolbox Version 23.2 (R2023b) Navigation Toolbox Version 23.2 (R2023b) Optimization Toolbox Version 23.2 (R2023b) Parallel Computing Toolbox Version 23.2 (R2023b) Partial Differential Equation Toolbox Version 23.2 (R2023b) Phased Array System Toolbox Version 23.2 (R2023b) Powertrain Blockset Version 23.2 (R2023b) Predictive Maintenance Toolbox Version 23.2 (R2023b) RF Blockset Version 23.2 (R2023b) RF Toolbox Version 23.2 (R2023b) Requirements Toolbox Version 23.2 (R2023b) Risk Management Toolbox Version 23.2 (R2023b) Robotics System Toolbox Version 23.2 (R2023b) Robust Control Toolbox Version 23.2 (R2023b) Sensor Fusion and Tracking Toolbox Version 23.2 (R2023b) SerDes Toolbox Version 23.2 (R2023b) Signal Processing Toolbox Version 23.2 (R2023b) SimBiology Version 23.2 (R2023b) SimEvents Version 23.2 (R2023b) Simscape Version 23.2 (R2023b) Simscape Driveline Version 23.2 (R2023b) Simscape Electrical Version 23.2 (R2023b) Simscape Fluids Version 23.2 (R2023b) Simscape Multibody Version 23.2 (R2023b) Simulink 3D Animation Version 23.2 (R2023b) Simulink Check Version 23.2 (R2023b) Simulink Code Inspector Version 23.2 (R2023b) Simulink Coder Version 23.2 (R2023b) Simulink Control Design Version 23.2 (R2023b) Simulink Coverage Version 23.2 (R2023b) Simulink Design Optimization Version 23.2 (R2023b) Simulink Design Verifier Version 23.2 (R2023b) Simulink Desktop Real-Time Version 23.2 (R2023b) Simulink PLC Coder Version 23.2 (R2023b) Simulink Real-Time Version 23.2 (R2023b) Simulink Report Generator Version 23.2 (R2023b) Simulink Test Version 23.2 (R2023b) Stateflow Version 23.2 (R2023b) Statistics and Machine Learning Toolbox Version 23.2 (R2023b) Symbolic Math Toolbox Version 23.2 (R2023b) System Identification Toolbox Version 23.2 (R2023b) Text Analytics Toolbox Version 23.2 (R2023b) Vehicle Dynamics Blockset Version 23.2 (R2023b) Vehicle Network Toolbox Version 23.2 (R2023b) Vision HDL Toolbox Version 23.2 (R2023b) WLAN Toolbox Version 23.2 (R2023b) Wavelet Toolbox Version 23.2 (R2023b) Wireless HDL Toolbox Version 23.2 (R2023b)
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')
status = 1
errmsg = 0×0 empty char array
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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by