Error using plot Vectors must be the same length.

So I've got an error in plot:
Error using plot
Vectors must be the same length.
Error in lesson5_1 (line 25)
plot(X,Y,'*k',xi,zi);grid
% Построение графика
Here is the code;
X=[1910 1920 1930 1940 1950 1960 1970 1980 1990]-1909; %госдолг сша
Y=[120.7 26.0 162.0 50.7 256.9 290.5 380.9 90.9 20.6];
xzv=1955-1909;
xi=min(X):0.05:max(X); % Вспомогательный вектор абсциссы
yi=spline(X,Y,xi);
AS=[2 1 0 0 0 0 0 0 0;
1 4 1 0 0 0 0 0 0;
0 1 4 1 0 0 0 0 0;
0 0 1 4 1 0 0 0 0;
0 0 0 1 4 1 0 0 0;
0 0 0 0 1 4 1 0 0;
0 0 0 0 0 1 4 1 0;
0 0 0 0 0 0 1 4 1;
0 0 0 0 0 0 0 1 2];
BS=3.*[26.0-120.7;162.0-120.7;50.7-26.0;
256.9-162.0;290.5-50.7;380.9-256.9;
90.9-290.5;20.6-380.9;20.6-90.9];
S=AS\BS;
dx=(max(X)-min(X))/50; % Вычисление шага
for i=(1:50),
xi(i)=min(X)+dx*i; % Вспомогательные точки абсциссы
zi(i)=form2(X,Y,S,xi(i)); % Значение S3 в точке xi(i)
end;
plot(X,Y,'*k',xi,zi);grid % Построение графика
And here is the function form2:
function y=form2(X,Y,S,z);
i=max(find(X<z));
h=X(i+1)-X(i);
p=(X(i+1)-z)/h;
q=(z-X(i))/h;
y=p^2*(2*q+1)*Y(i)+p^2*q*S(i)+...
q^2*(2*p+1)*Y(i+1)-q^2*p*S(i+1);
What could be the problem?

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 28 日

0 投票

No Error
678.png

6 件のコメント

Valeria Pinus
Valeria Pinus 2019 年 10 月 28 日
編集済み: Valeria Pinus 2019 年 10 月 28 日
that's the first plot, i'm asking about the second (edited the code, now there won't be misunderstanding)
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 28 日
編集済み: KALYAN ACHARJYA 2019 年 10 月 28 日
To plot the two vectors must be same length , see xi and zi, both are different length
>> whos xi
Name Size Bytes Class Attributes
xi 1x1601 12808 double
>> whos zi
Name Size Bytes Class Attributes
zi 1x50 400 double
You have defined xi varible in two places
xi=min(X):0.05:max(X);
Other
within for loop
Valeria Pinus
Valeria Pinus 2019 年 10 月 28 日
I understand that, but I don't know why these vectors have different length and how to fix it.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 28 日
編集済み: KALYAN ACHARJYA 2019 年 10 月 28 日
Here I have changed the later xi variable name to xi_new
X=[1910 1920 1930 1940 1950 1960 1970 1980 1990]-1909; %госдолг сша
Y=[120.7 26.0 162.0 50.7 256.9 290.5 380.9 90.9 20.6];
xzv=1955-1909;
xi=min(X):0.05:max(X); % Вспомогательный вектор абсциссы
yi=spline(X,Y,xi);
AS=[2 1 0 0 0 0 0 0 0;
1 4 1 0 0 0 0 0 0;
0 1 4 1 0 0 0 0 0;
0 0 1 4 1 0 0 0 0;
0 0 0 1 4 1 0 0 0;
0 0 0 0 1 4 1 0 0;
0 0 0 0 0 1 4 1 0;
0 0 0 0 0 0 1 4 1;
0 0 0 0 0 0 0 1 2];
BS=3.*[26.0-120.7;162.0-120.7;50.7-26.0;
256.9-162.0;290.5-50.7;380.9-256.9;
90.9-290.5;20.6-380.9;20.6-90.9];
S=AS\BS;
dx=(max(X)-min(X))/50; % Вычисление шага
for i=(1:50)
xi_new(i)=min(X)+dx*i; % Вспомогательные точки абсциссы
zi(i)=form2(X,Y,S,xi_new(i)); % Значение S3 в точке xi(i)
end
plot(X,Y,'*k',xi_new,zi,'r');grid % Построение графика
99.png
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 28 日
編集済み: KALYAN ACHARJYA 2019 年 10 月 28 日
The reason, the later xi just replaced the previous 50 elements of the initial xi vector and maintained the same length.
Lets example you defined xi as following
xi=0:1:10 % Length eleven
for i=1:5
xi(i)=i*3;
y(i)=..
end
In the second loop, it replace the initial 5 elements of the xi, but it maintained the initial length of xi, and y reflects the length of equal to the loop iteration. So you cant plot xi and yi
Always assign the new variable name to avoid such clashing the variables name, if memory allows.
Hope it helps
Valeria Pinus
Valeria Pinus 2019 年 10 月 28 日
now it works, thank you!

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2019a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by