Find two x values for one y value
Show older comments
I have a function where I need to know values of f for 0.707*max(WI) value. I tried this code, but found only one value:
C=1*10^(-4);
L=3*10^(-1);
R=100
N2=100;
frez=1/(2*pi*sqrt(L*C)) %f value for Imax(theory of RLC circles)
WI=zeros(N2+1,1);
f=zeros(N2+1,1);
for n=0:N2
omega=2*pi*n;
I=1/(sqrt(R^2+(omega*L-(1/(omega*C)))^2));
WI(n+1)=I;
fcz=n;
f(n+1)=fcz;
end
plot(f,WI)
Imax=max(WI)
Igr=0.707*max(WI) %value that I searched for
[~, index] = min(abs(WI - 0.707*max(WI))); %trying to find f values for Igr but only one show up(should be two)
fgr=f(index)
Accepted Answer
More Answers (1)
Jan
on 28 May 2021
tmp = abs(WI - 0.707*max(WI));
minValue = min(tmp);
index = (tmp == minValue);
fgr = f(index)
For numerical values it is unlikely that there are no rounding artifacts. Searching for tmp==minValue is critical. Prefer to use a tolerance of a matching size instead.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!