Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

graphic simulation on simulink

Asked by Hana on 15 Jul 2012

from my previous question http://www.mathworks.ch/matlabcentral/answers/43243-3d-simulink-animation-ball-on-surface

i found a example from %AHMADREZA ESHGHINEJAD (<mailto:ahmadreza.eshghinejad@gmail.com ahmadreza.eshghinejad@gmail.com>) of a bouncing ball on a sinusoidal surface and tried to modify it to fit my model.

but matlab shows an error while calling the "graphicfunction" without any further information. i've been trying to figure out for hours trying to fix it, but it still does not work.

i would really appreciate your help.

main

 %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')

matlab function inside the simulink model

 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));

graphic function

 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;
y2l1=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

0 Comments

Hana

1 Answer

Answer by Paul Metcalf on 16 Jul 2012

Type 'ver' at the command line. Do you have 'Simulink 3D Animation' installed? If not, therein lies your problem.

1 Comment

Hana on 26 Jul 2012

thanks for your reply! it's installed. i still couldn't find the mistake and it still won't work.

Paul Metcalf

Contact us