Errors in my program with mpower and mtimes

3 views (last 30 days)
Cameron
Cameron on 14 Apr 2014
Commented: Star Strider on 14 Apr 2014
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

Answers (2)

dpb
dpb on 14 Apr 2014
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 on 14 Apr 2014
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 Comments
dpb
dpb on 14 Apr 2014
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 on 14 Apr 2014
I suggest putting ‘.’ in front of every *, / and ^ operator in a function unless you know you want to do matrix operations.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!