使用matlab编写代码要求将x1 x2 x3 x4 y1 y2 y3 y4四组xy散点图分别进行B样条拟合并画出其中每一组x都形如005 0103448 0151515 02 025 每一组y都形如-0447321 -0277143 -0101953 0009087 010446但每一组的值都不一样 并将拟合后的曲线求导后一起画在另一张图里 要求不能使用ppdiff
以下是一份可能的代码实现:
%% 拟合每一组xy散点图 x1 = [0.05, 0.103448, 0.151515, 0.2, 0.25]; y1 = [-0.447321, -0.277143, -0.101953, 0.009087, 0.10446]; x2 = [0.1, 0.2, 0.3, 0.4, 0.5]; y2 = [-0.2, 0.1, 0.3, 0.5, 0.7]; x3 = [0.05, 0.1, 0.15, 0.2, 0.25]; y3 = [0.1, 0.3, 0.5, 0.7, 0.9]; x4 = [0.1, 0.2, 0.3, 0.4, 0.5]; y4 = [0.5, 0.7, 0.9, 1.1, 1.3];
t1 = linspace(0,1,length(x1)); t2 = linspace(0,1,length(x2)); t3 = linspace(0,1,length(x3)); t4 = linspace(0,1,length(x4));
k = 4; % B样条次数 n1 = length(x1)+k-1; % 控制点个数 n2 = length(x2)+k-1; n3 = length(x3)+k-1; n4 = length(x4)+k-1;
% 生成控制点 p1 = bsplinefit(x1,y1,k,t1); p2 = bsplinefit(x2,y2,k,t2); p3 = bsplinefit(x3,y3,k,t3); p4 = bsplinefit(x4,y4,k,t4);
% 生成拟合曲线 xx1 = linspace(min(x1),max(x1),100); yy1 = bsplineval(p1,xx1,k,t1); xx2 = linspace(min(x2),max(x2),100); yy2 = bsplineval(p2,xx2,k,t2); xx3 = linspace(min(x3),max(x3),100); yy3 = bsplineval(p3,xx3,k,t3); xx4 = linspace(min(x4),max(x4),100); yy4 = bsplineval(p4,xx4,k,t4);
% 画出拟合曲线 figure; subplot(2,2,1); plot(x1,y1,'o',xx1,yy1,'-'); title('Group 1'); subplot(2,2,2); plot(x2,y2,'o',xx2,yy2,'-'); title('Group 2'); subplot(2,2,3); plot(x3,y3,'o',xx3,yy3,'-'); title('Group 3'); subplot(2,2,4); plot(x4,y4,'o',xx4,yy4,'-'); title('Group 4');
%% 求导并画出 % 求导方法:将控制点坐标差分并乘以次数再除以区间长度 % 注意:最后一个控制点的导数需要特殊处理 dx1 = k*(p1(2:end,:)-p1(1:end-1,:))./(t1(end)-t1(1)); dx1(end,:) = k*(p1(end,:)-p1(end-1,:))./(t1(end)-t1(end-1)); dy1 = k*(bsplineval(fnder(p1,[1,0]),xx1)-bsplineval(fnder(p1,[1,0]),min(x1)))./(max(x1)-min(x1)); dx2 = k*(p2(2:end,:)-p2(1:end-1,:))./(t2(end)-t2(1)); dx2(end,:) = k*(p2(end,:)-p2(end-1,:))./(t2(end)-t2(end-1)); dy2 = k*(bsplineval(fnder(p2,[1,0]),xx2)-bsplineval(fnder(p2,[1,0]),min(x2)))./(max(x2)-min(x2)); dx3 = k*(p3(2:end,:)-p3(1:end-1,:))./(t3(end)-t3(1)); dx3(end,:) = k*(p3(end,:)-p3(end-1,:))./(t3(end)-t3(end-1)); dy3 = k*(bsplineval(fnder(p3,[1,0]),xx3)-bsplineval(fnder(p3,[1,0]),min(x3)))./(max(x3)-min(x3)); dx4 = k*(p4(2:end,:)-p4(1:end-1,:))./(t4(end)-t4(1)); dx4(end,:) = k*(p4(end,:)-p4(end-1,:))./(t4(end)-t4(end-1)); dy4 = k*(bsplineval(fnder(p4,[1,0]),xx4)-bsplineval(fnder(p4,[1,0]),min(x4)))./(max(x4)-min(x4));
% 画出导数曲线 figure; subplot(2,2,1); plot(xx1,dy1,'-'); title('Group 1'); subplot(2,2,2); plot(xx2,dy2,'-'); title('Group 2'); subplot(2,2,3); plot(xx3,dy3,'-'); title('Group 3'); subplot(2,2,4); plot(xx4,dy4,'-'); title('Group 4');
原文地址: http://www.cveoy.top/t/topic/MBG 著作权归作者所有。请勿转载和采集!