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?

回答 (1 件)

the cyclist
the cyclist 2015 年 11 月 8 日

0 投票

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 件のコメント

Karim Belkhiria
Karim Belkhiria 2015 年 11 月 8 日
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);
Jan
Jan 2015 年 11 月 8 日
編集済み: Jan 2015 年 11 月 8 日
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)"
the cyclist
the cyclist 2015 年 11 月 8 日
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.

サインインしてコメントする。

質問済み:

2015 年 11 月 8 日

編集済み:

Jan
2015 年 11 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by