Fuctions: Multiple inputs & outputs with if statements

조회 수: 7 (최근 30일)
Mizo Amreya
Mizo Amreya 2020년 7월 10일
댓글: Mizo Amreya 2020년 7월 10일
Hello,
I'm an amatuer user of Matlab and I'm looking to enhance my knowledge of coding.
I've written a code in the past to calculate relative permeabilty using functions.
I had a separate function file for water, and another for oil both involving if/elseif/else statements.
I'm trying to make my code more efficient and combine both functions into one function with multiple inputs/outputs.
But I'm not getting the same answer that I'm expecting and getting with seperate function files. Why is that? and how can I resolve it?
Thanking you in advance.
Code for Oil Relative Permeability:
function kro = rel_perm_oil(So,Swr,Sor,no,krostar)
% Calculation of Oil Relative Permeability
if So <= Sor
kro = 0;
elseif So >= 1 - Swr
kro = krostar;
else
kro = krostar*(((So-Sor)/(1-Sor-Swr))^no);
end
Code for Water Relative Permeability:
function krw = rel_perm_wat(Sw,Swr,Sor,nw,krwstar)
% Calculation of Water Relative Permeability
if Sw <= Swr
krw = 0;
elseif Sw >= 1-Sor
krw = krwstar;
else
krw = krwstar*(((Sw-Swr)/(1-Sor-Swr))^nw);
end
Combined code for both oil & water permeabilities:
function [krw,kro] = rel_perm(Sw,So,Swr,Sor,nw,no,krwstar,krostar)
% Calculation of Relative Permeability
% Water Relative Permeability
if Sw <= Swr
krw = 0;
elseif Sw >= 1-Sor
krw = krwstar;
else
krw = krwstar*(((Sw-Swr)/(1-Sor-Swr))^nw);
end
% Oil Relative Permeability
if So <= Sor
kro = 0;
elseif So >= 1 - Swr
kro = krostar;
else
kro = krostar*(((So-Sor)/(1-Sor-Swr))^no);
end

채택된 답변

Matt J
Matt J 2020년 7월 10일
편집: Matt J 2020년 7월 10일
No reason at all. And I don't see any disagreement:
K>> [krw,kro] = rel_perm(1,1,1,1,1,1,1,1)
krw =
0
kro =
0
K>> krw = rel_perm_wat(1,1,1,1,1)
krw =
0
K>> kro = rel_perm_oil(1,1,1,1,1)
kro =
0
Solved!!
  댓글 수: 1
Mizo Amreya
Mizo Amreya 2020년 7월 10일
You're right. I should get the same values.
Turns out the problem I had was with calling the function in my code.
Thank you for your effort.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Partial Differential Equation Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by