How do I perform several iterations in MATLAB?
1 view (last 30 days)
Show older comments
I am trying to find the corresponding wave length from about 8,000 wave period data points. I have the matlab code to import the data and turn it into a table. I need to use each individual wave period and run iterations to find wave length using this equation: L = 1.56(T^2)tanh(61.58/L). I am not sure where to start, this is all I have so far.
%% Create Table From Excel File
% Identify excel file
filename = '2022WaveData.xlsx'; % Excel File Name
sheet = 1; % Sheet number
% Import data from the Excel file
wavedata = readtable(filename);
% Select every 2nd cell from the data - create array of hourly data
hourlydata = wavedata(1:2:end, :);
% Use only two columns from the selected data
hourlydata = hourlydata(:, 9:10); % Isolate wave height and dominant wave period
% Display the selected data
disp('Hourly Wave Data 2022:');
disp(hourlydata);
%% Perform an iterative calculation to find wavelength
% L = 1.56*(y^2)*tanh(61.58/L)
% Initial guess for L
L = 60;
% Perform the iterative calculation
y = hourlydata(:, 2);
for K = 1 : 100
L = 1.56*(y^2)*tanh(61.58/L);
end
% Display the final value of L
disp(['The wavelength [m] is approximately: ', num2str(L)]);
Any help would be greatly appreciated!
0 Comments
Accepted Answer
Star Strider
on 9 Nov 2023
Since ‘y’ appears to be a column vector, the loop is not necessary. Just use:
L = 1.56*(y.^2)*tanh(61.58/L);
If you are looping through various columns of ‘hourlydata’ the loop still is not necessary —
L = 60;
y = randn(10,5)
L = 1.56*(y.^2)*tanh(61.58/L)
If there is something in your question that I am missing, please clarify.
.
2 Comments
More Answers (1)
Walter Roberson
on 9 Nov 2023
Change
for K = 1 : 100
L = 1.56*(y^2)*tanh(61.58/L);
end
to
LGuess = 60;
for K = 1 : 100
L(K) = fzero(@(L) 1.56*(y(K)^2)*tanh(61.58/L) - L, LGuess);
end
This presumes that L = 1.56*(y^2)*tanh(61.58/L) is an equation to be solved for L given a particular y value.
0 Comments
See Also
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!