Problem with looping: the loop does not stop

Hello!
I have a problem that I have built a simple loop, which does not stop while running the code. Does anyone know what could be the issu? I have Matlab 2014.
Lw=106.5
hs=80
hr=1.5
a_air=0.0001
La=zeros(1,8000)
d=1:8000
for i=1:8000
if i<=1000
La(i)=Lw - 8 - 20*log(i) - 0.005*i
else
deltaLa=10*log(10^(Lw/10)) - 10*log(10^((Lw-sqrt(i)*a_air)/10))
La(i)=Lw - 8 - 20*log(i) - deltaLa + 10*log(i/1000)
end
end
Thank you!

1 comentario

Why are you using
d=1:8000
for loop check the if condition at every iteration, and will continue for 8000 in total.
So you can skip the for loop and just use 'd' array to compute your results
Lw=106.5;
hs=80;
hr=1.5;
a_air=0.0001;
La=zeros(1,8000);
d=1:8000;
if d<=1000
La(d)=Lw - 8 - 20*log(d) - 0.005*d
else
deltaLa=10*log(10.^(Lw/10)) - 10*log(10.^((Lw-sqrt(d)*a_air)/10));
La(d)=Lw - 8 - 20*log(d) - deltaLa + 10*log(d/1000)
end

Iniciar sesión para comentar.

 Respuesta aceptada

Jamal Nasir
Jamal Nasir el 25 de Abr. de 2020
Editada: Jamal Nasir el 25 de Abr. de 2020
%%% you can try this it work good %%%%
clc
clear
Lw=106.5
hs=80
hr=1.5
a_air=0.0001
d=1:8000
La=[];
for i=1:8000
if i<=1000
La=[La; Lw - 8 - 20*log(i) - 0.005*i];
else
deltaLa=10*log(10^(Lw/10)) - 10*log(10^((Lw-sqrt(i)*a_air)/10))
La=[La;Lw - 8 - 20*log(i) - deltaLa + 10*log(i/1000)];
end
end

4 comentarios

your logic contradicts as you first established
La=zeros(8000,1)
and then
La=[];
it means you assigned La zeros values and make it empty
Jamal Nasir
Jamal Nasir el 25 de Abr. de 2020
Editada: Jamal Nasir el 25 de Abr. de 2020
Delete first one
Muhammad Usman
Muhammad Usman el 25 de Abr. de 2020
What about the results produced by me in the comment section?
Anne
Anne el 25 de Abr. de 2020
Thanks! It works :)
Why my script did not work? I just found out that I had semicolons missing, do you think it was the reason why it kept running?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Abr. de 2020

Comentada:

el 25 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by