How to start loop when it finds the first maximum
Ältere Kommentare anzeigen
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 Kommentare
Dyuman Joshi
am 1 Feb. 2023
What is actual_value supposed to be? Is it the input data? If so, how does it vary?
JORGE REVUELTA LOSADA
am 3 Feb. 2023
Bearbeitet: JORGE REVUELTA LOSADA
am 3 Feb. 2023
Akzeptierte Antwort
Weitere Antworten (1)
Tushar Behera
am 1 Feb. 2023
Bearbeitet: Tushar Behera
am 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
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!