How to start loop when it finds the first maximum
1 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
Mathieu NOE
2023-2-1
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 个评论
更多回答(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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!