How can i use symbolic variables in a parallel Computing (parfor)?

Hi guys,
I'm using 'parfor' instead of 'for' and performing some calculations with symbolic variables and matrices. However, I encountered some errors when using this function (figs belos).
Are there any solutions for this?
I attached the code.
thanks so much

 Akzeptierte Antwort

Matt J
Matt J am 29 Mär. 2025
Bearbeitet: Matt J am 29 Mär. 2025
Dlu_11_1= zeros([M,N,N,M], 'sym') ;
parfor t=1:(M*N)^2
[ki,kj,j,i]=ind2sub([M,N,N,M],t);
Dlu_11_1(t)=diff(dlU_11(ki,kj),U1(i,j));
end
Dlu_11_1=reshape( Dlu_11_1 , M*N, []);

4 Kommentare

asad
asad am 29 Mär. 2025
Bearbeitet: asad am 29 Mär. 2025
Dear Matt,
Thanks for your response.
I actually made a mistake in the size of the matrix 'Dlu_11'. It should be:
dlU_11= zeros([M*N,1], 'sym') ;
However, there is a problem—the matrix 'Dlu_11_1' obtained from the old code is different from the one generated by the code you proposed.
I have attached the results here for you.
asad
asad am 29 Mär. 2025
Verschoben: Matt J am 29 Mär. 2025
I really appreciate your help, Matt.
I have solved the problem by changing the code to the following form:
Dlu_11_1= zeros([M*N,N,M], 'sym') ;
parfor t=1:(M*N)^2
[s,j,i]=ind2sub([M*N,N,M],t);
Dlu_11_1(t)=diff(dlU_11(s),U1(i,j));
end
Dlu_11_1=reshape( Dlu_11_1 , M*N, []);
Thanks a lot!
This might be a little bit faster,
MN=M*N;
Dlu_11_1= zeros(MN, 'sym') ;
U1=U1.';
parfor t=1:(MN)^2
[I,J]=ind2sub([MN,MN],t);
Dlu_11_1(t)=diff(dlU_11(I),U1(J));
end
U1=U1.';
Dlu_11_1=reshape( Dlu_11_1 , MN, []);
Thanks again Matt.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Catalytic
Catalytic am 30 Mär. 2025
Bearbeitet: Catalytic am 30 Mär. 2025
Why not simply -
Dlu_11_1 = reshape( jacobian(dlU_11, U1) , M*N,N,M);

Kategorien

Produkte

Version

R2024b

Gefragt:

am 29 Mär. 2025

Bearbeitet:

am 30 Mär. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by