How can I run multiple symbolic functions simultaneously

2 次查看(过去 30 天)
Dear all,
I have symbolic Matlab codes and I want to run three symbolic functions simultaneously. But I noticed that the main program does not run correctly. I need to find the difference UXT(k)-WK(k) but it gives only values of WK(k). I add the codes. Any help would be appretiated. Thanks in advance.
function [y]=karalamax()
clc
clear all
syms x t epsilon eta zeta xsi N m k tau
[WK] =worker1k()
Y=[];
UXT=[];
for k=1:8
WK(k)
x=1;
tau=1;
epsilon=1;
UXT=(k-1)*tau*epsilon*x;
UXT(k)=UXT
Y(k)=UXT(k)-WK(k)
% YY(k)=(UXT(k)-WK(k))
end
this is my main program.
function [WK] =worker1k(WK1,WK2)
clc
clear all
syms x t epsilon eta zeta xsi N m tau
[WK1,WK2] =symk();
% [FK]=worker3();
% syms x t alpha beta epsilon tau eta zeta xsi N m k
WK=[];
FK=[];
alpha=1/2;
beta=-1/2;
N=3;
for k=1:6
x=1;
tau=1;
epsilon=1;
FK(k)=epsilon*x*((k+1)*tau+1);
%
% FKKK(k)=subs(fk,{x,tau,epsilon,N},{5,0.02,0.001,50}) ;
% FKK=simplify(FKKK(k));
% FK(k)=eval(FKK);
WK(1)=WK1;
WK(2)=WK2;
% WK(1)=subs(w0,{x,tau,epsilon,N},{10,1,2,10});
% WK(2)=subs(w1,{x,tau,epsilon,N},{10,1,2,10});
WK(k+2)=-WK(k)+WK(k+1)+FK(k);
end
end
This is the third one
function [WK1,WK2] =symk()
% clc
% clear all
syms f(x) x t epsilon tau eta zeta xsi N m
N=4;
for k=1:6
x=1;
tau=1;
epsilon=1;
alpha=1/2;
beta=-1/2;
w0=epsilon*x*tau;
WK1=w0;
w1=2*epsilon*x*tau;
WK2=w1;
% W1=subs(w0,{x,tau,epsilon,N},{5,0.02,0.001,50});
% WW1=simplify(W1);
% WK1=eval(WW1);
% W2=subs(w1,{x,tau,epsilon,N},{5,0.02,0.001,50});
% WW2=simplify(W2);
% WK2=eval(WW2);
% simplify(WK1)
% simplify(WK2)
% çıkan fonksiyon sonucu command window da yazdım ve direk sayısal
% değerini verdi
end
end

采纳的回答

Milan Bansal
Milan Bansal 2024-4-1
Hi Ozgur,
I understand that you are trying to run three symbolic functions simultaneously in MATLAB and encountering issues with the main program not correctly calculating the difference UXT(k) - WK(k), where it seems to only return values of WK(k).
The primary issue seems to stem from how arrays are being handled and updated within the loop in the "karalamax" function. Specifically, the line UXT(k) = UXT within the loop is problematic because UXT is being reused in a manner that conflicts with MATLAB's handling of array indices and assignments. Additionally, the use of "clear all" within functions can lead to unexpected behavior.
Please refer to the code snippet below to modify the "karalamax" function.
function [Y] = karalamax()
clc;
% clear all; % Consider removing or commenting out when debugging
syms x t epsilon eta zeta xsi N m k tau;
[WK] = worker1k();
Y = zeros(1,8); % Pre-allocate for efficiency
UXT = zeros(1,8); % Pre-allocate to avoid dynamic resizing
for k = 1:8
x = 1;
tau = 1;
epsilon = 1;
UXT(k) = (k-1) * tau * epsilon * x; % Directly assign to UXT(k)
Y(k) = UXT(k) - WK(k);
end
end
Please refer to following documentation link to learn more about Symbolic Math Toolbox.
Hope this helps.

更多回答(1 个)

Walter Roberson
Walter Roberson 2024-4-1
In order to run three symbolic functions simultaneously, you can use a background pool and parfeval() the execution of each of the functions. Alternately if you have the Parallel Computing Toolbox then you can use parfor or spmd or parfeval

Community Treasure Hunt

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

Start Hunting!

Translated by