I'm currently stuck trying to solve Laplaces equation (2D problem) using numerically solution. I have several cases I'm supposed to solve for (assignment), and I think I've solved one of them.
clc
clear all
L1=1;
L2=1;
nx=50; %Number of steps in space(x)
ny=50; %Number of steps in space(y)
niter=10000; %Number of iterations
dx=1/(nx-1); %Width of space step(x)
dy=1/(ny-1); %Width of space step(y)
x=0:dx:L1; %Range of x(0,2) and specifying the grid points
y=0:dy:L2; %Range of y(0,2) and specifying the grid points
[X,Y] = meshgrid(x,y);
u = zeros(ny,nx);
un = zeros(ny,nx);
%Randvillkor
u4 = sin(pi.*x);
ua = sin((pi.*X)./L1).*(cosh((pi.*Y).*(L2/L1))-(sinh((pi.*Y).*(L2/L1))/tanh(pi)));
ua(:,1)=0;
ua(ny,:)=0;
ua(1,:) = u4;
ua(ny,nx)=0;
ua(ny,1) = 0;
ua(:,nx) = 0;
u(:,nx)=0;
u(:,1)=0;
u(1,:)=0;
u(ny,:) = u4;
u(ny,nx)=0;
u(ny,1) = 0;
j = 2:nx-1;
i = 2:ny-1;
%u(i,j) =
for it = 1:niter
un=u;
u(i,j)=((un(i+1,j)+un(i-1,j))+(un(i,j+1)+un(i,j-1)))/(4);
%u(i,j)=(dy^2*(un(i+1,j)+un(i-1,j))+dx^2*(un(i,j+1)+un(i,j-1)))/(2*(dx^2+dy^2));
disp(sum((u(:)-un(:)).^2)) %konvergens
end
%mesh(ua)
%u
ua1 = flipud(ua);
%ua1
disp((sum((ua1(:)-u(:)).^2))./sum((ua1(:)+u(:)).^2))
subplot(1,2,1)
mesh(X,Y,ua1)
subplot(1,2,2)
mesh(X,Y,u)
The problem now is that my two lengths, L1 and L2, are not supposed to be 1 for my next problem, instead they need to be 2 and 3. As you understand, I will end up with problems with the discretization and definitely the boundary conditions as well (that's where I'm getting my error messages). The errormessage is:
Subscripted assignment dimension mismatch.
Error in pdenummeoscgustb (line 31)
u(ny,:) = u4;
My question is pretty broad, but I'm unsure how the discretization and how my boundary conditions should look for L1=2 and L2=3. Wasnt sure if this question belong here or on stack exchange.