Sending parameters to a function

6 views (last 30 days)
Jon
Jon on 30 Apr 2011
I have a function that looks like this:
function ci = cifun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
syms G2;syms T;syms TM;syms TMO;syms ny;syms A;syms Epsi;
syms b;syms x;syms y;syms Vc;syms c2;syms D2;syms Q;syms z;
R = 8.314472;
k = 1.38065E-23;
D = D2.*exp(-Q./(R.*T));
cidel = matlabFunction(cidelfun, 'Vars',{'z' 'T' 'TM' 'TMO' 'G2' 'A' 'b' 'x' 'Epsi' 'ny' 'D2' 'Q' 'R' 'Vc'});
cidelfun2 = @(z) cidel(z,T,TM,TMO,G2,A,b,x,Epsi,ny,D2,Q,R,Vc);
ci = ((Vc.*c2)./D).*exp(-(Wifun./(T.*k))-(Vc.*y./D))*quad(cidelfun2,-250*b,250*b);
end
It needs certain parameters to work and if I write them in the function it works. But the problem is that I'm going to use this function with different parameters different times so I need to send them along when I call the function. I tried to write like this:
T = 1273;
TM = 2163;
TMO = -0.5;
G2 = 126000;
A = 1.2E-29;
b = 0.000000000258;
x = 2/3*b;
Epsi = 0.00487903650119246;
ny = 0.3;
D2 = 0.0000169;
Vc = 1E-9;
c2 = 18.07551;
Q = 263900;
R = 8.314472;
ci = cifun(T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
But it don't work. So my question is how to do to make it work?

Accepted Answer

Matt Fig
Matt Fig on 30 Apr 2011
Of course, "But it don't work" is very vague.
Now, even though you don't say what happens, I am guessing that your input variables are immediately overwritten in the function by your declarations. When a value is passed in, you don't then re-declare it inside the function, because then whatever you passed in is lost. For example, if you pass in a value for T of 1273, as you show, the first thing that happens (on the first line!) in your function is that the value you passed in is overwritten and T is declared as a symbolic variable.
Try this simple function as an example. It simply takes a number and doubles it. First try to double any number using the function as I have written it, then try it again with the first line of the function erased. See if you get a different answer!
function B = mydouble(A)
A = rand; % Erase this line after you try to call, say: mydouble(3)
B = 2*A;
%
%
%
EDIT In response to your 'answer' below...
The first error is because you have declared your function to take 14 argument with this line:
function ci = cifun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
But then on this line,
ci = cifun(T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
You only pass in 13. MATLAB doesn't care about the names, just the number and order. When you call your function as above, MATLAB goes through and assigns the first passed argument to the first declared parameter. So it gives the value you passed in as T to the parameter y inside the function. Then it assigns the second passed argument to the second declared parameter. So it gives the value you passed in as TM to the parameter T inside the function... and so on until you see that the value you passed in as Q goes to the parameter D2 inside the function, leaving Q inside the function undefined.
So if your function declaration says it takes N arguments and you only pass N-1, the Nth parameter will be undeclared inside the function. If you try to then use this parameter, you will see the same error you saw.
Since you declare y as syms inside the function, you are back to the first problem you had. That you are overwriting the value as soon as it is passed in. So take the y out of the function declaration line!
  1 Comment
Jon
Jon on 30 Apr 2011
Thank you. I solved it by putting syms y in the main window and then adding y so the number and order got correct.

Sign in to comment.

More Answers (1)

Jon
Jon on 30 Apr 2011
Oops, I'm new to this site and I will try to be more clear in my questions. Thank you for pointing it out.
If I write my function like this:
function ci = cifun(y,T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
syms y;
R = 8.314472;
k = 1.38065E-23;
D = D2.*exp(-Q./(R.*T));
cidel = matlabFunction(cidelfun, 'Vars',{'z' 'T' 'TM' 'TMO' 'G2' 'A' 'b' 'x' 'Epsi' 'ny' 'D2' 'Q' 'R' 'Vc'});
cidelfun2 = @(z) cidel(z,T,TM,TMO,G2,A,b,x,Epsi,ny,D2,Q,R,Vc);
ci = ((Vc.*c2)./D).*exp(-(Wifun./(T.*k))-(Vc.*y./D))*quad(cidelfun2,-250*b,250*b);
end
And do like this:
T = 1273;
TM = 2163;
TMO = -0.5;
G2 = 126000;
A = 1.2E-29;
b = 0.000000000258;
x = 2/3*b;
Epsi = 0.00487903650119246;
ny = 0.3;
D2 = 0.0000169;
Vc = 1E-9;
c2 = 18.07551;
Q = 263900;
ci = cifun(T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
I get this error:
??? Input argument "Q" is undefined.
Error in ==> cifun at 6
D = D2.*exp(-Q./(R.*T));
Error in ==> test at 41
ci = cifun(T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
And if I ad syms Q to the function I get this error:
??? Undefined function or method 'isfinite' for input arguments of type 'sym'.
Error in ==> quad at 81
if ~isfinite(y(1))
Error in ==> cifun at 10
ci = ((Vc.*c2)./D).*exp(-(Wifun./(T.*k))-(Vc.*y./D))*quad(cidelfun2,-250*b,250*b);
Error in ==> test at 41
ci = cifun(T,TM,TMO,G2,A,b,x,Epsi,ny,Vc,c2,D2,Q)
But if I add:
Q = 263900;
Inside the function, It works. Both with and without the syms Q in the function. I guess the problem is that Q is not used in the actual cifun but in defining D in the function. So now the problem is to get the parameter Q to the function without having to write it inside the function. Do I have to write D as a separate function to do this?

Tags

Community Treasure Hunt

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

Start Hunting!