Using a loop and counter for parametric equations

5 visualizaciones (últimos 30 días)
Landon Sugar
Landon Sugar el 18 de Oct. de 2019
Comentada: darova el 21 de Oct. de 2019
I'm very new to MATLAB so I am aware this code is probably atrocious. Please speak clearly!
I have an equation for calculating wave power density (E) in an ocean wave. It has three variables (H, T, and h) with different numerical ranges. (i.e. H ranges from 1 to 3, T ranges from 5 to 22, and h ranges from 5 to 6.) I want to create a FOR loop where MATLAB will plug in different numbers, with increments of 1, essentially being a parametric study. I can't find any clear videos or links on nested for loops with counters.
Ex:
Screen Shot 2019-10-18 at 9.42.42 AM.png

Respuesta aceptada

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 18 de Oct. de 2019
Hi
Here is the solution of your task:
function E = realED(H, T, h)
%
%
%
rho=1030;
g=9.81;
%
%
L = zeros(numel(H), numel(T), numel(h));
k=L;
Cg=L;
E=L;
for ii=1:numel(H)
for m=1:numel(T)
for jj = 1:numel(h)
L(ii, m, jj) = (g*T(m)^2)/(2*pi*tan(h(jj)))*((4*pi)^2)*h(jj)/(g*T(m)^2)^3;
k(ii, m, jj)=2*pi/L(ii, m, jj);
Cg(ii, m, jj) = .5*((1+((2*k(ii, m, jj)*h(jj))/sin(h(jj))))*2*k(ii,m,jj)*h(jj))*L(ii, m, jj)/T(m);
E(ii, m, jj) = ((rho*g)/16)*(H(ii)^2)*Cg(ii, m, jj);
end
end
end
Good luck
  2 comentarios
Sulaymon Eshkabilov
Sulaymon Eshkabilov el 21 de Oct. de 2019
Memory allocation is already done for : L, k, Cg and E.
darova
darova el 21 de Oct. de 2019
Yes, Sir!

Iniciar sesión para comentar.

Más respuestas (1)

darova
darova el 18 de Oct. de 2019
Preallocate array before loop
E = zeros(3-1+1,22-5+1,6-5+1);
for
%% E(H,T,h) = ...
end
  2 comentarios
Landon Sugar
Landon Sugar el 18 de Oct. de 2019
Editada: Landon Sugar el 18 de Oct. de 2019
Thank you for your response!
what does the "3-1+1, 22-5+1, 6-5+1" part mean?
darova
darova el 18 de Oct. de 2019
Those values are dimensions of 3D matrix. You will have 3 x 18 x 2 matrix. You want to visualize it?

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by