Help vectors size doesn't match...why?

This is my code to evaluate Lagrange where x_in are the given interpolation points and x_out is suppose to be f(x_in), but it keeps saying x_in and x_out don;t have the same vector length
x = -1:50;
y = 1.0 ./ (1+9*x.^2);
x_in=linspace(-1,1,100);
x_out=(1.0) ./ (1+9*(x_in).^2);
y_out=make_Ln(x,x_in,x_out);
plot(x,y,':',x_in,y_out,'*');
title('Lagrange Interpolation:x_in=linspace(-1,1,100)')
legend('y','Interpolation');
figure;

6 件のコメント

Zoltán Csáti
Zoltán Csáti 2014 年 10 月 23 日
Show your make_Ln function.
Guillaume
Guillaume 2014 年 10 月 23 日
And also give us the exact error message you get, particularly the bit that shows you the failing instruction.
cakey
cakey 2014 年 10 月 23 日
Here is my function call:
if true
% code
endfunction y=lagrange(x,x_in,x_out)
n=size(x_in,2); L=ones(n,size(x,2)); if (size(x_in,2)~=size(x_out,2)) fprintf(1,'x_in and x_out must have same elements'); y=NaN; else for i=1:n for j=1:n if (i~=j) L(i,:)=L(i,:).*(x-x_in(j))/(x_in(i)-x_in(j)); end end end y=0; for i=1:n y=y+x_out(i)*L(i,:); end end
if true
% code
end
cakey
cakey 2014 年 10 月 23 日
if true
% code
endfunction y=lagrange(x,x_in,x_out)
n=size(x_in,2); L=ones(n,size(x,2)); if (size(x_in,2)~=size(x_out,2)) fprintf(1,'x_in and x_out must have same elements'); y=NaN; else for i=1:n for j=1:n if (i~=j) L(i,:)=L(i,:).*(x-x_in(j))/(x_in(i)-x_in(j)); end end end y=0; for i=1:n y=y+x_out(i)*L(i,:); end end
cakey
cakey 2014 年 10 月 23 日
Error using plot Vectors must be the same lengths.
cakey
cakey 2014 年 10 月 23 日
編集済み: Andrei Bobrov 2014 年 10 月 24 日
Sorry it is this one:
y_out=make_Ln(x,x_in,x_out)
n=size(x_in,2);
L=ones(n,size(x,2));
if (size(x_in,2)~=size(x_out,2))
fprintf(1,'x_in and x_out must have same elements');
y=NaN;
else
for i=1:n
for j=1:n
if (i~=j)
L(i,:)=L(i,:).*(x-x_in(j))/(x_in(i)-x_in(j));
end
end
end
y=0;
for i=1:n
y_out=y_out+x_out(i)*L(i,:);
end
end

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

 採用された回答

Julia
Julia 2014 年 10 月 23 日

0 投票

Hi,
x_in and x_out have the same length (100).
x and y are shorter (52), but I don't know if this bothers your function make_Ln().
I suppose y_out and x_in do not match, but without knowing your function make_Ln() I cannot say more.
To plot x_in and x_out is no problem
plot(x,y,':',x_in,x_out,'*')

7 件のコメント

cakey
cakey 2014 年 10 月 23 日
Here it is
y=lagrange(x,x_in,x_out)
n=size(x_in,2); L=ones(n,size(x,2)); if (size(x_in,2)~=size(x_out,2)) fprintf(1,'x_in and x_out must have same elements'); y=NaN; else for i=1:n for j=1:n if (i~=j) L(i,:)=L(i,:).*(x-x_in(j))/(x_in(i)-x_in(j)); end end end y=0; for i=1:n y=y+x_out(i)*L(i,:); end end
Julia
Julia 2014 年 10 月 23 日
編集済み: Julia 2014 年 10 月 23 日
Your problem is the definition of L. In the last loop you create a vector y with length 52 (but you need 100). This is because of the second dimension of L (which is only 52).
plot(x,y,':',x_in(1:52),y_out,'*');
works. But I don't think you can use that plot ... I don't get much information out of it.
cakey
cakey 2014 年 10 月 23 日
Oh I am sorry I forgot I changed the names here is how it is supposed to look
y_out=make_Ln(x,x_in,x_out)
n=size(x_in,2); L=ones(n,size(x,2)); if (size(x_in,2)~=size(x_out,2)) fprintf(1,'x_in and x_out must have same elements'); y=NaN; else for i=1:n for j=1:n if (i~=j) L(i,:)=L(i,:).*(x-x_in(j))/(x_in(i)-x_in(j)); end end end y=0; for i=1:n y_out=y_out+x_out(i)*L(i,:); end end
cakey
cakey 2014 年 10 月 23 日
Julia, how are you able to tell the forst lengths were 100 and that one was 50? How can I see this?
Julia
Julia 2014 年 10 月 24 日
I guessed that you changed the name, otherwise it would give you a completely different error ;)
How to find the length/size of a matrix or vector:
>> x=-1:50;
>> length(x)
ans =
52
>> [m,n]=size(x)
m =
1
n =
52
Patrik Ek
Patrik Ek 2014 年 10 月 24 日
Cakey i think that you should take a look at the debugger. You can easily set breakpoints in the code and check dimensions, values, etc. It is also possible to set a breakpoint if error. Both under the editor menu under breakpoints, or simply by typing dbstop error. This can be of help in the future and also to make it easier to find solutions of problems.
cakey
cakey 2014 年 11 月 16 日
Thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

質問済み:

2014 年 10 月 23 日

コメント済み:

2014 年 11 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by