Function with 2 variables
32 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
James Tursa
le 9 Juil 2018
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.
Réponses (8)
James Tursa
le 9 Juil 2018
Modifié(e) : James Tursa
le 9 Juil 2018
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 commentaires
Keen
le 10 Juil 2018
Modifié(e) : James Tursa
le 10 Juil 2018
2 commentaires
James Tursa
le 10 Juil 2018
Modifié(e) : James Tursa
le 10 Juil 2018
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
le 10 Juil 2018
Modifié(e) : Stephen23
le 19 Juil 2018
1 commentaire
Stephen23
le 19 Juil 2018
@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
le 19 Juil 2018
Modifié(e) : per isakson
le 19 Juil 2018
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 commentaires
Voir également
Catégories
En savoir plus sur Performance and Memory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!