Function keeps getting errors, how to fix?
1 次查看(过去 30 天)
显示 更早的评论
I have to make a function that takes 2 inputs to find relative humidity.
This is my attempt:
function RH = RelHum(Tdb, Twb)
%finds relative humidityIf the
ftoc = @(n) ((n-32)*(5/9));
%finding pressures
svp = exp(1)^((16.78*ftoc(Tdb) - 116.9)/(ftoc(Tdb) + 237.3));
vp = exp(1)^((16.78*ftoc(Twb) - 116.0)/(ftoc(Twb) + 273.3)) - 0.066858 *(1+ 0.00115*ftoc(Twb))*(ftoc(Tdb)-ftoc(Twb));
RH = vp/svp * 100;
end
The error just indicates a problem with the 'vp' line and highlights it in red.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/423958/image.png)
This is the function I'm supposed to be using for vp ^^^^^^^^
I appricate any help you can give me, right now I'm pretty lost.
0 个评论
采纳的回答
VBBV
2020-11-23
function RH = RelHum(Tdb, Twb)
syms n % define the symbolic n
ftoc = @(n) ((n-32)*(5/9));
svp = exp(1)^((16.78*ftoc(Tdb) - 116.9)/(ftoc(Tdb) + 237.3));
vp = exp(1)^((16.78*ftoc(Twb) - 116.0)/(ftoc(Twb) + 273.3)) - 0.066858 *(1+ 0.00115*ftoc(Twb))*(ftoc(Tdb)-ftoc(Twb));
RH = vp/svp * 100;
end
Define the symbolic n inside the function before using ftoc
更多回答(1 个)
Steven Lord
2020-11-23
编辑:Steven Lord
2020-11-23
What is the full and exact text (everything displayed in red and/or orange in the Command Window) of the error and/or warning messages you receive when you run your code? The exact text of the messages may include information that will help us determine the problem or at least the next step to take in investigating the problem.
Looking at your code:
% svp = exp(1)^((16.78*ftoc(Tdb) - 116.9)/(ftoc(Tdb) + 237.3));
Rather than raising the value returned by exp(1) to a power, just put the power inside the exp call.
esquared1 = exp(1)^2
esquared2 = exp(2)
% vp = exp(1)^((16.78*ftoc(Twb) - 116.0)/(ftoc(Twb) + 273.3)) - 0.066858 *(1+ 0.00115*ftoc(Twb))*(ftoc(Tdb)-ftoc(Twb));
Is the part of vp before the minus sign (just prior to the constant 0.066858) suppose to be the same as svp? If spo just reuse svp to avoid problems like the typo I think you made in vp (you subtract 116.0 instead of 116.9.) Or if they are supposed to be the same except for using a different variable (svp uses Tdb, vp uses Twb) maybe turn that into a function handle like you did ftoc.
As a very minor efficiency suggestion, you're using the quantities ftoc(Twb) and ftoc(Tdb) repeatedly in your code, recomputing them each time. Perhaps compute each of those quantities once, giving them good names, prior to running the code that uses them to avoid the repeated conversions.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!