问一个关于matlab的问题
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/20 11:59:28
问一个关于matlab的问题
x=[7.0,10.5,13.0,17.5,34.0,40.5,44.5,48.0,56.0,61.0,68.5,76.5,80.5,91.0,96.0,101.0,104.0,106.5,111.5,118.0,123.5,136.5,142.0,146.0,150.0,157.0,158.0];
y1=[44,45,47,50,50,38,30,30,34,36,34,41,45,46,43,37,33,28,32,65,55,54,52,50,66,66,68];
y2=[44,59,70,72,93,100,110,110,110,117,118,116,118,118,121,124,121,121,121,122,116,83,81,82,86,85,68];
xx=7:0.1:158;
yy1=spline(x,y1,xx);
yy2=spline(x,y2,xx)
plot(xx,yy1,'g',xx,yy2,'b')
s1=trapz(yy1,xx)
s2=trapz(yy2,xx)
s=s1-s2
为什么计算出的s2会是负的呢?
x=[7.0,10.5,13.0,17.5,34.0,40.5,44.5,48.0,56.0,61.0,68.5,76.5,80.5,91.0,96.0,101.0,104.0,106.5,111.5,118.0,123.5,136.5,142.0,146.0,150.0,157.0,158.0];
y1=[44,45,47,50,50,38,30,30,34,36,34,41,45,46,43,37,33,28,32,65,55,54,52,50,66,66,68];
y2=[44,59,70,72,93,100,110,110,110,117,118,116,118,118,121,124,121,121,121,122,116,83,81,82,86,85,68];
xx=7:0.1:158;
yy1=spline(x,y1,xx);
yy2=spline(x,y2,xx)
plot(xx,yy1,'g',xx,yy2,'b')
s1=trapz(yy1,xx)
s2=trapz(yy2,xx)
s=s1-s2
为什么计算出的s2会是负的呢?
怀疑你把trapz的参数顺序写错了,本来应该是:s1=trapz(xx,yy1)
s2=trapz(xx,yy2)按照你的写法,横轴是yy1或yy2,积分的时候大致上按照横坐标的增量乘以纵坐标的值,yy2对于较小的xx主要是增大,而对于较大的xx则主要是减小(相当于增量为负),导致最终的和为负.
s2=trapz(xx,yy2)按照你的写法,横轴是yy1或yy2,积分的时候大致上按照横坐标的增量乘以纵坐标的值,yy2对于较小的xx主要是增大,而对于较大的xx则主要是减小(相当于增量为负),导致最终的和为负.