Need Help Understanding the 'function_oceanwave()' function

Hello, I found this code for creation of ocean waves but I am not able to understand the function oceanwave in the code. Please help me with this. Code:
%% wave propagation
% initialize matlab
clc;
clear all;
close all;
% time division
Td=0.1 ; % second
% total propagation time
Tp=100; % second
%
figure;
% maximize window to screen size
set(gcf,'units','normalized','outerposition',[0 0 1 0.7])
% loop
for t=0:Td:Tp%
% calculate wave field
[surfaceX,surfaceY,surfaceZ1] = function_oceanwave(5,0.5,-10,200,300,1,t);
[surfaceX,surfaceY,surfaceZ2] = function_oceanwave(8,0.2,30,200,300,1,t);
% superposition
surfaceZ=surfaceZ1+surfaceZ2;
%
tic
% left figure of wavefield
s1 = subplot(1,2,1);
surf(surfaceX,surfaceY,surfaceZ);
axis([0 200 0 300 -1.5 1.5]);
xlabel('west-east distance (m)');
ylabel('south-north distance (m)');
shading interp;
colorbar
colormap(jet)
view(3);
% right figure of wavefield
s2 = subplot(1,2,2);
surf(surfaceX,surfaceY,surfaceZ);
axis([0 200 0 300 -1.5 1.5]);
xlabel('west-east distance (m)');
ylabel('south-north distance (m)');
shading interp;
colorbar
colormap(jet)
view(2)
axis equal;
toc
% update plots
drawnow;
end
% ------ function file: function_oceanwave ----
function [surfaceX,surfaceY,surfaceZ] = function_oceanwave(period,height,direction,waterWidth,waterLength,gridSize,time)
% acceleration of gravity
g = 9.8; % m/s/s
% wave length
waveLength = g*period*period/(2*pi);
% wave phase
wavePhase =(2*pi/period)*time;
% surface grid
x = [0:gridSize/waveLength*2*pi:waterWidth/waveLength*2*pi];
y = [0:gridSize/waveLength*2*pi:waterLength/waveLength*2*pi];
[gridX,gridY] = meshgrid(x,y);
% directional factors
dirX=cos(deg2rad(direction+270));
dirY=sin(deg2rad(direction+270));
% water particle positions
[surfaceX,surfaceY] = meshgrid(0:1:length(x)-1,0:1:length(y)-1);
surfaceZ = (height/2)*cos(dirX*gridX +dirY*gridY - wavePhase);
end

Answers (1)

Yes, that's the problem when people write code and don't put in good comments. You don't understand it and now you want us to put in the missing comments the original author should have put in.
Looks like it makes a 2-D array of sin/cos waves and pseudocolors it.
I suggest asking the author or looking at the help for each function you don't understand, like meshgrid (which makes an array of x coordinates and y coordinates for a 2-D image). You should then insert better comments as you go. Once you're done giving a good comment for each line, you should understand it.
To learn other fundamental concepts, invest 2 hours of your time here:

1 Comment

@Image Analyst I found this code on Youtube that's the problem. I just wanted to understand the physics behind surfaceZ = (height/2)*cos(dirX*gridX +dirY*gridY - wavePhase);
Thank You

Sign in to comment.

Categories

Find more on Oceanography and Hydrology in Help Center and File Exchange

Asked:

on 8 Oct 2023

Commented:

on 8 Oct 2023

Community Treasure Hunt

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

Start Hunting!