-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeTheory.m
131 lines (109 loc) · 4.6 KB
/
EdgeTheory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
%% POUYA ZARBIPOUR LAKPOSHTEH EMAIL: [email protected]
function EdgeTheory
% Initialize the GUI
fig = figure('Name', 'Wave Calculator', 'NumberTitle', 'off', ...
'Position', [100, 100, 800, 600], 'Resize', 'off', ...
'CloseRequestFcn', @closeApp);
% Input Panel UI
uicontrol(fig, 'Style', 'text', 'Position', [20, 550, 200, 20], ...
'String', 'Wave Period (sec)?');
TInput = uicontrol(fig, 'Style', 'edit', 'Position', [20, 525, 200, 25], ...
'String', '3.0');
uicontrol(fig, 'Style', 'text', 'Position', [20, 500, 200, 20], ...
'String', 'Mean Slope?');
SlopeInput = uicontrol(fig, 'Style', 'edit', 'Position', [20, 475, 200, 25], ...
'String', '0.1');
uicontrol(fig, 'Style', 'text', 'Position', [20, 450, 200, 20], ...
'String', 'Max Offshore Distance (m)?');
XmaxInput = uicontrol(fig, 'Style', 'edit', 'Position', [20, 425, 200, 25], ...
'String', '5.0');
% Buttons
uicontrol(fig, 'Style', 'pushbutton', 'Position', [20, 380, 100, 30], ...
'String', 'Calculate', 'Callback', @calculateCallback);
uicontrol(fig, 'Style', 'pushbutton', 'Position', [130, 380, 100, 30], ...
'String', 'Stop', 'Callback', @stopCallback);
% Axes for plotting
ax = axes(fig, 'Position', [0.35, 0.2, 0.6, 0.7]);
xlabel(ax, 'Offshore Distance (m)');
ylabel(ax, 'Wave Height');
title(ax, 'Wave Simulation');
hold(ax, 'on');
% Globals for wave animation
stopFlag = false; % Use a persistent variable to manage the stop condition
waveData = struct();
% Nested function: Calculate button callback
function calculateCallback(~, ~)
% Get inputs
T = str2double(get(TInput, 'String'));
slope = str2double(get(SlopeInput, 'String'));
xmax = str2double(get(XmaxInput, 'String'));
% Validate inputs
if isnan(T) || isnan(slope) || isnan(xmax)
errordlg('Please enter valid numbers.', 'Input Error');
return;
end
% Generate bathymetry profile
bath = generateBathymetry(slope, xmax, 200);
% Initialize wave data
waveData = initializeWave(T, slope, bath, xmax);
% Reset the stop flag and start animation
stopFlag = false;
animateWaves(ax, waveData, xmax);
end
% Nested function: Stop button callback
function stopCallback(~, ~)
% Set the stop flag to true
stopFlag = true;
end
% Nested function: Animate waves
function animateWaves(ax, waveData, xmax)
while ~stopFlag
% Update wave data
waveData.jstep = waveData.jstep + 1;
sigma = 2 * pi / waveData.T;
vect = cell(1, waveData.nModes);
for n = 1:waveData.nModes
vect{n} = waveData.ewave{n} .* cos(sigma * waveData.jstep * waveData.dt);
end
% Plot waves
cla(ax);
colors = ['r', 'b', 'g'];
for n = 1:waveData.nModes
plot(ax, linspace(0, xmax, waveData.bath.npts), vect{n}, ...
colors(n), 'LineWidth', 1.5);
end
% Add labels
legend(ax, arrayfun(@(x) sprintf('Mode %d', x - 1), 1:waveData.nModes, ...
'UniformOutput', false), 'Location', 'northwest');
% Pause for animation
pause(0.05);
end
end
% Nested function: Close the app
function closeApp(~, ~)
stopFlag = true; % Stop animation when the app is closed
delete(fig);
end
end
function bath = generateBathymetry(slope, xmax, npts)
dx = xmax / (npts - 1);
bath.h = slope * (0:dx:xmax);
bath.npts = npts;
end
function waveData = initializeWave(T, slope, bath, xmax)
g = 9.81; % Gravity
nModes = 3; % Number of wave modes
waveData.T = T;
waveData.dt = T / 300;
waveData.jstep = 0;
waveData.lambda = zeros(1, nModes);
waveData.ewave = cell(1, nModes);
% Calculate decay factor and wave amplitudes
for n = 0:nModes - 1
waveData.lambda(n + 1) = (2 * pi / T)^2 / (g * (2 * n + 1) * slope);
waveData.ewave{n + 1} = -exp(-waveData.lambda(n + 1) * bath.h);
end
waveData.bath = bath;
waveData.nModes = nModes;
waveData.xmax = xmax;
end