我曾尝试在matlab中进行绘图,但matlab绘图无法正常工作

问题描述 投票:0回答:1

我试图在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

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

matlab plot matlab-figure
1个回答
0
投票

我认为这可能是matlab版本之间的兼容性问题,因为我在Matlab R2016a中尝试了此代码,并且可以正常工作。另外,来自youtube的那个人正在使用Matlab2015。如果您没有收到任何错误/警告消息,该怎么办,就是要搜索从Matlab 2015到2018 1的更改,然后尝试查看您的内容是否有用您的代码中使用的已更改。

© www.soinside.com 2019 - 2024. All rights reserved.