Need Help Understanding the '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 do help me with this. Code:
%% wave propagation
% initialize matlab
clc;
clear all;
close all;
% time division
Td = 0.1; % second
% total propagation time
Tp = 20; % 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
Elapsed time is 0.907147 seconds. Elapsed time is 0.292892 seconds. Elapsed time is 0.286320 seconds. Elapsed time is 0.141093 seconds. Elapsed time is 0.159926 seconds. Elapsed time is 0.122202 seconds. Elapsed time is 0.116322 seconds. Elapsed time is 0.123267 seconds. Elapsed time is 0.118437 seconds. Elapsed time is 0.118710 seconds. Elapsed time is 0.118743 seconds. Elapsed time is 0.118334 seconds. Elapsed time is 0.122403 seconds. Elapsed time is 0.117283 seconds. Elapsed time is 0.123602 seconds. Elapsed time is 0.117222 seconds. Elapsed time is 0.121096 seconds. Elapsed time is 0.119285 seconds. Elapsed time is 0.116784 seconds. Elapsed time is 0.117624 seconds. Elapsed time is 0.120168 seconds. Elapsed time is 0.126237 seconds. Elapsed time is 0.120535 seconds. Elapsed time is 0.118778 seconds. Elapsed time is 0.120666 seconds. Elapsed time is 0.119763 seconds. Elapsed time is 0.122269 seconds. Elapsed time is 0.123478 seconds. Elapsed time is 0.124565 seconds. Elapsed time is 0.124226 seconds. Elapsed time is 0.134298 seconds. Elapsed time is 0.124872 seconds. Elapsed time is 0.118357 seconds. Elapsed time is 0.117405 seconds. Elapsed time is 0.118011 seconds. Elapsed time is 0.137090 seconds. Elapsed time is 0.117531 seconds. Elapsed time is 0.119120 seconds. Elapsed time is 0.121766 seconds. Elapsed time is 0.129125 seconds. Elapsed time is 0.119379 seconds. Elapsed time is 0.117683 seconds. Elapsed time is 0.120702 seconds. Elapsed time is 0.118127 seconds. Elapsed time is 0.120316 seconds. Elapsed time is 0.127845 seconds. Elapsed time is 0.117358 seconds. Elapsed time is 0.115233 seconds. Elapsed time is 0.116686 seconds. Elapsed time is 0.121443 seconds. Elapsed time is 0.117942 seconds. Elapsed time is 0.130770 seconds. Elapsed time is 0.117263 seconds. Elapsed time is 0.121419 seconds. Elapsed time is 0.118109 seconds. Elapsed time is 0.138076 seconds. Elapsed time is 0.118121 seconds. Elapsed time is 0.118015 seconds. Elapsed time is 0.120088 seconds. Elapsed time is 0.126919 seconds. Elapsed time is 0.116207 seconds. Elapsed time is 0.119444 seconds. Elapsed time is 0.148592 seconds. Elapsed time is 0.134619 seconds. Elapsed time is 0.116208 seconds. Elapsed time is 0.118189 seconds. Elapsed time is 0.122815 seconds. Elapsed time is 0.123143 seconds. Elapsed time is 0.116544 seconds. Elapsed time is 0.119937 seconds. Elapsed time is 0.117241 seconds. Elapsed time is 0.128399 seconds. Elapsed time is 0.119393 seconds. Elapsed time is 0.116841 seconds. Elapsed time is 0.119622 seconds. Elapsed time is 0.115328 seconds. Elapsed time is 0.122472 seconds. Elapsed time is 0.133576 seconds. Elapsed time is 0.118851 seconds. Elapsed time is 0.118038 seconds. Elapsed time is 0.117819 seconds. Elapsed time is 0.122062 seconds. Elapsed time is 0.117098 seconds. Elapsed time is 0.168572 seconds. Elapsed time is 0.126678 seconds. Elapsed time is 0.124151 seconds. Elapsed time is 0.118346 seconds. Elapsed time is 0.116865 seconds. Elapsed time is 0.118455 seconds. Elapsed time is 0.117027 seconds. Elapsed time is 0.118931 seconds. Elapsed time is 0.127678 seconds. Elapsed time is 0.117170 seconds. Elapsed time is 0.114990 seconds. Elapsed time is 0.119609 seconds. Elapsed time is 0.117832 seconds. Elapsed time is 0.119356 seconds. Elapsed time is 0.119249 seconds. Elapsed time is 0.117666 seconds. Elapsed time is 0.124012 seconds. Elapsed time is 0.133029 seconds. Elapsed time is 0.118524 seconds. Elapsed time is 0.116490 seconds. Elapsed time is 0.117336 seconds. Elapsed time is 0.119500 seconds. Elapsed time is 0.121413 seconds. Elapsed time is 0.116181 seconds. Elapsed time is 0.117712 seconds. Elapsed time is 0.119652 seconds. Elapsed time is 0.119290 seconds. Elapsed time is 0.115873 seconds. Elapsed time is 0.117513 seconds. Elapsed time is 0.130843 seconds. Elapsed time is 0.118269 seconds. Elapsed time is 0.118171 seconds. Elapsed time is 0.116427 seconds. Elapsed time is 0.117069 seconds. Elapsed time is 0.118826 seconds. Elapsed time is 0.119118 seconds. Elapsed time is 0.116356 seconds. Elapsed time is 0.120454 seconds. Elapsed time is 0.115588 seconds. Elapsed time is 0.137131 seconds. Elapsed time is 0.116801 seconds. Elapsed time is 0.118221 seconds. Elapsed time is 0.196411 seconds. Elapsed time is 0.124739 seconds. Elapsed time is 0.117553 seconds. Elapsed time is 0.117905 seconds. Elapsed time is 0.120064 seconds. Elapsed time is 0.118175 seconds. Elapsed time is 0.119582 seconds. Elapsed time is 0.118594 seconds. Elapsed time is 0.117717 seconds. Elapsed time is 0.118136 seconds. Elapsed time is 0.120108 seconds. Elapsed time is 0.120507 seconds. Elapsed time is 0.116581 seconds. Elapsed time is 0.117901 seconds. Elapsed time is 0.116905 seconds. Elapsed time is 0.128492 seconds. Elapsed time is 0.116886 seconds. Elapsed time is 0.120516 seconds. Elapsed time is 0.120510 seconds. Elapsed time is 0.124785 seconds. Elapsed time is 0.124105 seconds. Elapsed time is 0.156394 seconds. Elapsed time is 0.123625 seconds. Elapsed time is 0.119958 seconds. Elapsed time is 0.120315 seconds. Elapsed time is 0.118118 seconds. Elapsed time is 0.121774 seconds. Elapsed time is 0.118541 seconds. Elapsed time is 0.123675 seconds. Elapsed time is 0.118472 seconds. Elapsed time is 0.119349 seconds. Elapsed time is 0.117373 seconds. Elapsed time is 0.120386 seconds. Elapsed time is 0.126365 seconds. Elapsed time is 0.119422 seconds. Elapsed time is 0.117676 seconds. Elapsed time is 0.118487 seconds. Elapsed time is 0.119343 seconds. Elapsed time is 0.119641 seconds. Elapsed time is 0.117806 seconds. Elapsed time is 0.127159 seconds. Elapsed time is 0.136230 seconds. Elapsed time is 0.193245 seconds. Elapsed time is 0.129831 seconds. Elapsed time is 0.118571 seconds. Elapsed time is 0.117501 seconds. Elapsed time is 0.126550 seconds. Elapsed time is 0.119849 seconds. Elapsed time is 0.117533 seconds. Elapsed time is 0.118374 seconds. Elapsed time is 0.119381 seconds. Elapsed time is 0.119987 seconds. Elapsed time is 0.118357 seconds. Elapsed time is 0.118834 seconds. Elapsed time is 0.117864 seconds. Elapsed time is 0.123820 seconds. Elapsed time is 0.118941 seconds. Elapsed time is 0.117832 seconds. Elapsed time is 0.116590 seconds. Elapsed time is 0.117626 seconds. Elapsed time is 0.126993 seconds. Elapsed time is 0.119129 seconds. Elapsed time is 0.118313 seconds. Elapsed time is 0.119329 seconds. Elapsed time is 0.119414 seconds. Elapsed time is 0.120006 seconds. Elapsed time is 0.116728 seconds. Elapsed time is 0.120030 seconds. Elapsed time is 0.116132 seconds. Elapsed time is 0.122892 seconds. Elapsed time is 0.119231 seconds. Elapsed time is 0.120180 seconds. Elapsed time is 0.118690 seconds. Elapsed time is 0.118847 seconds. Elapsed time is 0.125363 seconds. Elapsed time is 0.138503 seconds.
% ------ 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

6 Kommentare

Please format your code properly.
Sam Chak
Sam Chak am 7 Okt. 2023
@Vaishnavi P, I tried helping, but your original code is copied/pasted as a one-line code in the forum. Due to the source comes from you, we need your intervention to help us move forward.
Please do check now
Sam Chak
Sam Chak am 7 Okt. 2023
@Vaishnavi P, The propagation time is reduced from 100 s to 20 s because your original code took longer than 55 seconds to run.
@Sam Chak Yes My doubt is surface z is defined in function ocean wave right, I am not getting on what basis it's written
@Sam Chak Hey,I just wanted to understand the physics behind surfaceZ = (height/2)*cos(dirX*gridX +dirY*gridY - wavePhase);
Thank You

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Sam Chak
Sam Chak am 9 Okt. 2023
I regret to inform you that I am not an oceanographer. From the context, it appears that you are most likely a student specializing in the study of the physical properties and processes of the ocean, encompassing aspects such as ocean currents, tides, waves, and the circulation of water masses.
However, in your provided MATLAB code, the positions of water particles in the Z-direction of the Cartesian coordinate system are defined as follows:
surfaceZ = (height/2)*cos(dirX*gridX + dirY*gridY - wavePhase);
This expression can be mathematically interpreted as follows:
, where the phase is ,
with surfaceZ (z) represents the vertical position of water particles, height (h) is a parameter that influences the wave's amplitude, dirX () and dirY () are directional coefficients affecting the wave's orientation, gridX (x) and gridY (y) denote spatial coordinates, and lastly, wavePhase () is a phase term contributing to wave oscillation.
It is evident that the function defines a sinusoidal plane wave. While sine and cosine functions are commonly employed to describe the shape of an "idealized" ocean wave, it is important to note that the actual motion can be more precisely characterized as a "trochoid." Nevertheless, the MATLAB-generated topographical representation of the ocean wave appears to be quite impressive.
For a more comprehensive understanding of the physics underlying ocean waves, I recommend exploring the following resources:
  1. Mathematics of Ocean Waves and Surfing
  2. HyperPhysics - Water Waves
  3. Lumen Learning - Mathematics of Waves
  4. Celer Network - Mathematics of Ocean Waves
Additionally, you may find valuable insights in the book titled "Introduction to Ocean Waves" authored by Prof. Emeritus Rick Salmon. You can access this book at the following link: http://www-pord.ucsd.edu/rsalmon/111.textbook.pdf.
I hope these resources provide you with the necessary information to delve deeper into the field of oceanography and the mathematics behind ocean waves.

3 Kommentare

@Sam Chak Thank You for the valuable info. Also one more question regarding the same
dirX = cos(deg2rad(direction + 270));
dirY = sin(deg2rad(direction + 270));
What these equations are based on?
Sam Chak
Sam Chak am 9 Okt. 2023
You're welcome, @Vaishnavi P. If you find the resourses helpful, please consider clicking 'Accept' ✔ on the answer and voting 👍 for it. Thanks a bunch!
Returning to the directional equations, I speculate that they are related to the direction of wave propagation resulting from wind interacting with the surface water. When I conducted a keyword search on Google, it led me to the Wikipedia page on Wind Waves:
Scrolling down to the section on Spectral Models, you will notice that the function describing wave elevation, denoted by the symbol zeta (ζ), exhibits similarities to your surfaceZ function. In science & engineering, the term "elevation" is frequently employed to denote the height or vertical position of a physical object or an molecular particle concerning a reference point or surface.
@Sam Chak Thanks you for the help

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Oceanography and Hydrology finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by