Function keeps getting errors, how to fix?

조회 수: 2 (최근 30일)
katelyn sowden
katelyn sowden 2020년 11월 22일
댓글: katelyn sowden 2020년 11월 23일
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.

채택된 답변

VBBV
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
  댓글 수: 2
VBBV
VBBV 2020년 11월 23일
편집: VBBV 2020년 11월 23일
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 2020년 11월 23일
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.

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

추가 답변 (1개)

Steven Lord
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
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
katelyn sowden
katelyn sowden 2020년 11월 23일
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.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by