Find two x values for one y value
2 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
采纳的回答
Star Strider
2021-5-28
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
[WImax,idx] = max(WI) % Find Maximum & Index
idxv = {1:idx; idx:numel(f)} % Create Index Vectors
for k = 1:2
xval(k) = interp1(WI(idxv{k}),f(idxv{k}), WImax/sqrt(2)) % Interpolate To Fine X-Values
end
plot(f,WI)
hold on
plot(xval, [1 1]*WImax/sqrt(2), 'rs') % Plot Desired Points
hold off
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)
.
更多回答(1 个)
Jan
2021-5-28
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
