problem matrix dimensions dont agree

3 visualizzazioni (ultimi 30 giorni)
Karim Belkhiria
Karim Belkhiria il 8 Nov 2015
Modificato: Jan il 8 Nov 2015
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?

Risposte (1)

the cyclist
the cyclist il 8 Nov 2015
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 Commenti
Jan
Jan il 8 Nov 2015
Modificato: Jan il 8 Nov 2015
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 il 8 Nov 2015
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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by