How to make a piecewise function?

8 次查看(过去 30 天)
I just want to plot the 3D function that makes a rollercoaster track. I have done it succesfully in 2D in desmos using arcs of circles through both implicit and parametric functions which I have attached.
I can not create a piecewise function that represents what I have in either desmos images.
What should I be putting into MATLAB to get the desmos equivalent? Is my piecewise failing because of the domain restrictions?
I have tried to make a piecewise function for only the x axis. The plan was to make 3 piecewise functions for x y and z, then to plot the 3 piecewise functions. Using the following command:
pwx = piecewise(0<=t<=pi/2,f1x,pi<=t<=3*pi/2,f2x,3*pi/2<=2*pi,f3x,0<=t<=pi,f4x,pi<=t<=3*pi/2,f5x)
where
f1x = @(t) 2*cos(t);
f2x = @(t) cos(t);
f3x = @(t) 2*cos(t)+3;
f4x = @(t) cos(t)+4;
f5x = @(t) 2*cos(t)+5;
and
t = linspace(0,3*pi/2)
I get the error message:
Undefined function 'piecewise' for input arguments of type 'function_handle'.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-14
Piecewise takes a symbolic variable as input. Try
syms t
f1x = 2*cos(t);
f2x = cos(t);
f3x = 2*cos(t)+3;
f4x = cos(t)+4;
f5x = 2*cos(t)+5;
pwx = piecewise(0<=t & t<pi/2,f1x, pi<=t & t<3*pi/2,f2x, 3*pi/2<=t & t<2*pi,f3x, 0<=t & t<pi,f4x, pi<=t & t<3*pi/2, f5x);
fplot(pwx)
For the 2D parametric curve question you posted earlier, you can follow the same method as above and make a piecewise function for y values. However, this method has the disadvantage that all the lines will have the same style. You can try the following code for an alternative way to draw similar plots.
fig = figure();
ax = gca();
hold(ax);
grid on
daspect([1 1 1]);
a = 2;
b = 1;
fplot(@(t) a*cos(t), @(t) a*sin(t), [0, pi/2], 'Color', 'r', 'LineWidth', 1.5);
fplot(@(t) b*cos(t)+3, @(t) b*sin(t), [pi, 3*pi/2], 'Color', 'b', 'LineWidth', 1.5);
fplot(@(t) a*cos(t)+3, @(t) a*sin(t)+1, [3*pi/2, 2*pi], 'Color', 'g', 'LineWidth', 1.5);
fplot(@(t) b*cos(t)+4, @(t) b*sin(t)+1, [0, pi], 'Color', 'k', 'LineStyle', '--', 'LineWidth', 1.5);
fplot(@(t) a*cos(t)+5, @(t) a*sin(t)+1, [pi, 3*pi/2], 'Color', 'k', 'LineWidth', 1.5)
fplot(@(t) t, @(t) -1*ones(size(t)), [5 6], 'Color', 'm', 'LineWidth', 1.5)
  3 个评论
Ameer Hamza
Ameer Hamza 2020-3-14
I guess the problem is happening because two different functions are mapped on the same range, for example, f1y and f4y are mapped between [0, pi/2]. This causes issues for the fplot function. I couldn't find a way without creating two separate piecewise functions.
syms t
a = 2;
b = 1;
f1x = a*cos(t);
f2x = b*cos(t)+3;
f3x = a*cos(t)+3;
f4x = b*cos(t)+4;
f5x = a*cos(t)+5;
f1y = a*sin(t);
f2y = b*sin(t);
f3y = a*sin(t)+1;
f4y = b*sin(t)+1;
f5y = a*sin(t)+1;
ax = axes();
hold(ax);
pwx = piecewise(0<t & t<pi/2,f1x, pi<t & t<3*pi/2,f2x, 3*pi/2<t & t<2*pi,f3x);
pwy = piecewise(0<t & t<pi/2,f1y, pi<t & t<3*pi/2,f2y, 3*pi/2<t & t<2*pi,f3y);
fplot(pwx, pwy, [0 2*pi])
pwx = piecewise(0<t & t<pi,f4x, pi<t & t<3*pi/2, f5x);
pwy = piecewise(0<t & t<pi,f4y, pi<t & t<3*pi/2, f5y);
fplot(pwx, pwy, [0 2*pi])
john warts
john warts 2020-3-15
Thank you very much for your help. It works finally!

请先登录,再进行评论。

更多回答(1 个)

john warts
john warts 2020-3-15
For anyone who wants to know the final script to make the rollercoaster in 3D:
(The figure command is essential)
syms t
a = 2;
b = 1;
f1x = a*cos(t);
f2x = b*cos(t)+3;
f3x = a*cos(t)+3;
f4x = b*cos(t)+4;
f5x = a*cos(t)+5;
f1y = a*sin(t);
f2y = b*sin(t);
f3y = a*sin(t)+1;
f4y = b*sin(t)+1;
f5y = a*sin(t)+1;
f1z = @(t) 0;
f2z = @(t) 0;
f3z = @(t) 0;
f4z = @(t) t;
f5z = @(t) pi;
figure
hold on;
pwx = piecewise(0<t & t<pi/2,f1x, pi<t & t<3*pi/2,f2x, 3*pi/2<t & t<2*pi,f3x);
pwy = piecewise(0<t & t<pi/2,f1y, pi<t & t<3*pi/2,f2y, 3*pi/2<t & t<2*pi,f3y);
pwz = piecewise(0<=t & t<pi/2,f1z, pi<=t & t<3*pi/2,f2z, 3*pi/2<=t & t<2*pi,f3z);
fplot3(pwx, pwy, pwz, [0 2*pi])
pwx = piecewise(0<t & t<pi,f4x, pi<t & t<3*pi/2, f5x);
pwy = piecewise(0<t & t<pi,f4y, pi<t & t<3*pi/2, f5y);
pwz = piecewise(0<t & t<pi,f4z, pi<t & t<3*pi/2, f5z);
fplot3(pwx, pwy, pwz, [0 2*pi])

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by