How to assign values to an array with broadcasting

26 views (last 30 days)
hi in the code bwloe i recieve a message in
xi1 = x1(index_i);
xi2 = x2(index_i);
i receieve a warning
function [Is]=currentMoM()
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
[f,N,ra,k0,Z0,lambda,gap_angle] = parameter();
gamma_const=1.781;
%Phi0=zeros(N);
e=exp(1);
dftm=(2.*gap_angle)/(N-1);
n=1:N;
Phi0=-gap_angle+(n-1).*dftm;
%for jj = 1:N
%Phi0(jj)=(jj-1).*dftm;
%end
x1 = Phi0(:); % Create a column vector of x1 values
x2 = x1 + dftm; % x2 values depend on x1
lmn=zeros(N,N);
gm = zeros(1,N);
zmn = zeros(N,N);
%opts = optimset('RelTol', 1e-6, 'AbsTol', 1e-6);
%vim = zeros(1,N);
%vsn = zeros(1,N);
coeif=(Z0.*k0./4).*ra.*dftm;
coeifn=(Z0./2).*sin(k0.*ra.*dftm./2);
B=(4./(Z0.*k0));
A=(gamma_const./2).*k0.*ra;
parfor idx = 1:N*N
% Convert linear index to subscripts
[index_i, index_j] = ind2sub([N, N], idx);
% Precompute integration bounds
xi1 = x1(index_i);
xi2 = x2(index_i);
xj1 = x1(index_j);
xj2 = x2(index_j);
if index_i == index_j
% Handle diagonal elements
funa = @(x, y) triangle_basisn(x, index_i) .* triangle_basisn(y, index_j) .* ...
ra .* (1 - 1i .* (2/pi).* (log(A) + log(abs(x - y + 1e-10))));
lmn(idx) = integral2(funa, xi1, xi2, xj1, xj2, 'RelTol', 1e-6, 'AbsTol', 1e-6);
else
% Handle off-diagonal elements
funb = @(x, y) triangle_basisn(x, index_i) .* triangle_basisn(y, index_j) .* ...
ra .* besselh(0, 2, k0 .* ra .* sqrt(2 - 2 .* cos(x - y)) +1e-9)
lmn(idx) = integral2(funb, xi1, xi2, xj1, xj2, 'RelTol', 1e-6, 'AbsTol', 1e-6);
end
% Assign to zmn
zmn(idx) = lmn(idx);
end
% Reshape the result back to a 2D matrix
lmn = reshape(lmn, [N, N]);
zmn = reshape(zmn, [N, N]);
% Compute gm vector
parfor index_i = 1:N
func = @(x) B .* triangle_basisn(x, index_i) .* Efieldin(x);
gm(index_i) = integral(func, x1(index_i), x2(index_i), 'RelTol', 1e-6, 'AbsTol', 1e-6);
end
% Solve linear system
Is = linsolve(zmn, gm');
end

Answers (1)

Walter Roberson
Walter Roberson on 13 Dec 2024 at 5:59
xi1 = x1(index_i);
xi2 = x2(index_i);
xj1 = x1(index_j);
xj2 = x2(index_j);
You access x1 and x2 at two different locations. If you were to only access each of them at one location and that one location could be calculated through simple arithmetic on the parfor variable, then hypothetically you could probably slice the variable. But as it is, indexing at two different locations and that location determined by non-trivial calculations on the parfor variable... the only option is for the arrays to be broadcast arrays.
  5 Comments
Walter Roberson
Walter Roberson on 13 Dec 2024 at 19:48
I do not understand why zmn(idx) = lmn(idx)
function [Is]=currentMoM()
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
[f,N,ra,k0,Z0,lambda,gap_angle] = parameter();
gamma_const=1.781;
%Phi0=zeros(N);
e=exp(1);
dftm=(2.*gap_angle)/(N-1);
n=1:N;
Phi0=-gap_angle+(n-1).*dftm;
%for jj = 1:N
%Phi0(jj)=(jj-1).*dftm;
%end
x1 = Phi0(:); % Create a column vector of x1 values
x2 = x1 + dftm; % x2 values depend on x1
lmn=zeros(N,N);
gm = zeros(1,N);
zmn = zeros(N,N);
%opts = optimset('RelTol', 1e-6, 'AbsTol', 1e-6);
%vim = zeros(1,N);
%vsn = zeros(1,N);
coeif=(Z0.*k0./4).*ra.*dftm;
coeifn=(Z0./2).*sin(k0.*ra.*dftm./2);
B=(4./(Z0.*k0));
A=(gamma_const./2).*k0.*ra;
lmn = zeros(N,N);
for index_j = 1:N
xj1 = x1(index_j);
xj2 = x2(index_j);
lmn_i = zeros(N,1);
parfor index_i = 1:N
% Precompute integration bounds
xi1 = x1(index_i);
xi2 = x2(index_i);
if index_i == index_j
% Handle diagonal elements
funa = @(x, y) triangle_basisn(x, index_i) .* triangle_basisn(y, index_j) .* ...
ra .* (1 - 1i .* (2/pi).* (log(A) + log(abs(x - y + 1e-10))));
lmn_ii =
lmn_i(index_i) = integral2(funa, xi1, xi2, xj1, xj2, 'RelTol', 1e-6, 'AbsTol', 1e-6);
else
% Handle off-diagonal elements
funb = @(x, y) triangle_basisn(x, index_i) .* triangle_basisn(y, index_j) .* ...
ra .* besselh(0, 2, k0 .* ra .* sqrt(2 - 2 .* cos(x - y)) +1e-9)
lmn_ii = integral2(funb, xi1, xi2, xj1, xj2, 'RelTol', 1e-6, 'AbsTol', 1e-6);
end
lmn_i(index_i) = lmn_ii;
end
lmn(:,index_i) = lmn_i;
end
zmn = lmn;
% Compute gm vector
parfor index_i = 1:N
func = @(x) B .* triangle_basisn(x, index_i) .* Efieldin(x);
gm(index_i) = integral(func, x1(index_i), x2(index_i), 'RelTol', 1e-6, 'AbsTol', 1e-6);
end
% Solve linear system
Is = linsolve(zmn, gm');
end

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!