Effacer les filtres
Effacer les filtres

How do I perform several iterations in MATLAB?

1 vue (au cours des 30 derniers jours)
Alexis
Alexis le 9 Nov 2023
Commenté : Star Strider le 10 Nov 2023
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!

Réponse acceptée

Star Strider
Star Strider le 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)
y = 10×5
0.7598 -0.4159 0.9938 0.3012 1.5481 0.1682 -0.9686 0.2074 -1.5123 1.4598 1.5901 0.5688 -0.9679 0.6580 -0.7723 1.0208 -0.4262 -0.2904 2.6653 0.8932 -1.3013 -0.1593 0.8142 -2.4869 1.3280 0.0064 -1.0026 1.1594 0.8647 1.2845 -1.5432 0.4921 0.3040 -0.8782 -1.4256 -1.0761 -0.2994 0.7769 1.8965 0.5642 1.6883 0.6276 -0.8931 0.4189 -1.5163 -0.3547 1.7922 0.4473 0.9087 0.5698
L = 1.56*(y.^2)*tanh(61.58/L)
L = 10×5
0.6957 0.2084 1.1902 0.1093 2.8878 0.0341 1.1305 0.0518 2.7558 2.5680 3.0467 0.3899 1.1288 0.5217 0.7187 1.2556 0.2189 0.1016 8.5603 0.9613 2.0405 0.0306 0.7988 7.4523 2.1250 0.0000 1.2112 1.6197 0.9010 1.9883 2.8696 0.2918 0.1114 0.9294 2.4491 1.3954 0.1080 0.7273 4.3341 0.3835 3.4347 0.4746 0.9611 0.2114 2.7706 0.1516 3.8705 0.2411 0.9949 0.3913
If there is something in your question that I am missing, please clarify.
.
  2 commentaires
Alexis
Alexis le 10 Nov 2023
This worked thank you! First I had to use curly brackets to pull the wave periods (y) from the table using y=hourlydata{:, 2}; then it ran the iterations and gave me all my values for L. Thanks again!
Star Strider
Star Strider le 10 Nov 2023
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 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.

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by