Function with 2 variables
조회 수: 32 (최근 30일)
이전 댓글 표시
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
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.
답변 (8개)
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)
댓글 수: 0
Keen
2018년 7월 10일
편집: James Tursa
2018년 7월 10일
댓글 수: 2
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
2018년 7월 10일
편집: Stephen23
2018년 7월 19일
댓글 수: 1
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:
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!