How to solve the following error in matlab "Error using semilogy: Invalid second data argument"?!
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello!
I am trying to run the following code about numerical integration in Matlab, but I get an error that I do not understand. The error is:
"Error using semilogy
Invalid second data argument".
What is causing this error in the code?
Thank you.
close all;clear;
klin1=1; klin2=0.5;
variance1 = sqrt(1/(2.*(klin1+1)));
mean1=sqrt(1.*klin1/(klin1+1));
variance2 = sqrt(1/(2.*(klin2+1)));
mean2=sqrt((1.*klin2)/(klin2+1));
avgdb= 30;
avg=10.^(avgdb./10);
fx=@(x) 0;
for q=0:15
for w=0:15
c2=(1./(4.*variance1.^2.*variance2.^2)).*exp(-mean1.^2/(2.*variance1.^2)).*exp(-mean2.^2./(2.*variance2.^2)).*(1./((factorial(q).^2).*(factorial(w)).^2))*(((mean1)./(2.*variance1.^2)).^(2.*q)).*(((mean2)./(2.*variance2.^2)).^(2.*w))*((1./(2.*(variance2.^2))).^(q-w)).*(1./avg).^((q+1));
f2=@(x) c2.*(1./(2.*x)).^(-q-1).*MeijerG({[1+q-w,1,q+2], []}, {[], [q+1]}, ((avg./(2.*x)).*(4.*variance1.^2.*variance2.^2)));
fx=@(x) fx+f2;
end
end
outage=@(x) 1-fx.*fx;
P=@(x) (exp(-x)*x^(-0.5)*outage);
pe=@(x) (1/(2*sqrt(pi)))*integral(P,1,100);
semilogy(avgdb,pe);
By the way the MeijerG function i am using is the following:
function y = MeijerG(a,b,z)
% MeijerG
% A wrapper for MATLAB symbolic library implementation of the 'MeijerG' G^{m,n}_{p,q}(...|z) function.
% It uses Maple with MATLAB 2008a or earlier; muPad with later versions.
%
% Syntax :
% MeijerG({[a_1,...a_n],[a_n+1,...a_p]},{[b_1,...b_m],[b_m+1,...b_q]},z)
% Input arguments :
% a - {[a_1,...a_n],[a_{n+1},...a_p]}
% b - {[a_1,...a_m],[a_{m+1},...a_q]}
% z - matrix of (possibly complex) numbers
% Output:
% y - has same dimensions as 'z'
%
% Notes:
% 1.) For invalid arguments, 'double' function for converting results back
% from symbols would return an error.
% 'MeijerG' catches the error, displays a warning, and sets corresponding
% position of 'y' to 'nan'.
% 2.) 'double' to 'string' conversion used for forming the symbolic
% expressions causes a precision loss, and possibly, round of errors.
% 3.) Sometimes, even the slightest changes to arguments
% could produce unacceptable results.
% e.g.
% >> MeijerG({[1,1], []},{1, 1},[1,2,3])
% ans =
% NaN 0.666666666666667 0.750000000000000
% >> MeijerG({[1,1], []},{1, 1},[1+1e-5,2,3])
% ans =
% 0.500002499987500 0.666666666666667 0.750000000000000
% Here the second result appears correct, since
% MeijerG({[1,1], []},{1, 1},z ) = z/(z+1)
% Please let me know if such issues can be circumvented.
%
% Author : Damith Senaratne, (http://www.damiths.info)
% Released date : 16th August 2011
if verLessThan('matlab','7.7.0')
useMaple = true;
maple('MeijerGi := sqrt(-1);');
else
useMaple = false;
SE = symengine;
evalin(SE,'MeijerGi := sqrt(-1)');
end
% Note: Given the performance penalty and other implications,
% the symbolic engine is not reset within 'MeijerG'.
% Hence, the choosing a simple identifier for imaginary unit (e.g. 'i')
% appears difficult.
% The identifier 'MeijerGi' is chosen above hoping that it won't conflict
% with any already defined symbolic variable.
% extract the arguments
a1 = a{1}; a2 = a{2};
b1 = b{1}; b2 = b{2};
a1 = a1(:); a2 = a2(:);
b1 = b1(:); b2 = b2(:);
zvec = z(:); % vectorize (to make the computation independent of the dimensions)
y = zeros(size(zvec));
for k=1:length(y)
% iterate for each element of 'z'!
if imag(z(k))>=0
zStr = sprintf('%g + MeijerGi* %g',real(z(k)),imag(z(k)));
else
zStr = sprintf('%g - MeijerGi* %g',real(z(k)),abs(imag(z(k))));
end
if useMaple
cmd = sprintf('evalf(MeijerG([%s,%s],[%s,%s],%s))',intVecToStr(a1),intVecToStr(a2),intVecToStr(b1),intVecToStr(b2),zStr);
[r,s] = maple(cmd);
if s ~= 0
warning('MeijerG: failed evaluating for k = %d',k);
y(k) = nan;
else
r = str2double(r);
y(k) = r(1);
end
else
expr = sprintf('meijerG([%s,%s],[%s,%s],%s)',intVecToStr(a1),intVecToStr(a2),intVecToStr(b1),intVecToStr(b2),zStr);
[r,s] = evalin(SE,expr);
if s ~= 0
warning('MeijerG: failed evaluating for k = %d',k);
y(k) = nan;
else
try
y(k) = double(r(1));
catch err
warning('MeijerG: failed evaluating for k = %d',k);
y(k) = nan;
end
end
end
end
y = reshape(y,size(z));
% -----------------------
% function: intVecToStr
% -----------------------
% converts elements of an integer array 'a' to a string
function s = intVecToStr(a)
s = ['[', sprintf(' %ld,',a)];
s = [s(1:end-1), ' ]'];
3 Kommentare
David Wilson
am 19 Apr. 2019
Bearbeitet: David Wilson
am 19 Apr. 2019
It seems that pe is an anonymous function, so you need to give it an argument.
semilogy(avgdb,pe(x))
But I suspect that's not your only problem. The variable avgdb is a scalar. Is that what you want to plot? A single point?
Also I'm a bit unclear about things like:
fx=@(x) fx+f2;
I think you mean
fx=@(x) fx(x) + f2(x)
You'll need to correct quite a bit of that type of error.
Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!