Clear Filters
Clear Filters

Mass Matrix SWITCH Problem in Matlab

1 view (last 30 days)
Marius
Marius on 7 Apr 2016
Commented: Marius on 23 Apr 2016
I'm having a problem with my ODE in Matlab. The code to start the ODE solver is the following:
refine = 4;
options = odeset('Refine', refine,...
'RelTol', 1e-9,'absTol',1e-9*ones(1,length(y_sym)),'Mass',Mss_bend,'MassSingular','no','MstateDependence','strong')
[t_bend_i,y_bend_i] = ode45(@(t,y) ...
bending_DGL(t,y,time_r,dt,ug,vg,Mss_bend,Fss_bend,Sys,flag),time_r_i,IC_bend_i,options);
And the corresponding function file containing the ODE is:
function dy = bending_DGL(t,y,time_r,dt,ug,vg,Mss_bend,Fss_bend,Sys,flag)
r = Sys.r;
roh = Sys.roh;
A = Sys.A;
I = Sys.I;
ne = Sys.ne;
nnode = Sys.nnode;
ndof = Sys.ndof;
Le = Sys.Le;
E = Sys.E;
g = Sys.g;
EI = E*I;
EA = E*A;
%Interpolate Earthquake Excitation
i = floor(t/dt) + 1;
if i < length(time_r)
u = ug(i) + (t-time_r(i))/(time_r(i+1)-time_r(i))*(ug(i+1)-ug(i));
v = vg(i) + (t-time_r(i))/(time_r(i+1)-time_r(i))*(vg(i+1)-vg(i));
else
u = 0;
v = 0;
end
%Load State Space Representation of Problem
switch flag
case ''
dy = Fss_bend;
case 'mass'
dy = Mss_bend;
end
Now I'm trying to solve the ODE where Mss_bend is the mass matrix which is symbolic, because it is statedependent so Mss_bend(y,t) and Fss_bend(y,t) is the right side of the Equation which is state dependent too. So now I wan't to solve the Problem Mss_bend(y,t)*dy = Fss_bend where dy is the derivative of the vector y with respect to time.
When I start the calculation it gives me the
Error: Switch expression must be a scalar or string constant.
Error in bending_DGL (line 31)
switch flag
Error in @(t,y)bending_DGL(t,y,time_r,dt,ug,vg,Mss_bend,Fss_bend,Sys,flag)
Error in odearguments (line 87)
What did i do wrong? Is there a problem because the matrices are symbolic?
Matlab tell's me in the function file that y is unused:
function dy = bending_DGL(t, y,time_r,dt,ug,vg,Mss_bend,Fss_bend,Sys,flag
How do I solve this Problem. y is as symbolics in the Mss_bend and Fss_bend where y(1), y(2) etc is declared as symbols.
Thank you for your answers!
  1 Comment
Marius
Marius on 9 Apr 2016
I found the solution! I had to load the symbolic y vector
y_sym = [y(1), y(2), y(3) ...]
into my function file, end then make
Fss_bend = subs(Fss_bend,y_sym,y)
Fss_bend = subs(Fss_bend,{sym('u'), sym('v')}, {u, v}
dy = double(Fss_bend);

Sign in to comment.

Answers (4)

Walter Roberson
Walter Roberson on 7 Apr 2016
We would need to see how you initialized your flag variable to figure out why you are getting the error for switch.
You will need to change your code anyhow. Assuming that your Fss_bend and Mss_bend are expressions rather than functions:
%Load State Space Representation of Problem
switch flag
case ''
dy = double( subs(Fss_bend, {sym('t', sym('y')}, {t, y}) );
case 'mass'
dy = double( subs(Mss_bend, {sym('t', sym('y')}, {t, y}) );
end
However, it seems likely that your Mss_bend and Fss_bend also use some of the variables that you compute, such as u and v. We need to know whether Mss_bend and Fss_bend are functions or expressions, and if they are functions we need to know the order they expect their arguments in. If they are expressions then we need to know which symbolic variables need to be substituted.
  7 Comments
Marius
Marius on 8 Apr 2016
Hmm I tested it and you are right. If I just run my dy function it returns a 256x1 vector whith 16 different results each in 16 following rows. Like:
dy = 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...
so it repeats each result 16 times?
Torsten
Torsten on 8 Apr 2016
Maybe
dy = double( subs(Fss_bend, {sym('u'), sym('v'), sym('y')}, {u, v, y'}) );
?
Best wishes
Torsten.

Sign in to comment.


Marius
Marius on 8 Apr 2016
Thank you for your answer!
1)We would need to see how you initialized your flag variable to figure out why you are getting the error for switch.
What do you mean with this? All I have done with the flag is already in the code. What did I miss? Sorry I have never worked with flags and the Mass representation of ODE's before.
2)You will need to change your code anyhow. Assuming that your Fss_bend and Mss_bend are expressions rather than functions.
Fss_bend is a 16x1 symbolic vector which contains 'u','v', and 'y(1)' - 'y(16)' as symbolic variables. Mss_bend is a strongly nonlinear 16x16 symbolic matrix which contains 'y(1)' - 'y(16)'. as symbolic variables.
What do I need to change?

Marius
Marius on 21 Apr 2016
Is there another possibility to do this? Is it true that now it is substituting the variables in each step of the ODE15s solver? This takes very long. Isn't there a way that doesn't use subs?
  13 Comments
Marius
Marius on 23 Apr 2016
Now i got the next error:
Error using odearguments (line 90)
@(T,Y)ROCKING_DGL(T,Y,TIME_R,DT,UG,VG,FSSFUN,SYS) must return a column vector.
Error in ode15s (line 148)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in Processing (line 270)
[t_up,y_up,te,ye,ie] = ode15s(@(t,y) ...
Walter Roberson
Walter Roberson on 23 Apr 2016
What is your FssFun reshape()'ing to? It should be reshaping to a column vector.
If you are using matlabFunction(something) to generate FssFun then use matlabFunction(something(:))

Sign in to comment.


Marius
Marius on 23 Apr 2016
Now it gives me the error:
Error using odearguments (line 92)
@(T,Y)ROCKING_DGL(T,Y,TIME_R,DT,UG,VG,FSSFUN,SYS) returns a vector of length 576, but the length of initial conditions
vector is 24. The vector returned by @(T,Y)ROCKING_DGL(T,Y,TIME_R,DT,UG,VG,FSSFUN,SYS) and the initial conditions
vector must have the same number of elements.
Error in ode15s (line 148)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in Processing (line 270)
[t_up,y_up,te,ye,ie] = ode15s(@(t,y) ...
  1 Comment
Marius
Marius on 23 Apr 2016
I had to change it to:
FssFun = matlabFunction(Fss(:,1), 'vars', {'t', y, 'u', 'v'})

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!