Function with 2 variables

29 Ansichten (letzte 30 Tage)
Keen
Keen am 9 Jul. 2018
Beantwortet: Keen am 21 Jul. 2018
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 Kommentare
James Tursa
James Tursa am 9 Jul. 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.
Keen
Keen am 9 Jul. 2018
Hi James, I sent you my coding via email would you please check it and email back when you feel free.
Kind regards, Keen

Melden Sie sich an, um zu kommentieren.

Antworten (8)

James Tursa
James Tursa am 9 Jul. 2018
Bearbeitet: James Tursa am 9 Jul. 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)

Keen
Keen am 10 Jul. 2018
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 Kommentar
James Tursa
James Tursa am 10 Jul. 2018
Please show us your current code.

Melden Sie sich an, um zu kommentieren.


Keen
Keen am 10 Jul. 2018
Bearbeitet: James Tursa am 10 Jul. 2018
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 Kommentare
James Tursa
James Tursa am 10 Jul. 2018
Bearbeitet: James Tursa am 10 Jul. 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
Keen am 19 Jul. 2018
I MADE THE REQUESTED CHANGES, BUT I`M STILL STUCK.

Melden Sie sich an, um zu kommentieren.


Keen
Keen am 10 Jul. 2018
Hi I made the changes and nothing happened to my results hence I removed it.

Keen
Keen am 10 Jul. 2018
Bearbeitet: Stephen23 am 19 Jul. 2018
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 Kommentar
Stephen23
Stephen23 am 19 Jul. 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:

Melden Sie sich an, um zu kommentieren.


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

per isakson
per isakson am 19 Jul. 2018
Bearbeitet: per isakson am 19 Jul. 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

Keen
Keen am 21 Jul. 2018
Many thanks,
Yes it works perfectly.
please advise about my errors.
Kind regards,

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by