The fplot
family accepts symbolic expressions and equations as inputs enabling easy analytical plotting without explicitly generating numerical data.
This example features the following functions
fplot
fplot3
fsurf
fcontour
fmesh
fimplicit
fimplicit3
syms x
fplot(sin(exp(x)))
fplot([sin(x),cos(x),tan(x)])
syms x y r = 1:10; fimplicit(x^2+y^2 == r)
subs
syms x a expression = sin(exp(x/a))
expression =
fplot(subs(expression,a,[1,2,4])) hold off legend show
Explore the spline approximation to
syms f(x) f(x) = x*exp(-x)*sin(5*x) -2; xs = 0:1/3:3; ys = double(subs(f,xs)); fplot(f,[0,3]) hold on plot(xs,ys,'*k','DisplayName','Data Points') fplot(@(x) spline(xs,ys,x),[0 3],'DisplayName','Spline interpolant') hold off grid on legend show
syms x n = 5; approx = cumsum(sin((1:2:2*n-1)*x)./(1:2:2*n-1)); fplot(approx,'LineWidth',1)
With symbolic input, we can perform computations and plot the results.
syms f(a,x)
assume(a>0);
f(a,x) = a*x^2+a^2*x+2*sqrt(a)
f(a, x) =
x_min = solve(diff(f,x), x)
x_min =
fplot(f(a,x_min),[0 5]) xlabel 'a' title 'Minimum value of f depending on a'
assume(a,'clear')
syms x t6 = taylor(cos(x),x,'Order',6)
t6 =
t8 = taylor(cos(x),x,'Order',8)
t8 =
fplot([cos(x) t6 t8]) xlim([-4 4]) ylim([-1.5, 1.5]) title '6th and 8th Order Taylor Series approximations to cos(x)' legend show
Some symbolic expressions cannot be converted to MATLAB functions.
syms x
f = x^x
f =
int(f,x)
ans =
diff(f,x)
ans =
fplot([f, int(f,x), diff(f,x)],[0 2])
legend show
Curves or can be drawn by fplot
or fplot3
(just like with plot
or plot3
for numerical data) :
syms t fplot3(sin(t)-t/2,cos(t),t^3,'--','LineWidth',2.5) view([-45 45])
syms x y fsurf(sin(x)+sin(y)-(x^2+y^2)/20,'ShowContours','on') set(camlight,'Color',[0.5 0.5 1]); set(camlight('left'),'Color', [1 0.6 0.6]); set(camlight('left'),'Color', [1 0.6 0.6]); set(camlight('right'),'Color', [0.8 0.8 0.6]); material shiny view(-19,56)
meshgrid
syms x y u = diff(diff(sin(x^2+y^2)))
u =
v = diff(diff(cos(x^2+y^2)))
v =
[X, Y] = meshgrid(-3:.1:3,-2:.1:2); U = subs(u, [x y], {X,Y}); V = subs(v, [x y], {X,Y}); startx = -3:0.1:3; starty = zeros(size(startx)); h = streamline(X,Y,U,V,X,Y); for i=1:length(h)-1 h(i).Color = [rand() rand() rand()]; end
Like fplot
, fsurf
evaluates your symbolic expression more densely where needed, to more accurately show curved areas and asymptotic regions.
fsurf(log(x) + exp(y), [-2 2])
Plot the implicit surface . Specify an output to make fimplicit3 return the plot object.
syms x y z f = 1/x^2 - 1/y^2 + 1/z^2; fimplicit3(f)
Unlike pure symbolic functions (like int
, diff
, solve
), fsurf
does not allow specifying the order of variables. To set the order, use symbolic functions:
syms f(t) x(u,v) y(u,v) z(u,v) f(t) = sin(t)*exp(-t^2/3)+1.5; x(u,v) = u
x(u, v) =
y(u,v) = f(u)*sin(v)
y(u, v) =
z(u,v) = f(u)*cos(v)
z(u, v) =
fsurf(x,y,z,[-5 5.1 0 2*pi])
fmesh
for 3D mesh plotsPlot the parameterized mesh
where
syms s t r = 8 + sin(7*s + 5*t); x = r*cos(s)*sin(t); y = r*sin(s)*sin(t); z = r*cos(t); fmesh(x, y, z, [0 2*pi 0 pi], 'Linewidth', 2) axis equal
syms x y g(x,y) g(x,y) = x^3-4*x-y^2
g(x, y) =
fcontour(g,[-3 3 -4 4],'LevelList',-6:6) title 'Some Elliptic Curves'