error when call function

hello,
I use matlab for excercises on the university. I declare a function 'benadering_four' and when I ask for benadering_four(0) (so 0 put in the function) then it works but when I put a vector in the function (in the code, benadering_four(xx) ) then it gives me the error:
*Error using * Inner matrix dimensions must agree.
Error in @(y)[1,cos([1:floor(n/2)]*y),sin([1:floor(n/2)]*y)]
Error in @(y)Fourier(pi*y)*coef_four*
xx = -1:0.001:1;
n = 9;
coef_four = 1/pi*[pi*atan(5)/5; 1.08104; .552133; .30444; .157123; 0; 0; 0; 0]
Fourier = @(y) [1 cos([1:floor(n/2)]*y) sin([1:floor(n/2)]*y)]
benadering_four = @(y) Fourier(pi*y)*coef_four
benadering_four(0)
benadering_four(xx)
I doesn't understand why it works with an number and why it doesn't work with a vector. Can someone help me?

 Accepted Answer

Adam
Adam on 30 Oct 2014
Edited: Adam on 30 Oct 2014
cos([1:floor(n/2)]*y
will only be valid syntax for y being a vector in very specific circumstances - i.e. if they are the same length and one is a row vector, the other is a column vector. Depending which way round they are in that case you either get a scalar result or a square matrix result.
In all other cases you get a syntax error because you cannot do matrix multiplication (or indeed element-wise multiplication) on two arbitrary vectors of different lengths.
In this case I don't know what your expectations would be from passing a vector in. Do you expect a 3-element vector out? If so then y needs to be a column vector of length n.

4 Comments

Koen
Koen on 30 Oct 2014
Edited: Koen on 30 Oct 2014
coef_four = 1/pi*[pi*atan(5)/5; 1.08104; .552133; .30444; .157123; 0; 0; 0; 0]
Fourier = @(y) [1 cos([1:floor(n/2)]*y) sin([1:floor(n/2)]*y)]
benadering_four = @(y) Fourier(pi*y)*coef_four
the size of Fourier is (1,9) (because floor(9/2)=4 ) and the size of coef_four is (9,1). So the vector product is well defined. benadering_four(0) works (it do give me the benadering_four function calculated in 0) but when I do benadering_four(xx) (here xx = -1:0.001:1;) i expect a vector of the same size of xx but I just get an error. (I think i don't understand your explanation)
Adam
Adam on 30 Oct 2014
Well if floor(n/2) is 4 then [1:floor(n/2)] is going to be a length 4 vector. You can't multiply a length 4 vector by whatever the length of xx is (2001 in this case).
The size of coef_four is not the problem, the error is in the Fourier part of the function.
Koen
Koen on 30 Oct 2014
Edited: Koen on 30 Oct 2014
I thought I would get the following: a matrix A of size (2001, 9) (2001 is the size of xx) with
A =[1 cos(xx(1)) cos(2*xx(1)) ... sin(4*xx(1));
1 cos(xx(2)) cos(2*xx(2)) ... sin(4*xx(2));
...
1 cos(xx(2001)) cos(2*xx(2001)) ... sin(4*xx(2001))]
How do I get this?
Adam
Adam on 30 Oct 2014
Try transposing xx or whatever you are passing in.
You can do matrix multiplication if you have a row vector and column vector but not two of the same.

Sign in to comment.

More Answers (0)

Categories

Find more on Aerospace Blockset in Help Center and File Exchange

Asked:

on 30 Oct 2014

Commented:

on 30 Oct 2014

Community Treasure Hunt

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

Start Hunting!