Using fsolve with gpuArray

5 次查看(过去 30 天)
JONAH TAYLOR
JONAH TAYLOR 2022-12-4
回答: Kartik 2023-3-21
I have a system of 5 non-linear equations I am trying to solve using fsolve. I'm calling fsolve many times for a bunch of different combinations of initial guesses for each of the 5 variables to see which one produces the most accurate result. I have defined the following initial guess vectors for each variable I am soliving for:
a = gpuArray([a1 a2 ... aN]);
b = gpuArray([b1 b2 ... bN]);
c = gpuArray([c1 c2 ... cN]);
d = gpuArray([d1 d2 ... dN]);
e = gpuArray( [e1 e2 ... eN]);
I then created a grid of initial guesses to solve over:
[aa,bb,cc,dd,ee] = ndgrid(a,b,c,d,e);
Finnally I used arrayfun to compute fsolve over this grid. The function max_Jemit(x, n0, kTe_eV, Vc) is the function containing my 5 equations:
[Solutionsx, Solutions_Val] = arrayfun(@(x01,x02,x03,x04,x05) fsolve(@(x) max_Jemit(x, n0, kTe_eV, Vc), [x01 x02 x03 x04 x05], opts), aa, bb, cc, dd, ee);
I get the following error:
Creating anonymous functions is not supported.
I am assuming it's referring to the line @(x01,x02,x03,x04,x05). However, that seems necessary to keep in so arrayfun knows where to put those arrays. This code works just fine using normal arrays (provided I include 'UniformOut', false). Is there a workaround for this?

回答(1 个)

Kartik
Kartik 2023-3-21
Hi,
It seems that the error message you are getting is related to the fact that anonymous functions are not supported in GPU arrays.
One possible workaround is to use a loop to call 'fsolve' for each initial guess combination, instead of using 'arrayfun'. This may not be as fast as using 'arrayfun', but it should still work.

类别

Help CenterFile Exchange 中查找有关 GPU Computing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by