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 で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!