How to plot 2 graphs with input as a range and join them together?

1 次查看(过去 30 天)
Here is the equation I'm trying to write a code which is b-spline curve
Below is the code I've written where p0-p3 is my first set to substitute in equation, and p1-p4 is my second set :
p0=0;
p1=6;
p2=1;
p3=3;
p4=3;
t=[0:0.01:1];
x1 = (1/6)*[((((-t).^3)+(3*(t.^2))-(3*t)+1)*p0)+(((3*(t.^3))-(6*(t.^2))+ 4)*p1)+(((-3*(t.^3)) + (3*(t.^2)) + (3*t) +1)*p2)+((t.^3)*p3)];
x2 = (1/6)*[((((-t).^3)+(3*(t.^2))-(3*t)+1)*p1)+(((3*(t.^3))-(6*(t.^2))+ 4)*p2)+(((-3*(t.^3)) + (3*(t.^2)) + (3*t) +1)*p3)+((t.^3)*p4)];
figure
plot(t,x1)
hold on
plot(t,x2)
I don't know how to join the graph and I'm not sure if my code is correct if i don't use for loop for my input t. Appreciate for any help

回答(1 个)

Alan Stevens
Alan Stevens 2021-9-30
Like this
p0=0;
p1=6;
p2=1;
p3=3;
p4=3;
t=[0:0.01:1];
x1 = (1/6)*[((((-t).^3)+(3*(t.^2))-(3*t)+1)*p0)+(((3*(t.^3))-(6*(t.^2))+ 4)*p1)+(((-3*(t.^3)) + (3*(t.^2)) + (3*t) +1)*p2)+((t.^3)*p3)];
x2 = (1/6)*[((((-t).^3)+(3*(t.^2))-(3*t)+1)*p1)+(((3*(t.^3))-(6*(t.^2))+ 4)*p2)+(((-3*(t.^3)) + (3*(t.^2)) + (3*t) +1)*p3)+((t.^3)*p4)];
figure
plot(t,x1,t+1,x2)
  1 个评论
Alan Stevens
Alan Stevens 2021-9-30
Alternatively, you could use Matlab's polyval function:
c1 = [-1 3 -3 1]/6;
c2 = [3 -6 0 4]/6;
c3 = [-3 3 3 1]/6;
c4 = [1 0 0 0]/6;
x = @(t,p) polyval(c1,t)*p(1) + polyval(c2,t)*p(2) + polyval(c3,t)*p(3) + polyval(c4,t)*p(4);
p = [0 6 1 3 3];
t=0:0.01:1;
x1 = x(t,p(1:4));
x2 = x(t,p(2:5));
plot(t,x1,t+1,x2)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by