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

2 views (last 30 days)
iicecont
iicecont on 30 Sep 2021
Commented: Alan Stevens on 30 Sep 2021
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

Answers (1)

Alan Stevens
Alan Stevens on 30 Sep 2021
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 Comment
Alan Stevens
Alan Stevens on 30 Sep 2021
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)

Sign in to comment.

Categories

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!