Function keeps getting errors, how to fix?

2 views (last 30 days)
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.
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.

Accepted Answer

VBBV
VBBV on 23 Nov 2020
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
  2 Comments
VBBV
VBBV on 23 Nov 2020
Edited: VBBV on 23 Nov 2020
What are Tdb and Twb ? Are they vectors or scalars ? if you are passing vectors as inputs, the use
.^ ./ % for element wise power and division
in the expressions
katelyn sowden
katelyn sowden on 23 Nov 2020
Tdb and Twb are scalars. I didn't know about syms before but using it makes a lot of sense, thank you for your help! I really appriciate you writing that in.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 23 Nov 2020
Edited: Steven Lord on 23 Nov 2020
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
esquared1 = 7.3891
esquared2 = exp(2)
esquared2 = 7.3891
% 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.
  1 Comment
katelyn sowden
katelyn sowden on 23 Nov 2020
Thank you for your suggestions! I have a bad habit of just rewriting things instead of defining variables. I'll probably turn svp into a function to reduce the risk of typos. I can't belive I didn't think to put the power in the cell, thank you for pointing that out. The red text was just that line outlined in red without any additinal direction like matlab usually gives. Thanks for taking the time to answer.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!