Defining the constant inside else if statement.

I am trying to create the function that includes 'if else if statement', and inside of that function I want to create the constant value, but I do not want to declare it as the variable. Here is the code that I wrote but keeps giving me the bug.
----Code-----
function Psat = fcn(Tsat)
%Calculate the saturation pressure
%Input: Temperature [C]
%Output: Pressure[MPa]
if Tsat > 2
a=1;
elseif Tsat <= 2
a=2;
end
Psat = a*Tsat
end

4 个评论

What do you consider to be a bug in this code (other than the fact that if Tsat is neither greater than 2 nor less than or equal to 2 the variable a will be undefined, which could happen if Tsat is nonscalar or if it is NaN?)
I do not understand your question. Your code executes. What are your trying to do?
function Psat = fcn(Tsat)
if Tsat > 2
a=1;
else
a=2;
end
Psat = a*Tsat;
Do you mean you have a bug, or an error? What are you passing in for Tsat that give the error or demonstrates the bug? You aren't just clicking the green "Run" triangle to execute the code are you? Because doing that will not "invent" some value to pass into fcn(). You need to tell it what to pass in in your script, like this:
Psat = fcn(5);

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2019b

提问:

2020-4-11

评论:

2020-4-11

Community Treasure Hunt

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

Start Hunting!

Translated by