Function with 2 variables

조회 수: 32 (최근 30일)
Keen
Keen 2018년 7월 9일
답변: Keen 2018년 7월 21일
Hi all,
I have a function y=f(x, Z), when I give inputs, it takes Z as a fix value. Would you please advise how can my function accept z as a variable? I need different inputs of X and Z to get different outputs of y.
Kind regards,
  댓글 수: 2
James Tursa
James Tursa 2018년 7월 9일
Please show the actual code you are using, not just a description of it. You can certainly pass two variables with the f(x,Z) syntax you mention, but only if the function is coded properly.
Keen
Keen 2018년 7월 9일
Hi James, I sent you my coding via email would you please check it and email back when you feel free.
Kind regards, Keen

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

답변 (8개)

James Tursa
James Tursa 2018년 7월 9일
편집: James Tursa 2018년 7월 9일
If you are passing in arguments that are vectors, then everything about your calculations inside the function needs to be vectorized. You've got divisions in your calculations that are not vectorized. E.g., this
CA= 10.^(0.1482*(((log(ZP)+3.0166)/(-0.1782*log(SA)-1.0026)).^2)+...
should be this instead
CA= 10.^(0.1482*(((log(ZP)+3.0166)./(-0.1782*log(SA)-1.0026)).^2)+... <-- changed / to ./
Also, your if-tests will not work with vectors since some of the elements might satisfy the condition while other elements do not. MATLAB isn't going to magically jump into the if-block for only those elements that satisfy the condition. So you need to rewrite that part of your code as well. (e.g., maybe using simple loops)

Keen
Keen 2018년 7월 10일
Hi James, I tried it and is the same error appearing. The equation is correct because the input of the first values of ZP and SA gave a correct value. The problem is my program accepts only one value of SA.
  댓글 수: 1
James Tursa
James Tursa 2018년 7월 10일
Please show us your current code.

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


Keen
Keen 2018년 7월 10일
편집: James Tursa 2018년 7월 10일
function [CA]=myfunction2(ZP,SA)
CA= 10.^(0.1482*(((log(ZP)+3.0166)/(-0.1782*log(SA)-1.0026)).^2)+(0.8796*((log(ZP)+3.0166)/(-0.1782*log(SA)-1.0026)))+3.207);
if ZP>= 0
CA= 10.^(0.1482*(((log(ZP)+3.0166)/(-0.1782*log(SA)-1.0026)).^2)+(0.8796*((log(ZP)+3.0166)/(-0.1782*log(SA)-1.0026)))+3.207);
elseif ZP< 0
CA= 10.^(0.1482*(((-(log(-ZP))+3.0166)/(-0.1782*log(SA)-1.0026)).^2)+(0.8796*((-(log(-ZP))+3.0166)/(-0.1782*log(SA)-1.0026)))+3.207);
end
if (CA>= 0)&& (CA <= 10)
CA=0 , disp('stronglywaterwet & CA=0') %#ok<NOPRT>
elseif (CA >= 10)&& (CA <= 89) %#ok<*BDSCA>
disp ('waterwet');
elseif (CA >= 90)&& (CA <= 180) %#ok<*BDSCA>
disp ('oilwet');
plot(ZP,CA)
end
NB: my inputs are ZP=[-41 -45 -53], SA=[0.001 0.005 0.01] my outputs CA should be [79 93 110]
  댓글 수: 2
James Tursa
James Tursa 2018년 7월 10일
편집: James Tursa 2018년 7월 10일
You haven't made the changes I suggested to you. You need to change the / matrix division operator to the ./ element-wise division operator (note the period). And you haven't changed your if-tests to be vectorized either (either use logical indexing or loop over the elements one-by-one). You will continue to have problems until you make these changes.
Keen
Keen 2018년 7월 19일
I MADE THE REQUESTED CHANGES, BUT I`M STILL STUCK.

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


Keen
Keen 2018년 7월 10일
Hi I made the changes and nothing happened to my results hence I removed it.

Keen
Keen 2018년 7월 10일
편집: Stephen23 2018년 7월 19일
function [CA]=myfunction2(ZP,SA)
CA= 10.^(0.1482*(((log(ZP)+3.0166)./(-0.1782*log(SA)-1.0026)).^2)+(0.8796*((log(ZP)+3.0166)./(-0.1782*log(SA)-1.0026)))+3.207);
if ZP>= 0
CA= 10.^(0.1482*(((log(ZP)+3.0166)./(-0.1782*log(SA)-1.0026)).^2)+(0.8796*((log(ZP)+3.0166)./(-0.1782*log(SA)-1.0026)))+3.207);
elseif ZP< 0
CA= 10.^(0.1482*(((-(log(-ZP))+3.0166)./(-0.1782*log(SA)-1.0026)).^2)+(0.8796*((-(log(-ZP))+3.0166)./(-0.1782*log(SA)-1.0026)))+3.207);
end
for n=1:3
ZP=ZP(n);
end
for n=1:3
SA=SA(n);
end
  댓글 수: 1
Stephen23
Stephen23 2018년 7월 19일
@keen: you need to either vectorize your code or use loops properly, indexing into the output array. The loops you have written will always throw errors:
for n=1:3
ZP=ZP(n);
end
On the first iteration (assuming that ZP non-empty) then your loop redefines ZP to be scalar (because you redefine ZP to be the first element of ZP). On the second iteration you try to access the second element of ZP, which does not exist because you just redefined ZP to be scalar (so it only has one element).
You need to learn how to debug your code, which means actually looking at what the code is really doing (not what you want or think it is doing) and to read about code vectorization and how to use loops:

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


Keen
Keen 2018년 7월 10일
My problem is my function cannot read the variable SA (2nd and 3rd value).
Kind regards,

per isakson
per isakson 2018년 7월 19일
편집: per isakson 2018년 7월 19일
and try
>> ZP=[-41 -45 -53]; SA=[0.001 0.005 0.01];
>> myfunction2( ZP, SA )
ans =
79.7988 92.8386 109.8079
where
function CA = myfunction2( ZP, SA )
%
fpos = @(z,s) 10.^(0.1482*(((log10(z)+3.0166)./(-0.1782*log10(s)-1.0026)).^2) ...
+(0.8796*((log10(z)+3.0166)./(-0.1782*log10(s)-1.0026)))+3.207) ;
fneg = @(z,s) 10.^(0.1482*(((-(log10(-z))+3.0166)./(-0.1782*log10(s)-1.0026)).^2)...
+(0.8796*((-(log10(-z))+3.0166)./(-0.1782*log10(s)-1.0026)))+3.207);
%
ispos = ZP >= 0;
isneg = ZP < 0;
%
CA = nan(size(ZP));
CA( ispos ) = fpos( ZP(ispos), SA(ispos) );
CA( isneg ) = fneg( ZP(isneg), SA(isneg) );
end

Keen
Keen 2018년 7월 21일
Many thanks,
Yes it works perfectly.
please advise about my errors.
Kind regards,

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by