【问题标题】:I have tried to plot in matlab but matlab plot is not working我试图在 matlab 中绘图,但 matlab 绘图不起作用
【发布时间】:2019-11-16 22:06:45
【问题描述】:

我试图在 Matlab 中绘制代码。但它不起作用。只是数字窗口已经出来,但该窗口上没有显示数字。我现在能做什么? 我从一个名为 Control Bootcamp 的 youtube 播放列表中尝试了这段代码,我的 Matlab 版本是 R2018a

这是主要代码:

clear all, close all, clc

m = 1;
M = 5;
L = 2;
g = -10;
d = 1;

tspan = 0:.1:4;
y0 = [0; 0; pi; .5];
[t,y] = ode45(@(t,y)cartpend(y,m,M,L,g,d,0),tspan,y0); %cartpend is a function

for k=1:length(t)
    drawcartpend_bw(y(k,:),m,M,L);
end

我试图绘制倒立摆的轨迹。所以我已经将函数声明为一个名为cartpend 的函数。 drawcartpend_bw用于绘制cartpend的轨迹。

function dy = cartpend(y,m,M,L,g,d,u)

Sy = sin(y(3));
Cy = cos(y(3));
D = m*L*L*(M+m*(1-Cy^2));

dy(1,1) = y(2);
dy(2,1) = (1/D)*(-m^2*L^2*g*Cy*Sy + m*L^2*(m*L*y(4)^2*Sy - d*y(2))) + m*L*L*(1/D)*u;
dy(3,1) = y(4);
dy(4,1) = (1/D)*((m+M)*m*g*L*Sy - m*L*Cy*(m*L*y(4)^2*Sy - d*y(2))) - m*L*Cy*(1/D)*u +.01*randn;

这是cartpend 函数。我在这里宣布了倒立摆方程。

function drawcartpend_bw(y,m,M,L)
x = y(1);
th = y(3);

% kinematics
% x = 3;        % cart position
% th = 3*pi/2;   % pendulum angle

% dimensions
% L = 2;  % pendulum length
W = 1*sqrt(M/5);  % cart width
H = .5*sqrt(M/5); % cart height
wr = .2; % wheel radius
mr = .3*sqrt(m); % mass radius

% positions
% y = wr/2; % cart vertical position
y = wr/2+H/2; % cart vertical position
w1x = x-.9*W/2;
w1y = 0;
w2x = x+.9*W/2-wr;
w2y = 0;

px = x + L*sin(th);
py = y - L*cos(th);

plot([-10 10],[0 0],'w','LineWidth',2)
hold on
rectangle('Position',[x-W/2,y-H/2,W,H],'Curvature',.1,'FaceColor',[1 0.1 0.1],'EdgeColor',[1 1 1])
rectangle('Position',[w1x,w1y,wr,wr],'Curvature',1,'FaceColor',[1 1 1],'EdgeColor',[1 1 1])
rectangle('Position',[w2x,w2y,wr,wr],'Curvature',1,'FaceColor',[1 1 1],'EdgeColor',[1 1 1])

plot([x px],[y py],'w','LineWidth',2)

rectangle('Position',[px-mr/2,py-mr/2,mr,mr],'Curvature',1,'FaceColor',[.3 0.3 1],'EdgeColor',[1 1 1])

% set(gca,'YTick',[])
% set(gca,'XTick',[])
xlim([-5 5]);
ylim([-2 2.5]);
set(gca,'Color','k','XColor','w','YColor','w')
set(gcf,'Position',[10 900 800 400])
set(gcf,'Color','k')
set(gcf,'InvertHardcopy','off')   

% box off
drawnow
hold off

在这里,我尝试绘制函数。但是绘图不起作用。我不知道为什么。

【问题讨论】:

  • 删除所有修改图表的xlimylimset调用。那你应该看到你的身影了。将这些调用一一添加回来,看看是哪一个导致了问题。
  • 问题很简单。只需删除以下行,因为它会使图形显示在您的屏幕尺寸之外,因此您看不到输出。 set(gcf,'Position',[10 900 800 400])
  • 如果@Mohammadnagdawi 是正确的,您应该会在任务栏中看到该图的另一个窗口打开,但选择它或悬停它不会显示任何内容

标签: matlab plot matlab-figure


【解决方案1】:

我认为这可能是 matlab 版本之间的兼容性问题,因为我在 Matlab R2016a 中尝试了此代码并且它正在工作。此外,来自 youtube 的人正在使用 Matlab 2015。如果您没有收到任何错误/警告消息,您可以做的是用谷歌搜索从 Matlab 2015 到 2018 的更改1,并尝试查看您的身份在您的代码中使用已更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2012-05-02
    • 2014-11-12
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多