Function with 2 variables
29 次查看(过去 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!