From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: code operating  speed is very slow
Date: Thu, 13 May 2010 12:07:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 25
Message-ID: <hsgq18$743$>
References: <hsgbs1$nvf$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1273752424 7299 (13 May 2010 12:07:04 GMT)
NNTP-Posting-Date: Thu, 13 May 2010 12:07:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:635420

"Wenxu Li" <> wrote in message <hsgbs1$nvf$>...
> I try to simulate a one DoF mass-spring system,the code is as follows:
> %M02, solution
> tspan=0:0.06:10*pi;
> x0=[0;5.0];
> [t,x]=ode23w('friction_force',tspan,x0);
> plot(t,x(:,1));
> %function 
> function y=friction_force(t,x)
> m=1;
> k=1;
> Frictionforce=10;
> y=zeros(2,1);
> y(1)=x(2);
> y(2)=-Frictionforce*sign(x(2))/m-k/m*x(1);
> My question is when I use y(2)=Frictionforce*sign(x(2))/m-k/m*x(1), i.e. without minus
> before Frictionforce, the code operates very quickly, while I add a minus, it is very slow, more than half an hour to get the results. Can anybody tell what is the matter?

  The dynamics of the system will be entirely different with and without that minus sign.  With the minus sign present, representing an actual retarding friction force, the mass can get stuck in a situation where the velocity is zero and the frictional force alternates back and forth rapidly.  Such a discontinuity would slow down the functioning of the algorithm greatly since it is designed to handle continuous functions.

  Without the minus sign the oscillations back and forth take on wider and wider swings so that there is no stuck-at-zero-velocity situation occurring.  Hence the discontinuity has far less effect on the speed of the algorithm.  However, it is not a realistic way for friction to behave.

Roger Stafford