Errors in my program with mpower and mtimes

조회 수: 3 (최근 30일)
Cameron
Cameron 2014년 4월 14일
댓글: Star Strider 2014년 4월 14일
I am writing a program to find conditional density functions for a transmitter going through Gaussian noise to receiver. In order to make plots for f(x|y) vs x given different values for y I need to have f be a piece wise function which I have shown below. These are the errors I am getting when trying to run my program.
??? Error using ==> mtimes Inner matrix dimensions must agree.
Error in ==> PartA at 6 k1 = int((x/(25*((8*3.14).^(1/2)))*(e.^((-(y-x).^2)/8))),x,0,5);
Here is my function and main program. If anyone knows what I am doing wrong please help.
i = 1;
y = -5;
e = 2.71;
x = 0 : 0.1 :10;
while(y <= 20)
k1 = int((x/(25*((8*3.14)^(1/2)))*(e^((-(y-x)^2)/8))),x,0,5);
k2 = int((0.4-(x/25))*(1/((8*3.14)^(1/2))*(e^((-(y-x)^2)/8))),x,5,10);
f = piecewise(x,y,k1,k2);
figure(i)
plot(x,f,'.');
y = y + 5;
i = i + 1;
end
function [ f ] = piecewise( x , y , k1, k2 )
e = 2.71;
if x > 0 && x <= 5
f = ((1/k1) * (x/(25*((8*3.14)^(1/2))) * (e^((-(y-x)^2)/8))));
elseif x > 5 && x <= 10
f = ((1/k2) * (0.4-(x/25)) * (1/((8*3.14)^(1/2))*(e^((-(y-x)^2)/8))));
else
f = NaN;
end
end

답변 (2개)

dpb
dpb 2014년 4월 14일
Error in ==> PartA at 6
k1 = int((x/(25*((8*3.14).^(1/2)))*(e.^((-(y-x).^2)/8))),x,0,5);
Looks like you're missing the 'dot' on the '.*' operator. You used it for the exponent, but unless you're intending doing a matrix multiplication of the vectors instead of element-by-element, you need it there, too.

Cameron
Cameron 2014년 4월 14일
thanks for the quick feedback, but I have made this change multiple times and I end up getting a similar error message on the same line. I have edited my program to this and I am still getting strange error messages which are:
Error in ==> fun1 at 3 f1 = (x/(25*((8*3.14).^(1/2))) * (e.^((-(y-x).^2)/8)));
Error in ==> @(x)fun1(x,y)
Error in ==> quad at 76 y = f(x, varargin{:});
Error in ==> PartA at 5 k1 = quad(@(x)fun1(x,y),0,5);
i = 1;
y = -5;
e = 2.71;
while(y <= 20)
k1 = quad(@(x)fun1(x,y),0,5);
k2 = quad(@(x)fun2(x,y),5,10);
f = piecewise(x,y,k1,k2);
figure(i)
plot(x,f,'.');
y = y + 5;
i = i + 1;
end
-------------And fun1 and fun2 are as follows
function [ f1 ] = fun1( x , y )
e = 2.71;
f1 = (x/(25*((8*3.14).^(1/2))) * (e.^((-(y-x).^2)/8)));
end
function [ f2 ] = fun2( x , y )
e = 2.71;
f2 = (0.4-(x/25)) * (1/((8*3.14).^(1/2))*(e.^((-(y-x).^2)/8)));
end
  댓글 수: 2
dpb
dpb 2014년 4월 14일
Whatever changes you've made, you still didn't change the '*' to '.*' in
Error in ==> fun1 at 3 f1 = (x/(25*((8*3.14).^(1/2))) * (e.^((-(y-x).^2)/8)));
What are dimensions of x and y and what is the dimension of f1 intended to be?
Star Strider
Star Strider 2014년 4월 14일
I suggest putting ‘.’ in front of every *, / and ^ operator in a function unless you know you want to do matrix operations.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by