matlab程序中,如何解决矢量长度必须相同的问题

问题代码:

clear all;close all;clc;

x = 0 : 1: 9;

y = sin(x);

n = 2*length(x);

yi = interpft(y, n);

xi = 0 : 0.5 : 10;

hold on ;

plot(x, y ,'ro');

plot(xi, yi, 'b.-');

plot(x, sin(x),'m--');

legend('原始数据','插值后');

 

第一步:计算所有矢量的长度

代码更正如下:

clear all;close all;clc;

x = 0 : 1: 9;

y = sin(x);

n = 2*length(x);

a1=n

yi = interpft(y, n);

a2=n

xi = 0 : 0.5 : 10;

a3 =length(xi)

从结果可以看出,a3=21,问题就出现在这,所以就调整xi的值,使a3=20。

第二步:更改相应矢量长度,使之相等

clear all;close all;clc;

x = 0 : 1: 9;

y = sin(x);

n = 2*length(x);

a1=n

yi = interpft(y, n);

a2=n

xi = 0 : 0.5 : 9.5;

a3 =length(xi)

hold on ;

plot(x, y ,'ro');

plot(xi, yi, 'b.-');

plot(x, sin(x),'m--');

legend('原始数据','插值后');

第三步:把添加用于计算观察矢量长度的的代码删除。

clear all;close all;clc;

x = 0 : 1: 9;

y = sin(x);

n = 2*length(x);

yi = interpft(y, n);

xi = 0 : 0.5 : 9.5;

hold on ;

plot(x, y ,'ro');

plot(xi, yi, 'b.-');

plot(x, sin(x),'m--');

legend('原始数据','插值后');

猜你喜欢

转载自blog.csdn.net/weixin_41649786/article/details/81127178
今日推荐