Several Floaters moving on Faraday waves

2 views (last 30 days)
Thierry on 10 Jan 2023
Edited: Bhavana Ravirala on 24 Feb 2023
Why does this code not function? A big thank you to who can help me !
% Parameters
L = 1; % length of the container
w = 2*pi; % angular frequency of the driving force
d = 0.1; % depth of the container
g = 9.81; % acceleration due to gravity
rho = 1; % density of the fluid
A = 0.1; % amplitude of the driving force
% Discretization
Nx = 100; % number of points in the x direction
Ny = 100; % number of points in the y direction
dx = L/Nx; % spatial step in the x direction
dy = d/Ny; % spatial step in the y direction
x = linspace(0, L, Nx+1); % x-coordinates of the grid points
y = linspace(0, d, Ny+1); % y-coordinates of the grid points
% Time stepping
T = 10; % total simulation time
dt = 0.001; % time step
Nt = T/dt; % number of time steps
% Initialize variables
eta = zeros(Nx+1, Ny+1); % free surface elevation
h = zeros(Nx+1, Ny+1); % fluid depth
u = zeros(Nx+1, Ny+1); % x-velocity of the fluid
v = zeros(Nx+1, Ny+1); % y-velocity of the fluid
%Floater parameter
m = 1; % mass
k = 1; % spring constant
c = 0.1; % damping constant
x0 = 0.5; %initial x position
y0 = 0.5; %initial y position
% Time-stepping loop
for n = 1:Nt
% Compute the fluid depth at the new time step
h = eta + d;
% Compute the x-velocity of the fluid
u(2:end-1,2:end-1) = -g/w*(diff(eta, 1, 1)/dy + diff(h, 1, 2)/dx);
% Compute the y-velocity of the fluid
v(2:end-1,2:end-1) = g/w*(diff(eta, 1, 2)/dx - diff(h, 1, 1)/dy);
% Enforce the no-normal flow boundary condition at the top
u(:, end) = 0;
v(:, end) = 0;
% Compute the free surface elevation at the new time step
eta(2:end-1,2:end-1) = eta(2:end-1,2:end-1) - dt*rho*(diff(u, 1, 1)/dy + diff(v, 1, 2)/dx);
eta(:, 1) = eta(:, 1) + A*sin(w*n*dt);
% Floater position and velocity
x(n+1) = x(n) + dt*vx(n);
y(n+1) = y(n) + dt*vy(n);
vx(n+1) = vx(n) + (dt/m)*(-k*(x(n)-x0)-c*vx(n) + interp2(x,y,u,x(n),y(n)));
vy(n+1) = vy(n) + (dt/m)*(-k*(y(n)-y0)-c*vy(n) + interp2(x,y,v,x(n),y(n)));
end
Arrays have incompatible sizes for this operation.
% Plot the floater position
figure(1)
plot(x,y,'-o')
xlabel('x-position')
ylabel('y-position')
the cyclist on 10 Jan 2023
I just formatted your code here, and ran it. I assume that that is the error message you got when you ran it locally?

Bhavana Ravirala on 23 Feb 2023
Edited: Bhavana Ravirala on 24 Feb 2023
Hi Thierry,
I understand that you are getting the error Arrays have incompatible sizes for this operation’ while you are running the code.
This is because while you are computing the x-velocity and y-velocity of the fluid you are trying to add two matrices of different sizes.
%u(2:end-1,2:end-1) = -g/w*(diff(eta, 1, 1)/dy + diff(h, 1, 2)/dx);
i= diff(eta, 1, 1)/dy; % is of size 100x101
j= diff(h, 1, 2)/dx; % is of size 101x100
Hope this helps!
Thierry Rebetez on 23 Feb 2023
Hi Bhavana,
Thanks a lot for your comment! I understand the issue. Do you know what I have to change in the code in order to "by pass" the issue?
Thank you!
Thierry
Bhavana Ravirala on 24 Feb 2023
Hi Thierry,
Can you share an example which shows the expected result of addition of the matrices. So I can understand the use case better.
Thanks
Bhavana

Categories

Find more on Fluid Mechanics in Help Center and File Exchange

R2022b

Community Treasure Hunt

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

Start Hunting!