problem matrix dimensions dont agree

I am trying to write a function that calculates the Vandermonde-Matrix. The function was working fine and everything is good, and then I think I changed something by error, I dont know what, and I am not seeing something wrong in the function, and it doesnt work anymore.
Here is my function:
function c = interpolation(x, y)
n = length(x);
V = ones(n);
for j = 2:n
V(:,j) = x.*V(:,j-1);
end
c = V \ y;
disp(V)
end
and here is the error I am getting:
Error using .*
Matrix dimensions must agree.
Error in interpolation (line 5)
V(:,j) = x.*V(:,j-1);
I gived as arguments x1 = [1 2 3 4 ] and x2= [5 6 7 8] I tried many different x1 and x2, and anyone works now :-/ The same x1 and x2 I gived as arguments before this error occurs, and still not working :/
Anyone has an idea?

Answers (1)

The proximate cause of the error is that in the line
V(:,j) = x.*V(:,j-1);
x is a 1x4 vector, and V(:,j-1) is a 4x1 vector.
Because V(:,j) is a 4x1 vector, I'm guessing you might want
V(:,j) = x'.*V(:,j-1);
But then the next line after the loop doesn't work, but I'm not sure what you want to do there.

3 Comments

yes it was because I had to give a column vector for ex x=[1 2 3 4]'
But the problem now, is that I want to use the output values, to make a polynom of this from p(x) = c0*x^0 + c1*x^1 + c2*x^2 + ... cn-1*x^(n-1) and plot it! I used this:
function c = interpolation(x, y)
n = length(x);
V = ones(n);
for j = 2:n
V(:,j) = x.*V(:,j-1);
end
c = V \ y;
disp(V)
for i = 0:n-1
fprintf('c%d= %.3f\n', i, c(i+1));
end
polynom(c);
function polynom(c)
n = length(c);
for l= 0:n-1
polynom = polynom + c^l;
end
disp(p)
end
end
but still not working.. with this error (code working till "polynom(c)".. it is displaying the output of the first function 'c' and displaying the matrix V):
Error using interpolation/polynom
Too many output arguments.
Error in interpolation/polynom (line 17)
polynom = polynom + c^l;
Error in interpolation (line 13)
polynom(c);
What is the purpose of this line:
polynom(c);
? Call the function polynom without catching the output? But then the result is calculated, but not used anywhere. In addition the function polynom dopes not have an output. Inside the function you call it recursively:
polynom = polynom + c^l;
But "polynom" is the function itself, but it does not create an output. So do not use the name of the function as name of the variable for collecting the sum. In addition I can see only a weak relation between "polynom = polynom + c^l" and "p(x) = c0*x^0 + c1*x^1 + c2*x^2 + ... cn-1*x^(n-1)"
You are using polynom as both a variable name and a function name. You need to change one of those.
If you change the variable to "polyn", for example, it may work. Don't forget to initialize it to some value -- guessing it should be zero.
Also, I think you probably want
c.^l
instead of just
c^l
but I'm not sure.

Sign in to comment.

Asked:

on 8 Nov 2015

Edited:

Jan
on 8 Nov 2015

Community Treasure Hunt

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

Start Hunting!