how can i fix it please

Respuestas (2)

Image Analyst
Image Analyst el 9 de Abr. de 2022
Editada: Image Analyst el 9 de Abr. de 2022

1 voto

Replace "and" by a double (not single) ampersand : &&
Get rid of lines 4 and 6. MATLAB does not have a "do" and does not start and end blocks with braces.
Your while loop doesn't seem to have any way to exit if it goes into it. How could soc and Ppv change if they're variables? If they're functions, then perhaps they could come back with different values each time, but they're not. They're either scalars or arrays (which is it?).
But there's also a problem in that your while has no failsafe - no limit on the number of iterations so you could get into an infinite loop situation. More robust code would be:
maxIterations = 100000; % Way more than you think you'd ever need.
loopCounter = 0; % The failsafe
while (soc < 80) && (Ppv > 4000) && loopcounter < maxIterations
u = 0;
% Then maybe some other code that changes soc or Ppv.
% Finally increment the loop counter.
loopCounter = loopCounter + 1;
end
if loopCounter >= maxIterations
warningMessage = sprintf('Loop exited early after %d iterations', loopCounter)
uiwait(warndlg(warningMessage));
end

6 comentarios

khalil chnini
khalil chnini el 9 de Abr. de 2022
Editada: khalil chnini el 9 de Abr. de 2022
thnk you for answering
the output u is an order to run or break a genrator...
and soc is batterie's🔋 charge degree (it ll be charged by the generator)...
and Ppv is the power of photovoltaïque
so first u = 0 next change u = 1 when soc< 20% and Ppv < 4000 ....and it has to be fixed in 1 untel the batterie🔋charged (sos >80) or Ppv >4000
Image Analyst
Image Analyst el 9 de Abr. de 2022
How does soc or Ppv ever get changed inside the loop?
khalil chnini
khalil chnini el 9 de Abr. de 2022
w enter them as an input throu (go to ) block
Image Analyst
Image Analyst el 9 de Abr. de 2022
OK, then you should break out of the loop if/when the criteria gets met.
khalil chnini
khalil chnini el 12 de Abr. de 2022
Editada: khalil chnini el 12 de Abr. de 2022
how i do this
Image Analyst
Image Analyst el 12 de Abr. de 2022
Somehow you are changing either soc or Ppv inside the loop. When the condition below
(soc < 80) && (Ppv > 4000) && loopcounter < maxIterations
is false, the loop will automatically exit. You said that you were getting those values inside the loop via asking the user with an input() statement, which is how I interpreted this: "w enter them as an input throu (go to ) block"

Iniciar sesión para comentar.

Karuppasamy Kalimuthu
Karuppasamy Kalimuthu el 9 de Abr. de 2022

0 votos

and to be replaced by &

3 comentarios

John D'Errico
John D'Errico el 9 de Abr. de 2022
Actually, no. You want to use && in an if statement. There is a difference.
Karuppasamy Kalimuthu
Karuppasamy Kalimuthu el 11 de Abr. de 2022
Yes. && is correct.
Walter Roberson
Walter Roberson el 11 de Abr. de 2022
Either & or && can be used when both sides are scalars, but && would be more efficient in such a case.
If the left side is non-scalar, or the left side is scalar non-zero and the right side is non-scalar, then & must be used instead of && .

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 9 de Abr. de 2022

Comentada:

el 12 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by