Find two x values for one y value
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Bartosz Pasek
am 28 Mai 2021
Beantwortet: Jan
am 28 Mai 2021
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 Kommentare
Akzeptierte Antwort
Star Strider
am 28 Mai 2021
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)
.
2 Kommentare
Weitere Antworten (1)
Jan
am 28 Mai 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!