Error while using a function.I need parentheses in the function header but it wont allow me to do it.

35 views (last 30 days)
N_freq = length(fs)
Av_k = zeros(N_freq,N_Stages);
for kk =1:N_Stages;
[Av_k2,] = EE_TF(fs,G1(kk),G2(kk), G3(kk),C1(kk),C2(kk));
Av_k(:,kk)= Av_kk;
end
%Total Gain
Av_k_dB = 20*log(Av_k);
Av_db = sum(Av_k_dB);
%------------------------------------------------------------------------------------------------------
function [Av_k2] = EE_TF(fs,G1(k2);G2(k2), G3(k2),C1(k2),C2(k2));
%-----------------------------------------------
A0=-G1*G3
B2=C1*C2;
B1=(G1+G2+G3)*C2;
B0=(G2*G3);
A0=-G1*G3
B2=C1*C2;
B1=(G1+G2+G3)*C2;
B0=(G2*G3);
Av_k2= A0/(B2.^2+B1 + B0);
  1 Comment
Tommy
Tommy on 21 Apr 2020
G1(k2), for example, is not a valid variable name:
>> isvarname('G1(k2)')
ans =
logical
0
but when you define a function with
function [Av_k2] = EE_TF(fs,G1(k2),G2(k2), G3(k2),C1(k2),C2(k2))
...
end
you are saying that the second argument supplied to the function will be named G1(k2) within the function, so MATLAB isn't happy. You don't need the indices in the function header because you supply them when you call the function. Try this for the header:
function [Av_k2] = EE_TF(fs,G1,G2, G3,C1,C2)
...
end

Sign in to comment.

Answers (2)

dpb
dpb on 21 Apr 2020
Edited: dpb on 21 Apr 2020
No, don't need any parens (and remove the semicolon, too) in the function dummy argument list, those are surrogate names for the actual arguments passed.
function [Av_k2] = EE_TF(fs,G1,G2,G3,C1,C2);
...
does exactly what you want...when you call it with
EE_TF(fs,G1(kk),G2(kk),G3(kk),C1(kk),C2(kk));
inside the function each Gn is the value passed, NOT the array Gn in the main program...it's a whole new variable with scope only within the function.
However, the power in MATLAB is to vectorize your functions so you can call them without the loop construct in the main program...
To do that, use the "dot" operators--in your case that's all that looks like is needed--
function [Av_db, Avk_dB] = EE_TF(fs,G1,G2,G3,C1,C2);
A0=-G1.*G3
B2= C1.*C2;
B1=(G1+G2+G3).*C2;
B0= G2.*G3;
A0=-G1.*G3;
B2= C1.*C2;
B1=(G1+G2+G3).*C2;
B0=(G2.*G3);
Av_k2= A0./(B2.^ +B1+B0);
%Total Gain
Avk_dB = 20*log(Av_k);
Av_db = sum(Av_k_dB);
end
I went ahead a moved the dB and total gain into the function as well--and returned the stage raw gains as an optional second output if they're wanted/needed as well.
Shouldn't log be log10, though?

BALAJI KARTHEEK
BALAJI KARTHEEK on 21 Apr 2020
the is no need of kk, just modify the function as given below and try your luck
function [Av_k2] = EE_TF(fs,G1;G2,G3,C1,C2);
%-----------------------------------------------
A0=-G1*G3
B2=C1*C2;
B1=(G1+G2+G3)*C2;
B0=(G2*G3);
A0=-G1*G3
B2=C1*C2;
B1=(G1+G2+G3)*C2;
B0=(G2*G3);
  1 Comment
Steven Lord
Steven Lord on 21 Apr 2020
Three comments, two of which are stylistic and one is significant.
function [Av_k2] = EE_TF(fs,G1;G2,G3,C1,C2);
When you have just one output argument, it is not necessary to wrap it in square brackets. The following would work the same way.
function Av_k2 = EE_TF(fs,G1;G2,G3,C1,C2);
Similarly, you don't need to end the function definition with a semicolon.
function Av_k2 = EE_TF(fs,G1;G2,G3,C1,C2)
Finally, and most important, input arguments in a function definition must be separated by commas, not semicolons. The previous two points may cause Code Analyzer (or people performing a code review on your function0 to complain but will still allow your function to run; unless you fix this, you won't be able to run your function.
function Av_k2 = EE_TF(fs,G1,G2,G3,C1,C2)
There's a fourth comment, that you never assign a value to Av_k2 in this code, but since you said "modify the function" I'm assuming you're leaving implementing that part of the code as an exercise to the reader.

Sign in to comment.

Categories

Find more on Modeling in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!