Problem with ode45

When I try and call my function "vector" I get the error "Output argument "dy" (and maybe others) not assigned during call to "vector"."
I am trying to define positive and negative values for dy within my function
my function is :
function dy = vector(t,y)
global F w k1 A c m v n
dyplus(1)=y(2);
dyplus(2)=1/m*(F*cos(w*t)-k1*y(1)-c*y(2)-A);
dyminus(1)=y(2);
dyminus(2)=1/m*(F*cos(w*t)-k1*y(1)-c*y(2)-A);
n=1e-05; % small stick band
v=1e-05;
h=y(2)-v; % wrel
N=[0;1]; % normal to switching boundary
tau=1e-05; % time constant
if abs(h)>n
if h>n
dy= dyplus;
else
dy=dymin;
end;
else
if N.*[dyplus]'>0 & N.*[dymin]'>0 ;
dy=dyplus;
end
if N.*[dyplus]'<0 & N.*[dymin]'<0 ;
dy=dymin;
end
if N.*[dyplus]'<0 & N.*[dymin]'>0 ;
alpha=(N*[dymin]'+tau^-1*h)/(N*[(dymin)-dyplus]');
dy=alpha*dyplus+(1-alpha)*dymin;
end
if N.*[dyplus]'>0 & N.*[dymin]'<0 ;
dy=dyplus;
end
end
and the main code is:
clc
clear all
global F w k1 A c m v n
w=50;
F=100;
A=1;
c=10;
m=10;
k1=10;
n=1e-05; % small stick band
v=1e-05;
options = odeset('Events',@eventsfn,'RelTol',1e-5,'AbsTol',[1e-5 1e-5]);
tspan=[0:0.0001:1];
y0=[0 0];
[t,y]=ode45(@vector,tspan,y0,options);
figure
plot(t,y(:,1),'r.',t,y(:,2),'b.')
legend('displacement','velocity')
xlabel('time sec')
ylabel('responses')

回答(1 个)

Jan
Jan 2017-3-7
编辑:Jan 2017-3-7

0 个投票

Matlab's ODE integrators are not designed to handle functions with discontinuities. If you are lucky, the integration stops with an error, but without luck you can an inaccurate solution. The step size control runs crazy, and you either get steps which use values from different intervals, or the step size is reduced until the round off errors dominate the integration. See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047 .
The error message is clear: In some cases the output variable "dy" is not defined. This happens, when:
  1. abs(h) <= n
  2. N.*dyplus' == 0 or N.*dymin' == 0
While including the == 0 cases solves this error message, the numerical issue will remain. The only valid approach is to use an event function to stop the integration, change the parameters to control the state and restart the integration using the final values of the former step as initial values to the current step.
See this thread for the reasons to avoid unnecessary square brackets.

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by