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 Comments

Jan
Jan on 13 May 2018
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.
Thank you for the advice although what I'm working on is a project in which I was specifically asked to solve this function with this method. I will remember this for the future, though

Sign in to comment.

 Accepted Answer

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

2 Comments

That was my issue, thank you! It's my first time using a switch function
My pleasure

Sign in to comment.

More Answers (1)

Ngoc Thanh Hung Bui
Ngoc Thanh Hung Bui on 13 May 2018
Edited: Ngoc Thanh Hung Bui on 13 May 2018

0 votes

ode45(fMu,tspan,IC)
try ode45(@fMU,...) or ode45('fMU',...) instead

1 Comment

I tried the first option you suggested and got this message ""fMu" was previously used as a variable, conflicting with its use here as the name of a function or command."

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!