I need to convert this code(excell) to matlab language help?

1 view (last 30 days)
Function ZFactor(Tr, Pr)
a = 0.064225133
b = 0.53530771 * Tr - 0.61232032
c = 0.31506237 * Tr - 1.0467099 - 0.57832729 / Tr ^ 2
d = Tr
e = 0.68157001 / Tr ^ 2
f = 0.68446549
g = 0.27 * Pr
Newton-Raphson iteration (commented due to unstability and convergence problems
rho = 0.27 * Pr / Tr 'Initial guess
rhoold = rho
For i = 1 To 100
frho = a * rho ^ 6 + b * rho ^ 3 + c * rho ^ 2 + d * rho + e * rho ^ 3 * (1 + f * rho ^ 2) * Exp(-f * rho ^ 2) - g
dfrho = 6 * a * rho ^ 5 + 3 * b * rho ^ 2 + 2 * c * rho + d + e * rho ^ 2 * (3 + f * rho ^ 2 * (3 - 2 * f * rho ^ 2)) * Exp(-f * rho ^ 2)
rho = rho - frho / dfrho test = Abs((rho - rhoold) / rho)
If test < 0.00001 Then Exit For
rhoold = rho
Next i
ZFactor = 0.27 * Pr / rho / Tr

Accepted Answer

Image Analyst
Image Analyst on 4 Nov 2014
Function, For, If, Abs, and Exp go to function, if, for, abs, and exp (all lower case). Next i goes to end.
Put a % symbol in front of the "Newton-Raphson ...." line.
The function line should look like
Function output = ZFactor(Tr, Pr)
and the last line should be
output = 0.27 * Pr / rho / Tr
"To" in the for statement should be replaced with a colon :.
This
If test < 0.00001 Then Exit For
should be
if test < 0.00001
break;
end
Other than that I think it should be okay. You might want to add semicolons at the end of the lines to suppress echoing the result to the command window.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!