www.gusucode.com > symbolic 源码程序 matlab案例代码 > symbolic/Ch2CreatePlotsExample.m

    %% Create Plots
%% Plot with Symbolic Plotting Functions 1
% Plot the symbolic expression $\sin(6x)$ by using |fplot|. By default,
% |fplot| uses the range $-5 < x < 5$.
syms x
fplot(sin(6*x))
%% Plot with Symbolic Plotting Functions 2
% abc
%% Plot with Symbolic Plotting Functions 3
% Plot a symbolic expression or function in polar coordinates $r$
% (radius) and $\theta$ (polar angle) by using |ezpolar|. By default, |ezpolar|
% plots a symbolic expression or function over the interval $0 < \theta <
% 2\pi$.
% 
% Plot the symbolic expression $\sin(6t)$ in polar coordinates.
syms t
ezpolar(sin(6*t))
%% Plot with MATLAB Plotting Functions 1
% abc
%% Plot with MATLAB Plotting Functions 2
% abc
%% Plot with MATLAB Plotting Functions 4
% As an alternative to plotting expressions symbolically, you can
% substitute symbolic variables with numeric values by using |subs|. Then, you can use these
% numeric values with plotting functions in MATLAB(TM).
% 
% In the following expressions |u| and |v|, substitute the symbolic
% variables |x| and |y| with the numeric values defined by |meshgrid|.
syms x y
u = sin(x^2 + y^2);
v = cos(x*y);
[X, Y] = meshgrid(-1:.1:1,-1:.1:1);
U = subs(u, [x y], {X,Y});
V = subs(v, [x y], {X,Y});
%% 
% Now, you can plot |U| and |V| by using standard MATLAB plotting
% functions.
% 
% Create a plot of the vector field defined by the functions |U(X,Y)| and
% |V(X,Y)| by using the MATLAB |quiver| function.
quiver(X, Y, U, V)
%% Plot Multiple Symbolic Functions in One Graph
% Plot several functions on one graph by adding the functions sequentially. After plotting the
% first function, add successive functions by using the |hold on| command.
% The |hold on| command keeps the existing plots. Without the |hold
% on| command, each new plot replaces any existing plot.
% After the |hold on| command, each new plot appears on top of existing
% plots. Switch back to the default behavior of replacing plots by using
% the |hold off| command.
% 
% Plot $f = e^x \sin(20x)$ using |fplot|. Show the bounds of $f$ by
% superimposing plots of $e^x$ and $e^{-x}$ as dashed red lines. Set the
% title by using the |DisplayName| property of the object returned by
% |fplot|.
syms x y
f = exp(x)*sin(20*x)
obj = fplot(f,[0 3]);
hold on
fplot(exp(x), [0 3], '--r')
fplot(-exp(x), [0 3], '--r')
title(obj.DisplayName)
hold off
%% Plot Multiple Symbolic Functions in One Figure 1
% Display several functions side-by-side in one figure by dividing the figure
% window into several subplots using |subplot|. The command
% |subplot(m,n,p)| divides the figure into a |m| by |n| matrix of
% subplots and selects the subplot |p|. Display multiple plots in separate  
% subplots by selecting the subplot and using plotting commands. Plotting
% into multiple subplots is useful for side-by-side comparisons of plots.
% 
% Compare plots of $sin((x^2+y^2)/a)$ for $a = 10, 20, 50, 100$ by using
% |subplot| to create side-by-side subplots.
syms x y a
f = sin((x^2 + y^2)/a);

subplot(2, 2, 1)
fsurf(subs(f, a, 10))
title('a = 10')

subplot(2, 2, 2)
fsurf(subs(f, a, 20))
title('a = 20')

subplot(2, 2, 3)
fsurf(subs(f, a, 50))
title('a = 50')

subplot(2, 2, 4)
fsurf(subs(f, a, 100))
title('a = 100')
%% Plot Multiple Symbolic Functions in One Figure 2
close
%% Combine Symbolic Function Plots and Numeric Data Plots 1
% Plot numeric and symbolic data on the same graph by using MATLAB and Symbolic Math
% Toolbox functions together.
% 
% For numeric values of $x$ between $[-5, 5]$, return a noisy sine curve
% by finding $y = \sin(x)$ and adding random values to $y$. View the noisy sine
% curve by using |scatter| to plot the points $(x1, y1), (x2, y2), \cdots$. 
x = linspace(-5,5);
y = sin(x) + (-1).^randi(10, 1, 100).*rand(1, 100)./2;
scatter(x, y)
%% 
% Show the underlying structure in the points by superimposing a plot of
% the sine function. First, use |hold on| to retain the scatter plot. Then, use
% |fplot| to plot the sine function.
hold on
syms t
fplot(sin(t))
hold off
%% Combine Symbolic Function Plots and Numeric Data Plots 3
% Combine symbolic and numeric plots in 3-D by using MATLAB and Symbolic
% Math Toolbox plotting functions. Symbolic Math Toolbox provides these 3-D plotting functions:
% 
% * <docid:symbolic_ug.buzhvpq> creates 3-D parameterized line plots.
% * <docid:symbolic_ug.buzhv1f> creates 3-D surface plots.
% * <docid:symbolic_ug.bu008tv> creates 3-D mesh plots.
% 
% Create a spiral plot by using |fplot3| to plot the parametric line 
% 
% $$\matrix{x = (1-t)\sin(100t) \cr y = (1-t)\cos(100t) \cr z = \sqrt{1 -
% x^2 - y^2}. }$$
% 
syms t
x = (1-t)*sin(100*t);
y = (1-t)*cos(100*t);
z = sqrt(1 - x^2 - y^2);
fplot3(x, y, z, [0 1])
title('Symbolic 3-D Parametric Line')
%% 
% Superimpose a plot of a sphere with radius 1 and center at (0, 0, 0).
% Find points on the sphere numerically by using |sphere|. Plot the sphere by using
% |mesh|. The resulting plot shows the symbolic parametric line wrapped
% around the top hemisphere.
hold on
[X,Y,Z] = sphere;
mesh(X, Y, Z)
colormap(gray)
title('Symbolic Parametric Plot and a Sphere')
hold off