I would like to change the massflowrate when the statement is correct, could you help?

For some reason the massflowrate doesn't change itself whenever I enter the following, no errors or anything appear. Just got no clue how to fix this.
electricalsignal=5;
massflowrate=3;
if electricalsignal==0
disp("error")
else
temperature = 200*electricalsignal-400;
end
if temperature<74;
disp("opening valves")&& massflowrate == massflowrate-50;
elseif (74<=temperature) && (temperature>=78)
disp("closing valves")
elseif temperature>78
disp("closing valves") && massflowrate == massflowrate+50;
endif

댓글 수: 1

I would strongly recommend that you go through the free matlab onramp to learn the basics of matlab.
I'm not entirely sure what you're trying to achieve with these lines:
disp("opening valves")&& massflowrate == massflowrate-50;
it's a completely made up syntax.

댓글을 달려면 로그인하십시오.

 채택된 답변

Subhamoy Saha
Subhamoy Saha 2020년 3월 14일
편집: Subhamoy Saha 2020년 3월 14일
The problem is with the line you are changing values of massflowrate. You are making it logical condition rather assigning it a new value. Please try the following
electricalsignal=5;
massflowrate=3;
if electricalsignal==0
disp("error")
else
temperature = 200*electricalsignal-400;
end
if temperature<74;
disp("opening valves"), massflowrate = massflowrate-50; % corrected
elseif (74<=temperature) && (temperature>=78)
disp("closing valves")
elseif temperature>78
disp("closing valves"), massflowrate = massflowrate+50; % corrected
end

댓글 수: 5

whenever I enter the code, the massflowrate doesn't change as you might see in the image
I basically want it to show massflowrate=53 in stead of massflowrate = 3
If that was the intent of the made up syntax I would strongly recommend splitting the statements onto several lines rather than using a ,.
if temperature<74;
disp("opening valves");
massflowrate = massflowrate-50; % corrected
elseif 74<=temperature && temperature>=78 %!!!!!!!!
disp("closing valves")
else
disp("closing valves")
massflowrate = massflowrate+50;
end
Also don't end on an elseif. If the 2nd elseif had been written correctly, then you would be guaranteed that the 3rd condition would be true if the other two were false, hence there's no point testing for it.
With regards to that second elseif, note that it tests that the temperature is greater than both 74 and 78. The second guarantees the first. Clearly that's not what was intended.
thanks for that, but the massflowrate doesn't change yet...
Dear Adam, the massflowrate is not changing beacause for electricalsignal=5 temperature=600 which satisfies your second elseif condition and doesnot change massflowrate (as there is not conditions in your code). Actually there is a problem in your second elseif condition. I think you want to imply
elseif (74<=temperature) && (temperature<=78)
Please correct this condition as per your need.
However, the solution I provided is working fine. Just change the electricalsignal value to something else like 2.1 or so. For its value 3 it is going to your second elseif condition.
Anyway, here is the full corrected code
electricalsignal=5;
massflowrate=3;
if electricalsignal==0
disp("error")
else
temperature = 200*electricalsignal-400;
end
if temperature<74;
disp("opening valves"), massflowrate = massflowrate-50; % corrected
elseif (74<=temperature) && (temperature<=78) %% corrected
disp("closing valves")
elseif temperature>78
disp("closing valves"), massflowrate = massflowrate+50; % corrected
end % corrected
Subhamoy Saha,
Thank you very much!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2020년 3월 14일

댓글:

2020년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by