How to start loop when it finds the first maximum

1 view (last 30 days)
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 Comments
Dyuman Joshi
Dyuman Joshi on 1 Feb 2023
What is actual_value supposed to be? Is it the input data? If so, how does it vary?
JORGE REVUELTA LOSADA
JORGE REVUELTA LOSADA on 3 Feb 2023
Edited: JORGE REVUELTA LOSADA on 3 Feb 2023
Thank you for your answer. It is the actual value of a sinusoidal excitation field.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 1 Feb 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 Comments
JORGE REVUELTA LOSADA
JORGE REVUELTA LOSADA on 3 Feb 2023
Edited: JORGE REVUELTA LOSADA on 3 Feb 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.

Sign in to comment.

More Answers (1)

Tushar Behera
Tushar Behera on 1 Feb 2023
Edited: Tushar Behera on 1 Feb 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 Comment
JORGE REVUELTA LOSADA
JORGE REVUELTA LOSADA on 3 Feb 2023
Thank you for your answer, I will take it into account in future developments.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!