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

Thread Subject:
code operating speed is very slow

Subject: code operating speed is very slow

From: Wenxu Li

Date: 13 May, 2010 08:05:21

Message: 1 of 2

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?

Subject: code operating speed is very slow

From: Roger Stafford

Date: 13 May, 2010 12:07:04

Message: 2 of 2

"Wenxu Li" <lwxlee@gmail.com> wrote in message <hsgbs1$nvf$1@fred.mathworks.com>...
> 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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us