How to start a time step function? SWITCH expression must be a scalar or a character vector.

I got this code from this community. This is the code for Newmark's method. Note that I only included part of the script.
switch acceleration
case 'Average'
gaama = 1/2 ;beta = 1/4 ;
case 'Linear'
gaama = 1/2 ;beta = 1/6 ;
end
.......
% Time step starts
for i = 1:nt
delP = P0+a*vel(:,i)+b*accl(:,i) ;
delu = Kcap\delP ;
delv = a1*delu-a4*vel(:,i)-a6*accl(:,i) ;
dela = a2*delu-a3*vel(:,i)-a5*accl(:,i);
disp(:,i+1) = disp(:,i)+delu ;
vel(:,i+1) = vel(:,i)+delv ;
accl(:,i+1) = accl(:,i)+dela ;
U(:,i+1) = phi*disp(:,i+1) ;
end
I loaded in a txt. file (which is an acceleration data, the matrix size is 2674x1) into the workspace, and then I ran the code. I don't know what went wrong it says: SWITCH expression must be a scalar or a character vector.

4 Comments

Hello! Can you show full code before switch operation?
function [disp,vel,accl,U,t] = NewmarkMethod(M,K,C,P,phi,dof,acceleration)
% NEWMARK'S METHOD : LINEAR SYSTEM
% Reference : Dynamics of Structures - Anil K. Chopra
%-------------------------------------------------------------------------
% Code written by :Siva Srinivas Kolukula |
% Senior Research Fellow |
% Structural Mechanics Laboratory |
% Indira Gandhi Center for Atomic Research |
% INDIA |
% E-mail : allwayzitzme@gmail.com |
%-------------------------------------------------------------------------
% Purpose : Dynamic Response of a system using linear Newmark's Method
% Synopsis :
% [disp,vel,accl,U,t] = NewmarkMethod(M,K,C,P,phi,dof,acceleration)
%
% Variable Description :
% INPUT :
% M - Mass Matrix (in modal coordinates)
% K - Stiffness Matrix (in modal coordinates)
% C - Damping Matrix (in modal coordinates)
% P - Force Matrix (in modal coordinates)
% dof - system degree's of freedom
% acceleration - Type of Newmark's Method to be used
%
% OUTPUT :
% disp - modal displacement's
% vel - modal velocities
% accl - modal accelerations
% U - system's displacement
% t - time values at which integration is done
%--------------------------------------------------------------------------
switch acceleration
case 'Average'
gaama = 1/2 ;beta = 1/4 ;
case 'Linear'
gaama = 1/2 ;beta = 1/6 ;
end
% Time step
ti = 0;
tf = 53.46 ;
dt = 0.02 ;
t = ti:dt:tf ;
nt = fix((tf-ti)/dt) ;
n = length(M) ;
% Constants used in Newmark's integration
a1 = gaama/(beta*dt) ; a2 = 1/(beta*dt^2) ;
a3 = 1/(beta*dt) ; a4 = gaama/beta ;
a5 = 1/(2*beta) ; a6 = (gaama/(2*beta)-1)*dt ;
disp = zeros(n,nt) ;
vel = zeros(n,nt) ;
accl = zeros(n,nt) ;
U = zeros(dof,nt) ;
% Initial Conditions
disp(:,1) = zeros ;
vel(:,1) = zeros ;
U(:,1) = phi*disp(:,1) ;
P0 = zeros(n,1) ;
accl(:,1) = M\(P-C*vel(:,1)-K*disp(:,1)) ;
Kcap = K+a1*C+a2*M ;
a = a3*M+a4*C ;
b = a5*M+a6*C ;
% Time step starts
for i = 1:nt
delP = P0+a*vel(:,i)+b*accl(:,i) ;
delu = Kcap\delP ;
delv = a1*delu-a4*vel(:,i)-a6*accl(:,i) ;
dela = a2*delu-a3*vel(:,i)-a5*accl(:,i);
disp(:,i+1) = disp(:,i)+delu ;
vel(:,i+1) = vel(:,i)+delv ;
accl(:,i+1) = accl(:,i)+dela ;
U(:,i+1) = phi*disp(:,i+1) ;
end
Basically, I got this Newmark-Method code from this community. I gotta be honest, I don't know how to write the function code.

Sign in to comment.

Answers (2)

Take a moment to read the switch/case documentation to become familiar with what that function does.
The acceleration variable needs to match either 'Average' or 'Linear' so it must be a character vector or string. That's case sensitive, too!
To avoid problems with case sensitivity,
switch lower(acceleration) % set to lower case
case 'average' % lower case!
gaama = ...
case 'linear' % lower case!
gaama = ...
end
Lastly, I urse people to include an "otherwise" to catch these types of errors. The "otherwise" section can either throw an error or set a default value.
switch lower(acceleration) % set to lower case
case 'average' % lower case!
gaama = ...
case 'linear' % lower case!
gaama = ...
otherwise
error('No case match for acceleration input.')
% or
% gaama = ...
end
SWITCH expression must be a scalar or a character vector.
The error message tells you, that the 7th input of the function is neither a CHAR vector nor a scalar. So how did you define acceleration in the caller?
NewmarkMethod(M,K,C,P,phi,dof,acceleration)
The method to find the cause of such error is to use the debugger. Type this in the command window:
dbstop if error
Then run your code again. Matlab stops at the error and you cancheck the contents of the concerned variable.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Asked:

on 4 Feb 2021

Edited:

Jan
on 5 Feb 2021

Community Treasure Hunt

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

Start Hunting!