# How to start loop when it finds the first maximum

2 visualizaciones (últimos 30 días)
JORGE REVUELTA LOSADA el 1 de Feb. de 2023
Hello,
In my application, a sinusoidal excitation field is being applied. The value of the maximum excitation field is known. What I am trying to do is to start a loop when the first maximum is reached. I know it seems quite simple, but I can't really find a statement that works. In advantage, thank you so much for your response.
%This code doesn't work
max_value = 104;
%Line to obtain the actual value of the sinusoid (actual_value)
if actual_value == max_value %Start loop when it finds the FIRST maximum of the sinusoid
for i=1:10
%do something
end
end
##### 2 comentariosMostrar NingunoOcultar Ninguno
Dyuman Joshi el 1 de Feb. de 2023
What is actual_value supposed to be? Is it the input data? If so, how does it vary?
JORGE REVUELTA LOSADA el 3 de Feb. de 2023
Thank you for your answer. It is the actual value of a sinusoidal excitation field.

Iniciar sesión para comentar.

Mathieu NOE el 1 de Feb. de 2023
hello
this code does work
maybe simply you have to pay attention that such condition statement may not work well simply because of rounding errors or lack of precision in your data between actual_value and max_value
if actual_value == max_value
it's more robust to make this way whith a given tolerance level
if abs(actual_value - max_value) < tol
fullcode :
%This code does work
max_value = 104;
tol = 1e-1; % tolerance for max value finding (can be reduced if you use a higher sampling rate)
%% dummy data
% one sine period simulation
samples = 53; % samples per period (choose some dummy value to not have "perfect" data)
x = 1:samples-1;
y = max_value*sin(2*pi*x/samples);
plot(y,'-*')
%% main loop
for k = 1:numel(y) % this for loop simulates data acquisition (sample by sample)
actual_value = y(k); %Line to obtain the actual value of the sinusoid (actual_value)
if abs(actual_value - max_value) < tol %Start loop when it finds the FIRST maximum of the sinusoid
disp(['max value (' num2str(actual_value) ') reached ! at sample = ' num2str(k)]);
for i=1:10
%do something
end
end
end
##### 2 comentariosMostrar NingunoOcultar Ninguno
JORGE REVUELTA LOSADA el 3 de Feb. de 2023
Thank you for your answer. The tolerance was necessary in a first step and as I also had a noisy signal I had to make a fitting. Also I had to change the loop because it was only evaluating for once the actual value of the sinusoidal excitation field. Thank you once more.
Mathieu NOE el 3 de Feb. de 2023
My pleasure !

Iniciar sesión para comentar.

### Más respuestas (1)

Tushar Behera el 1 de Feb. de 2023
Editada: Tushar Behera el 1 de Feb. de 2023
Hi Jorge,
It is difficult to give a proper solution with the information you have provided. However can you verify what are the values the variable "actual_value" takes during the codes run time. You can do this by debugging your code in debug mode.
I can only assume that probably "actual_value" never attains a value truly equal to "max_value". For example take a look at the below code:
a=2;
b=2.000000000000095
if a==b
z=3% this statement will never get executed
end
However, for the code
a=2;
b=2.00000000000000000095
if a==b
z=3%this will get executed
end
z will be assigned the value 3. Try to debug your code and take necessary steps so as "actual_value" will be numerically equal to "max_value". This is happenig because the error tolerance between "actual_value" and "maximu_value" might be too low.Some of the ways you can try is by using "floor" function in your code like below example:
a=2;
b=2.000000000000095
b=floor(b)
if a==b
z=3% this statement will get executed
end
Also you can try out the suggestion given by Mathieu in order set a limit for tolerance band.
Hope this helps you.
Regards,
tushar
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
JORGE REVUELTA LOSADA el 3 de Feb. de 2023
Thank you for your answer, I will take it into account in future developments.

Iniciar sesión para comentar.

### Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by