use*_*423 20 matlab matlab-figure
我有以下代码,我想将相空间图组合成一个单独的图.
我编写了函数,但我不知道如何让MATLAB将它们放到一个图中.正如你看到的,它是变量r
,a
,b
,和d
的改变.我如何组合它们?
我还想使用quiver命令绘制这些相空间图的矢量场,但它不起作用.
%function lotkavolterra
% Plots time series and phase space diagrams.
clear all; close all;
t0 = 0;
tf = 20;
N0 = 20;
P0 = 5;
% Original plot
r = 2;
a = 1;
b = 0.2;
d = 1.5;
% Time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
quiver(N,P);
axis([0 50 0 10])
%axis tight
% Change variables
r = 2;
a = 1.5;
b = 0.1;
d = 1.5;
%time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
plot(N,P);
axis([0 50 0 10])
% Change variables
r = 2;
a = 1;
b = 0.2;
d = 0.5;
% Time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
plot(N,P);
axis([0 50 0 10])
% Change variables
r = 0.5;
a = 1;
b = 0.2;
d = 1.5;
% Time series plots
lv = @(t,x)(lv_eq(t,x,r,a,b,d));
[t,NP] = ode45(lv,[t0,tf],[N0 P0]);
N = NP(:,1); P = NP(:,2);
figure
plot(t,N,t,P,' --');
axis([0 20 0 50])
xlabel('Time')
ylabel('predator-prey')
title(['r=',num2str(r),', a=',num2str(a),', b=',num2str(b),', d=',num2str(d)]);
saveas(gcf,'predator-prey.png')
legend('prey','predator')
% Phase space plot
figure
plot(N,P);
axis([0 50 0 10])
% FUNCTION being called from external .m file
%function dx = lv_eq(t,x,r,a,b,d)
%N = x(1);
%P = x(2);
%dN = r*N-a*P*N;
%dP = b*a*P*N-d*P;
%dx = [dN;dP];
Run Code Online (Sandbox Code Playgroud)
Ego*_*gon 29
那么,有多种方法可以在同一图中显示多个数据系列.
我将使用一些示例数据集以及相应的颜色:
%% Data
t = 0:100;
f1 = 0.3;
f2 = 0.07;
u1 = sin(f1*t); cu1 = 'r'; %red
u2 = cos(f2*t); cu2 = 'b'; %blue
v1 = 5*u1.^2; cv1 = 'm'; %magenta
v2 = 5*u2.^2; cv2 = 'c'; %cyan
Run Code Online (Sandbox Code Playgroud)
首先,当你想要在同一轴上的所有东西时,你将需要这个hold
功能:
%% Method 1 (hold on)
figure;
plot(t, u1, 'Color', cu1, 'DisplayName', 'u1'); hold on;
plot(t, u2, 'Color', cu2, 'DisplayName', 'u2');
plot(t, v1, 'Color', cv1, 'DisplayName', 'v1');
plot(t, v2, 'Color', cv2, 'DisplayName', 'v2'); hold off;
xlabel('Time t [s]');
ylabel('u [some unit] and v [some unit^2]');
legend('show');
Run Code Online (Sandbox Code Playgroud)
在许多情况下,您会发现这是正确的,但是,当两个数量的动态范围相差很大时(例如,u
值小于1,而v
值大得多),它会变得很麻烦.
其次,当您拥有大量数据或不同数量时,也可以使用subplot
不同的轴.我还使用该功能linkaxes
在x方向上链接轴.当您在MATLAB中放大其中任何一个时,另一个将显示相同的x范围,这样可以更轻松地检查更大的数据集.
%% Method 2 (subplots)
figure;
h(1) = subplot(2,1,1); % upper plot
plot(t, u1, 'Color', cu1, 'DisplayName', 'u1'); hold on;
plot(t, u2, 'Color', cu2, 'DisplayName', 'u2'); hold off;
xlabel('Time t [s]');
ylabel('u [some unit]');
legend(gca,'show');
h(2) = subplot(2,1,2); % lower plot
plot(t, v1, 'Color', cv1, 'DisplayName', 'v1'); hold on;
plot(t, v2, 'Color', cv2, 'DisplayName', 'v2'); hold off;
xlabel('Time t [s]');
ylabel('v [some unit^2]');
legend('show');
linkaxes(h,'x'); % link the axes in x direction (just for convenience)
Run Code Online (Sandbox Code Playgroud)
子图确实浪费了一些空间,但它们允许将一些数据保存在一起而不会过多地填充图表.
最后,作为一个更复杂的方法的例子,使用plotyy
函数在同一个数字上绘制不同的数量(或更好的是:yyaxis
自R2016a以来的功能)
%% Method 3 (plotyy)
figure;
[ax, h1, h2] = plotyy(t,u1,t,v1);
set(h1, 'Color', cu1, 'DisplayName', 'u1');
set(h2, 'Color', cv1, 'DisplayName', 'v1');
hold(ax(1),'on');
hold(ax(2),'on');
plot(ax(1), t, u2, 'Color', cu2, 'DisplayName', 'u2');
plot(ax(2), t, v2, 'Color', cv2, 'DisplayName', 'v2');
xlabel('Time t [s]');
ylabel(ax(1),'u [some unit]');
ylabel(ax(2),'v [some unit^2]');
legend('show');
Run Code Online (Sandbox Code Playgroud)
这当然看起来很拥挤,但是当信号的动态范围存在很大差异时它会派上用场.
当然,没有什么阻碍了你使用这些技术的结合在了一起:hold on
一起plotyy
和subplot
.
编辑:
因为quiver
,我很少使用那个命令,但无论如何,你很幸运我一段时间写了一些代码以便于矢量场图.您可以使用与上述相同的技术.我的代码远非严格,但这里有:
function [u,v] = plotode(func,x,t,style)
% [u,v] = PLOTODE(func,x,t,[style])
% plots the slope lines ODE defined in func(x,t)
% for the vectors x and t
% An optional plot style can be given (default is '.b')
if nargin < 4
style = '.b';
end;
% http://ncampbellmth212s09.wordpress.com/2009/02/09/first-block/
[t,x] = meshgrid(t,x);
v = func(x,t);
u = ones(size(v));
dw = sqrt(v.^2 + u.^2);
quiver(t,x,u./dw,v./dw,0.5,style);
xlabel('t'); ylabel('x');
Run Code Online (Sandbox Code Playgroud)
当被称为:
logistic = @(x,t)(x.* ( 1-x )); % xdot = f(x,t)
t0 = linspace(0,10,20);
x0 = linspace(0,2,11);
plotode(@logistic,x0,t0,'r');
Run Code Online (Sandbox Code Playgroud)
这会产生:
如果您需要更多指导,我发现源代码中的链接非常有用(尽管格式错误).
另外,您可能想看看MATLAB的帮助,它真的很棒.只需输入help quiver
或doc quiver
输入MATLAB或使用我上面提供的链接(这些链接应该提供相同的内容doc
).