MATLAB: I am having trouble where the error is coming from in this code “index out of bounds”

flow analysis

I see that when looking at u and v in the workspace their is only one number i would assume it would output at least 1000 data points for u and v. im thinking this has to be changed into a matrix but im not sure how. when ever change x= -10:10; i various data points but with the x=linspace(… it does not.
the error that the matlab outputs is
Attempted to access u(1,2); index out of bounds because numel(u)=1.
Error in Flow (line 42)
abs_V(i,j) = sqrt(u(i,j)^2+v(i,j)^2);
-from this code-
{
%%Definition of the grid
% Decrease N if you have memory problems
N = 1000;
x = linspace(-10,10,N);
y = linspace(-10,10,N);
[X,Y] = meshgrid(x,y);
%%Freestream conditions
V_inf = 20;
R = 3;
rho = 1.29;
P_inf = 101325;
% Total pressure calculation
Ptot = 101.583;
%%Flow choice
% Flow past a cylinder
u = [((-1+(x.^2-y.^2)*R^2)/(x.^2+y.^2).^2)*20];
v = [40*9*x.*y./(x.^2+y.^2).^2];
%%Dynamic and static pressure calculation
p = Ptot-(.5*rho*sqrt(u.^2+v.^2));
% get rid of the velocity and pressure information inside the cylinder
for i = 1:N
for j = 1:N
abs_V(i,j) = sqrt(u(i,j)^2+v(i,j)^2);
if X(i,j)^2 + Y(i,j)^2 <= R^2
p(i,j) = NaN;
u(i,j) = NaN;
v(i,j) = NaN;
abs_V(i,j) = NaN;
end
end
end
%%Plots
figure (1)
[C,h] = contourf(X,Y,abs_V,20);axis equal;view(2);colormap ('jet');colorbar;lighting phong;
a = streamslice(X,Y,u,v);
set(a,'Color','black','LineWidth',2);
t=colorbar;
set(get(t,'ylabel'),'string','V, m/s','fontsize',16,'fontname','Times New Roman','fontweight','bold')
axis ([min(x) max(x) min(y) max(y)])
set(gca,'fontsize',16,'fontname','Times New Roman','fontweight','bold');
set(gcf,'numbertitle','off','name','Velocity magnitude scalar plot')
xlabel ('x, m');
ylabel ('y, m');
set(gcf, 'color', [1 1 1] );
figure (2)
[C,h] = contourf(X,Y,p/1000,20);axis equal;view(2);colormap ('jet');colorbar;lighting phong;
a = streamslice(X,Y,u,v);
set(a,'Color','black','LineWidth',2);
t=colorbar;
set(get(t,'ylabel'),'string','p, kPa','fontsize',16,'fontname','Times New Roman','fontweight','bold')
axis ([min(x) max(x) min(y) max(y)])
set(gca,'fontsize',16,'fontname','Times New Roman','fontweight','bold');
set(gcf,'numbertitle','off','name','Static pressure scalar plot')
xlabel ('x, m');
ylabel ('y, m');
set(gcf, 'color', [1 1 1] );

Best Answer

You need to use the ‘X,Y’ values from your meshgrid call in calculating ‘u’ and ‘v’. You’re giving it vectors instead.
This works:
u = [((-1+(X.^2-Y.^2)*R^2)./(X.^2+Y.^2).^2)*20];
v = [40*9*X.*Y./(X.^2+Y.^2).^2];
abs_V = sqrt(u.^2+v.^2);
That should get you started.