error calling a function
2 次查看(过去 30 天)
显示 更早的评论
function f = Mu(t,y,ti,tf,n)
m1 = 55;
m2 = 400;
m3 = 100;
k1 = 230000;
k2 = 30000;
k3 = 50000;
k4 = 3000;
n2 = 1500;
n3 = 4000;
n4 = 700;
tm = (ti+tf)/2;
t1 = tf/4;
L0 = 5;
v = 15;
A = 0.03;
M =[0 1 0 0 0 0;
-(k1+k2)/m1 -(n2+n4)/m1 k2/m1 n4/m1 0 n4/m1;
0 0 0 1 0 0;
k2/m2 n2/m2 -(k3+k2)/m2 -(n3+n2)/m2 k3/m2 n3/m2;
0 0 0 0 0 1;
k4/m3 n4/m3 k3/m3 n3/m3 -(k3+k4)/m3 -(n3+n4)/m3];
switch n
case 1
u = (A/2)*(1-cos(2*pi*v*t/L0));
case 2
u = 1*(t<tm)+(-1)*(t>=tm);
case 3
u = 0*(t<tm)+1*(t>=tm);
case 4
u = 1*(t<tm)+0*(t>=tm);
case 5
u = (A*t)*(t<=tm)+0*(t>tm);
case 6
u1 = A*t;
u2 = A*(t-2*tm);
u = u1.*(t<=tm)+u2.*(t>tm);
case 7
u1 = A*t;
u2 = A*(-t+4*t1);
u3 = A*(-t+3*t1);
u = u1.*(t<=t1)+u2.*((t>=t1)&(t<=2*t1)) +u3.*(t>=2*t1);
case 8
u = A.*((t>=t1)&(t<=2*t1));
vect=[0;-k1*u/m1;0;0;0;0];
f = M*y+vect;
end
here is the main file where i'm trying to call the above function
% initial conditions
IC=[0 0 0 0 0 0];
%initial time
ti=0;
%final time
tf=20;
%interval where the problems can be solved
tspan=[ti,tf];
%function Matrix*Y+u(t)
fMu=@(t,y) Mu(t,y,ti,tf,1);
[t,y]=ode45(fMu,tspan,IC)
The error I'm receiving says "Output argument "f" (and maybe others) not assigned during call to "Mu"." I'm not sure what that means or how to fix it. Please help
2 个评论
Jan
2018-5-13
From the view point of numerical maths it is a professional blunder to use ODE45 to integrate a non-smooth function. See http://www.mathworks.com/matlabcentral/answers/59582#answer_72047. It is like using a drilling machine to beat a screw into the wall. Of course, it is in afterwards and you get a final number. But you have driven the tool against the specifications and have no control if the result is dominated by rounding errors.
The stable way is to run the integration in steps over the smooth intervals and to use the final value of one step as initial value of the next one.
采纳的回答
Stephan
2018-5-13
Hi Susan,
you should finish your switch case command with an end command.
function f = Mu(t,y,ti,tf,n)
m1 = 55;
m2 = 400;
m3 = 100;
k1 = 230000;
k2 = 30000;
k3 = 50000;
k4 = 3000;
n2 = 1500;
n3 = 4000;
n4 = 700;
tm = (ti+tf)/2;
t1 = tf/4;
L0 = 5;
v = 15;
A = 0.03;
M =[0 1 0 0 0 0;
-(k1+k2)/m1 -(n2+n4)/m1 k2/m1 n4/m1 0 n4/m1;
0 0 0 1 0 0;
k2/m2 n2/m2 -(k3+k2)/m2 -(n3+n2)/m2 k3/m2 n3/m2;
0 0 0 0 0 1;
k4/m3 n4/m3 k3/m3 n3/m3 -(k3+k4)/m3 -(n3+n4)/m3];
switch n
case 1
u = (A/2)*(1-cos(2*pi*v*t/L0));
case 2
u = 1*(t<tm)+(-1)*(t>=tm);
case 3
u = 0*(t<tm)+1*(t>=tm);
case 4
u = 1*(t<tm)+0*(t>=tm);
case 5
u = (A*t)*(t<=tm)+0*(t>tm);
case 6
u1 = A*t;
u2 = A*(t-2*tm);
u = u1.*(t<=tm)+u2.*(t>tm);
case 7
u1 = A*t;
u2 = A*(-t+4*t1);
u3 = A*(-t+3*t1);
u = u1.*(t<=t1)+u2.*((t>=t1)&(t<=2*t1)) +u3.*(t>=2*t1);
case 8
u = A.*((t>=t1)&(t<=2*t1));
end
vect=[0;-k1*u/m1;0;0;0;0];
f = M*y+vect
end
I guess what you wanted is shown above. Like you did it, f only gets a value if n=8.
In your example n=1 - that is the reason for f getting no value.
Best regards
Stephan
更多回答(1 个)
Ngoc Thanh Hung Bui
2018-5-13
编辑:Ngoc Thanh Hung Bui
2018-5-13
ode45(fMu,tspan,IC)
try ode45(@fMU,...) or ode45('fMU',...) instead
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Digital Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!