如何调试从函数文件中调用的开关大小写的错误?



我有这段代码,当我尝试运行它时,会出现以下消息:

Error in LoadFunctionCP (line 20)
switch type
Error in TEST2 (line 34)
p=amp*LoadFunctionCP(x,L,type); %compute p at current
value of x

LoadFunctionCP在另一个文件中,我从"MY CODE"文件调用它。起初它说输出参数太多并且能够解决这个问题,但现在这些消息出现了,我不知道如何解决它。我相信第二个错误是因为第一个错误,因为"类型"有问题。类型 # 正在"我的代码"中输入,我一直在尝试调试它,但我没有运气。你们能帮我吗?

谢谢

%%%%%% FUNCTION FILE  %%%%%%%%%%%
function [p] = LoadFunctionCP(x,L,type)
clc;clear;
switch type
case 1 % Constant load 
p = 1;
case 2 % Linear ramp up
p = x/L;
case 3 % Linear ramp down
p = 1 - x/L;
case 4 %sinusoid distribution
p = sin(pi*x/L);
end

```
%%%%%%%%% MY CODE %%%%%%%%
clear; clc;

%physical properties
L=10; %length of entire bar
EA=100; %EA set to 100
%parameters
nsimp=21; %number of simpson points
GTR=51; %number of GTR points
BETA=.5; %Beta(GTR parameter)
dx= L/(GTR-1);

%end conditions
fixed=[1,0]; %boundary conditions
free=[0,1]; %boundary conditions
BC=[fixed,fixed]; %boundary conditions
type=4; %half sinusoid
amp=3; %amplitude
%%%%%%%%%
%Simpsons Rule
wt = repmat([4,2],1,nsimp);%repeats the numbers in the [],how many rows, how many columns
wt=[1,wt,4,1]; %Establish Simpson weight
npts=length(wt); %number of Simpson points
h=L/(npts-1); %step size between points
wt=wt*h/3; %complete the Simpson wts
Int=0; %initializing integral to zero
Int1=0;
for i=1:npts %loop over simpson points
x=L*(i-1)/(npts-1);
p=amp*LoadFunctionCP(x,L,type); %compute p at current value of x
Int=Int+wt(i)*p; %integrals for I0
Int1=Int1*(1/EA)*wt(i)*(L-x)*p; %Integrals for I1
end
%computation for simpson's rule
d=L/EA;
z=[Int;Int1]; %copmute z by Simpson's rule
B=[1 0 -1 0;d 1 0 -1]; %(4x1) array, '1' when unknown and '0' when known
C=B(:,BC==1); %take all rows and any column that is 1 in the array BC
s=Cz; %reduced system, solve system for 2 unknowns
%%%%%%%%%%%%%
%trapezoidal rule integer
f=[0;0;0;0]; %setting up the array
f(BC==1)=s;

Nold=f(1); %setting N to the first array
Uold=f(2); %setting U to the second array
x=0;
History=zeros(GTR,4);
%for loop
for i=1:GTR;
Pold=amp*LoadFunctionCP(x,L,type);
History(i,:)=[x,Nold,Pold,Uold];
x=x+dx;
pnew=amp*LoadFunctionCP(x,L,type);
Nnew=Nold-dx*(BETA*Pold+(1-BETA)*pnew);
Unew=Uold+dx/EA*(BETA*Nold+(1-BETA)*Nnew);
Nold=Nnew;
Uold=Unew;
end
%display of graphs
figure(1); hold on; grid on; %figure1 is 'Axial Force' with xlabel of 'x' and ylabel of 'N'
xlabel('X'); ylabel('N');
title('Axial Force');
plot(History(:,1),History(:,2));
hold off;
figure(2); hold on; grid on; %figure2 is 'load' with xlabel of 'x' and ylabel of 'P'
xlabel('X'); ylabel('P');
title('Load');
plot(History(:,1),History(:,3));
hold off;
figure(3); hold on; grid on; %figure2 is 'Displacement' with xlabel of 'x' and ylabel of 'U'
xlabel('X'); ylabel('U');
title('Displacement');
plot(History(:,1),History(:,4));
hold off;


您在函数的顶部编写了clear。这将清除所有变量,包括type。因此,下一个语句switch type调用函数type(隐藏函数的变量不再存在(。type不返回任何值,因此switch没有要打开的输入。

最新更新