Method Jacobi . Error: The variable x in a parfor cannot be classified.
5 views (last 30 days)
Show older comments
Hello. I have a problem with function " parfor ". Please help, thank you!
A = [5 -2 3 0 2;-3 9 1 -2 3;2 -1 -7 1 4; 4 3 -5 7 1; 3 2 -4 5 -7]
b = [-1 2 3 0.5 4]'
x = zeros(1,5)';
n=size(x,1);
normVal=Inf;
tol=0.001; itr=0;
tic
while normVal>tol
xold=x;
parfor i=1:n
sigma=0;
for j=1:n
if j~=i
sigma=sigma+A(i,j)*x(j);
end
end
x(i)=(1/A(i,i))*(b(i)-sigma);
end
itr=itr+1;
normVal=abs(xold-x);
end
toc
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f\n%f \n in %d iterations\n',x,itr);
0 Comments
Answers (1)
Edric Ellis
on 15 Mar 2018
parfor cannot run in this case because the iterations of your loop are order dependent. That is, to calculate a the value of x(i), you are using all the existing values of x that have been calculated in prior iterations.
To speed this code up, I would first look to vectorise all the operations. For instance, you can remove the inner loop by first creating a version of A with zero on the diagonal, and then using * to compute sigma in one go:
Azd = A - diag(diag(A));
...
% instead of for j=1:n loop, simply compute
sigma = Azd(i,:) * x;
0 Comments
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!