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

1 回表示 (過去 30 日間)
iicecont
iicecont 2021 年 9 月 30 日
コメント済み: Alan Stevens 2021 年 9 月 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 Center および File ExchangeSpline Postprocessing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by