Find two x values for one y value

1 visualizzazione (ultimi 30 giorni)
Bartosz Pasek
Bartosz Pasek il 28 Mag 2021
Risposto: Jan il 28 Mag 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)

Risposta accettata

Star Strider
Star Strider il 28 Mag 2021
C=1*10^(-4);
L=3*10^(-1);
R=100
R = 100
N2=100;
frez=1/(2*pi*sqrt(L*C)) %f value for Imax(theory of RLC circles)
frez = 29.0576
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
WImax = 0.0100
idx = 30
idxv = {1:idx; idx:numel(f)} % Create Index Vectors
idxv = 2×1 cell array
{[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30]} {[30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101]}
for k = 1:2
xval(k) = interp1(WI(idxv{k}),f(idxv{k}), WImax/sqrt(2)) % Interpolate To Fine X-Values
end
xval = 12.8235
xval = 1×2
12.8235 65.8711
plot(f,WI)
hold on
plot(xval, [1 1]*WImax/sqrt(2), 'rs') % Plot Desired Points
hold off
Imax=max(WI)
Imax = 0.0100
Igr=0.707*max(WI) %value that I searched for
Igr = 0.0071
[~, 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)
fgr = 66
.

Più risposte (1)

Jan
Jan il 28 Mag 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.

Prodotti


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by