filling a matrix using for loops
4 views (last 30 days)
Show older comments
clc
clear all
Rmax=10;
Rcyl=.5;
Uinf=1;
Nr=50;
Ntheta=50;
theta=linspace(0,2*pi,Ntheta)
%discretize
dr=(Rmax-Rcyl)/(Nr-1);
dtheta= 2*pi/(Ntheta-1);
rj=zeros(Nr,1); %allocating rj vector
rtheta=zeros(Ntheta,1); %allocating rtheta vector
for jj=1:Nr
rj(jj)=Rcyl+(jj-1)*dr;
end
for kk=1:Ntheta
rtheta(kk)=(kk-1)*dtheta;
end
w=2/(1+sqrt(1-cos(pi/(Nr+1))*cos(pi/(Ntheta+1))));
term=((2./dr.^2)+(2./(rj.^2).*dtheta.^2)); %the term with psi on left side in eqn8
psi=zeros(Ntheta,Nr); %old psi
psi(1:Ntheta,1)=0; %BC's on wall side
psi(1:Ntheta,Nr)=Uinf*Rmax*sin(theta); %BC's on far field
psi(Ntheta,1:Nr)=0 ;%BC's on top
psi(1,1:Nr)= 0 ;%BC's on bottom
%SOR formula for new psi
psinew=zeros(Ntheta,Nr); %allocating new psi values
bracket=psi
for j=2:Nr-1
for k=2:Ntheta-1
psi(j,k)=((1./rj(j)).*((psi(j+1,k)-psi(j-1,k))./(2.*dr))+((psi(j+1,k)-psi(j-1,k))./(dr.^2))+(1./(rj(j).^2)).*((psi(j,k+1)+psi(j,k+1)))./(dtheta.^2)).*term;
deltapsi=bracket/((2/(dr^2))+(2./((rj.^2).*dtheta^2)))-psi(j,k)
psinew(j,k)=psi(j,k)+w.*deltapsi
end
end
im trying build a psinew matrix, based off psi(j,k) where psi(j,k) becvomes the bracket term. i get the error that "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 50-by-1." psi matrix should be a 50 by 50
any help is appreciated
0 Comments
Answers (1)
Walter Roberson
on 24 Feb 2023
Rmax=10;
Rcyl=.5;
Uinf=1;
Nr=50;
Ntheta=50;
theta=linspace(0,2*pi,Ntheta)
%discretize
dr=(Rmax-Rcyl)/(Nr-1);
dtheta= 2*pi/(Ntheta-1);
rj=zeros(Nr,1); %allocating rj vector
That is a vector
rtheta=zeros(Ntheta,1); %allocating rtheta vector
for jj=1:Nr
rj(jj)=Rcyl+(jj-1)*dr;
end
for kk=1:Ntheta
rtheta(kk)=(kk-1)*dtheta;
end
w=2/(1+sqrt(1-cos(pi/(Nr+1))*cos(pi/(Ntheta+1))));
term=((2./dr.^2)+(2./(rj.^2).*dtheta.^2)); %the term with psi on left side in eqn8
That uses all of rj, and rj is a vector, so term is non-scalar
psi=zeros(Ntheta,Nr); %old psi
psi(1:Ntheta,1)=0; %BC's on wall side
psi(1:Ntheta,Nr)=Uinf*Rmax*sin(theta); %BC's on far field
psi(Ntheta,1:Nr)=0 ;%BC's on top
psi(1,1:Nr)= 0 ;%BC's on bottom
%SOR formula for new psi
psinew=zeros(Ntheta,Nr); %allocating new psi values
bracket=psi
whos
for j=2:Nr-1
for k=2:Ntheta-1
psi(j,k)=((1./rj(j)).*((psi(j+1,k)-psi(j-1,k))./(2.*dr))+((psi(j+1,k)-psi(j-1,k))./(dr.^2))+(1./(rj(j).^2)).*((psi(j,k+1)+psi(j,k+1)))./(dtheta.^2)).*term;
That uses all of term on the right-hand side, and term is a vector, so the right hand side is a vector, but the left-hand side names a scalar destination.
deltapsi=bracket/((2/(dr^2))+(2./((rj.^2).*dtheta^2)))-psi(j,k)
psinew(j,k)=psi(j,k)+w.*deltapsi
end
end
3 Comments
Walter Roberson
on 24 Feb 2023
psi(j,k)=((1./rj(j)).*((psi(j+1,k)-psi(j-1,k))./(2.*dr))+((psi(j+1,k)-psi(j-1,k))./(dr.^2))+(1./(rj(j).^2)).*((psi(j,k+1)+psi(j,k+1)))./(dtheta.^2)).*term;
j and k are scalars
rj(j) is a scalar so 1./rj(j) is a scalar.
psi(j+1,k) is a scalar, and so is psi(j-1,k) so the subtraction is a scalar.
dr is a scalar.
So ((1./rj(j)).*((psi(j+1,k)-psi(j-1,k))./(2.*dr)) is operations only on scalars, giving a scalar result.
psi(j+1,k) is a scalar, as is psi(j-1,k) so the subtraction is a scalar. dr is a scalar. rj(j) is a scalar. So everything in ((psi(j+1,k)-psi(j-1,k))./(dr.^2))+(1./(rj(j).^2)) is operations on scalars, so that part is a scalar.
Likewise everything in (1./(rj(j).^2)).*((psi(j,k+1)+psi(j,k+1)))./(dtheta.^2)) is operations on scalars.
So every calculation on the line is a scalar. Until you get to the .*term part. term is a 50x1 vector. So you are multiplying some calculated scalar by a vector, which is going to give you a vector result. And you are trying to store that vector result into a scalar output location.
Perhaps you should be indexing term by something in the calculation, so that you get a scalar for that part?
See Also
Categories
Find more on Historical Contests 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!