eq1 have at each nf the values 0.186170065500000 0.336133515500000 0.486096965500000 0.636060415500000 0.786023865500000 0.935987315500000 1.08595076550000
i want to plot out1 when eq1 >1so, out1 should be calculated at 1.08595076550000
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi);
out1=(bwf*nf)./wi;
plot (eq1);
hold on
if all(eq1 >=1)
out1=(bwf*nf)./wi;
plot (out1);
end

1 comentario

Adam
Adam el 22 de Nov. de 2018
To answer the question in the title, read the logic of your if statement.
if all( eq1 >= 1 )
i.e. it will only evaluate to true if all the values in eq1 are greater than or equal to 1. The result is a scalar logical, as needed generally for an if statement, but it isn't capturing what you want.

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 22 de Nov. de 2018
Editada: madhan ravi el 22 de Nov. de 2018

0 votos

8 comentarios

maryhan mohamed
maryhan mohamed el 22 de Nov. de 2018
it works now
but,
nf 1:7
so eq1 will be >1 at nf=7 only
when out1 is calculated by the above code it calculate all nf 1:7 not nf=7
madhan ravi
madhan ravi el 22 de Nov. de 2018
Did you read the first link?
maryhan mohamed
maryhan mohamed el 22 de Nov. de 2018
Editada: madhan ravi el 22 de Nov. de 2018
yes, i got my floating point problem
but
nf = 1, 2,3,4,5,6,7
and
eq1 will be >1 at nf=7
please, i want to calculate the out1 when eq1 >1 at nf =7
out1=(bwf*nf)./wi;
my code give me result for out1 at each nf not at nf =7
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi);
out1=(bwf*nf)./wi;
plot (eq1);
hold on
if sum(eq1)>=1
out1=(bwf*nf)/wi;
plot (out1);
end
madhan ravi
madhan ravi el 22 de Nov. de 2018
if abs(eq1-1)>1e-4 %set tolerance by the way you didn't read the links I suggested
from the shown code,
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi);
out1=(bwf*nf)./wi;
plot (eq1);
hold on
if abs(eq1-1)>1e-4
out1=(bwf*nf)/wi;
plot (out1);
end
the output will be
eq1=0.186170065500000 0.336133515500000 0.486096965500000 0.636060415500000 0.786023865500000 0.935987315500000 1.08595076550000
out1=0.149963450000000 0.299926900000000 0.449890350000000 0.599853800000000 0.749817250000000 0.899780700000000 1.04974415000000
please,, what i want is to make out1 result = 1.04974415000000 only % nf=7 at eq1=1.08595076550000 which is greater than 1.
thanks for ur helping,
madhan ravi
madhan ravi el 22 de Nov. de 2018
bwf , wi?
wi=10000000;
nf=1:7;
nm1=1;
bwf=1499634.50;
bwm=362066.155;
madhan ravi
madhan ravi el 22 de Nov. de 2018
Editada: madhan ravi el 22 de Nov. de 2018
wi=10000000;
nf=1:7;
nm1=1;
bwf=1499634.50;
bwm=362066.155;
eq1=[0.186170065500000 0.336133515500000 0.486096965500000 0.636060415500000 0.786023865500000 0.935987315500000 1.08595076550000];
idx=abs(eq1-0)>1
result=(bwf*nf)/wi;
result=result(idx)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 22 de Nov. de 2018

3 votos

The link about comparing equality of floating point numbers is good information, though it doesn't apply here since none of your eq1 numbers are expected to be exactly 1 - they're all clearly either more than or less than 1.
Your original code
wi=10000000;
nf=1:7;
nm1=1;
bwf=1499634.50;
bwm=362066.155;
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi);
out1=(bwf*nf)./wi;
plot (eq1);
hold on
if all(eq1 >=1)
out1=(bwf*nf)./wi;
plot (out1);
end
did plot the first plot. It did not plot the second plot since not ALL elements of eq1 were more than 1. I believe you want masking, where you plot only those elements where eq1 > 1 and no others ("i want to plot out1 when eq1 >1"). To do that, see this corrected code:
wi=10000000;
nf=1:7;
nm1=1;
bwf=1499634.50;
bwm=362066.155;
eq1=((bwf*nf)./wi)+((bwm*nm1)./wi)
% Plot eq1.
plot (eq1, 'bo-');
xlabel('index', 'FontSize', 15);
ylabel('eq1', 'FontSize', 15);
grid on;
% Compute out1.
out1 = (bwf * nf) ./ wi % Note: same as
% Create a mask where only those where eq1 > 1 are true.
mask = eq1 > 1
% Plot only those where eq1 > 1 (where the mask is true).
hold on;
x = 1 : length(out1)
plot (x(mask), out1(mask), 'r*', 'MarkerSize',13, 'LineWidth', 2);
legend('eq1', 'out1', 'Location', 'north');
0000 Screenshot.png
Your original eq1 (outside the if block) is plotted in blue while the points of out1, only where eq1 is greater than 1, are plotted in red.

Preguntada:

el 22 de Nov. de 2018

Comentada:

el 22 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by