How do I perform several iterations in MATLAB?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2023-11-9
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.
.
更多回答(1 个)
Walter Roberson
2023-11-9
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!