How to start loop when it finds the first maximum
显示 更早的评论
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 个评论
Dyuman Joshi
2023-2-1
What is actual_value supposed to be? Is it the input data? If so, how does it vary?
JORGE REVUELTA LOSADA
2023-2-3
编辑:JORGE REVUELTA LOSADA
2023-2-3
采纳的回答
更多回答(1 个)
Tushar Behera
2023-2-1
编辑:Tushar Behera
2023-2-1
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
类别
在 帮助中心 和 File Exchange 中查找有关 Code Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!