我从%AHMADREZA ESHGHINEJAD()中找到了一个在正弦曲面上弹跳球的例子,并尝试修改它以适合我的模型。
但matlab在调用“graphicfunction”时显示错误而没有任何进一步的信息。我一直试图弄清楚几个小时试图解决它,但它仍然无效。
我真的很感谢你的帮助。
主
%This set of files including (Rmain.m, RCalc.m, RGraficfunction.m and Rod.mdl) are written by:
%AHMADREZA ESHGHINEJAD (ahmadreza.eshghinejad@gmail.com), April 2011
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% to simulate and animation of a rod falling on a flat surface.
%
%clear
%clc
%global dblcart1 pa dt amp freq I m floormin floorwidth s R
spacing_width=0.001; %[m]
spacing_amplitude=0.008; %[m]
%disc diameter
d=0.01; %[m]
r=d/2; %Radius
x0=0; %% initial x
y0=spacing_amplitude; %% initial y
Bth0=0; %% initial angular position
pa=.001; %% pause in simulation step
T=30; %% simulation time
m=1; %% ball mass
dt=.01; % time steps
s=0; % Movement of frame counter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
animinit('disc on surface');
dblcart1 = findobj('Type','figure','Name','dblcart1 Animation');
hold on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Initializing
% Initial Ball points
th=0:.1:2*pi;
xx=r*cos(th)+x0;
yy=r*sin(th)+y0;
% Initial Ball line points
x1l1=1*r*cos(Bth0)+x0;
x1l2=1*r*cos(Bth0+pi)+x0;
y1l1=1*r*sin(Bth0)+y0;
y1l2=1*r*sin(Bth0+pi)+y0;
x2l1=1*r*cos(Bth0+pi/2)+x0;
x2l2=1*r*cos(Bth0+pi+pi/2)+x0;
y2l1=1*r*sin(Bth0+pi/2)+y0;
y2l2=1*r*sin(Bth0+pi+pi/2)+y0;
% Drawing the floor
floormin=0;
floorwidth=3*2*pi*spacing_width;
floorheight=floorwidth;
axis([floormin floormin+floorwidth 0 floorheight]);
plot(floormin:.1:floormin+floorwidth,spacing_amplitude*sin((1/spacing_width)* (floormin:.1:floormin+floorwidth))+spacing_amplitude,'yellow','LineWidth',2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initial drawing of ball
Ballhndl1=plot([x1l1 x1l2],[y1l1 y1l2],'red');
Ballhndl2=plot([x2l1 x2l2],[y2l1 y2l2],'red');
Ballhndl=plot(xx,yy,'blue','EraseMode','background');
set(gca,'UserData',[Ballhndl;Ballhndl1;Ballhndl2]);
%set(gca,'UserData',Ballhndl);
sim('simmod.mdl')
simulink模型中的matlab函数
function [x,y,Bth] = fcn(ures,yres)
r=0.01; %% Ball radius
spacing_width=0.001; %[m]
spacing_amplitude=0.008; %[m]
for th=2*pi:-.01:0
x=ures+r*cos(th); %% bal points
y=yres+r*sin(th);
end;
Bth=atan(2*pi*(1/spacing_width)*spacing_amplitude*cos(2*pi*(1/spacing_width)*x));
图形功能
function GraficFunction(x,y,Bth)
global Ball
spacing_width=0.001; %[m]
pa=.01; %% pause in simulation step
s=0; % Movement of frame counter
floormin=0;
floorwidth=3*2*pi*spacing_width;
pause(pa)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% passing the window margins and moving the object based on the "s"
%%%%% parameter
if x<(floormin+s*floorwidth),s=s-1;
elseif x>((s+1)*floorwidth),s=s+1;end
x=x-s*floorwidth;
set(0,'currentfigure',Ball)
Ballhndl=get(gca,'UserData');
%Ball Circle points
th=0:.1:2*pi;
x1=ures+r*cos(th);
y1=yres+r*sin(th);
%%%%%%%%%%%%%% Ball line points
x1l1=1*r*cos(Bth)+x;
x1l2=1*r*cos(Bth+pi)+x;
y1l1=1*r*sin(Bth)+y;
y1l2=1*r*sin(Bth+pi)+y;
x2l1=1*r*cos(Bth+pi/2)+x;
x2l2=1*r*cos(Bth+pi+pi/2)+x;
y211=1*r*sin(Bth+pi/2)+y;
y2l2=1*r*sin(Bth+pi+pi/2)+y;
% Draw Lines
set(Ballhndl(1),'XData',x1);
set(Ballhndl(1),'YData',y1);
set(Ballhndl,'XData',x1);
set(Ballhndl,'YData',y1);
set(Ballhndl(2),'XData',[x1l1 x1l2]);
set(Ballhndl(2),'YData',[y1l1 y1l2]);
set(Ballhndl(3),'XData',[x2l1 x2l2]);
set(Ballhndl(3),'YData',[y2l1 y2l2]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
drawnow;
end