matlab - 如何调试从函数文件中调用的 switch case 的错误?
问题描述
我有这段代码,当我尝试运行它时,会出现以下消息:
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=C\z; %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
没有可打开的输入。
推荐阅读
- c - 按下键时多次执行功能(ncurses)
- javascript - 使用 jQuery 在按钮列表中只触发一个按钮
- c - strtok 不会继续下一次迭代
- amazon-web-services - 如何使用 cdk 将 github repo 与 aws 连接?
- python-3.x - 线程中的服务器(Python3.9.0+aiohttp):RuntimeError: can't register atexit after shutdown
- python - 打开和关闭大量文件后,脚本python无缘无故停止运行
- javascript - 为什么我的道具动作在“onClick”中的“if”语句中不起作用,但在“if”语句之外起作用?
- laravel - Echo 未收到发送给同一用户的推送客户端事件
- mariadb - UPDATE 引用更新表中的字段会导致死锁吗?
- jquery - 从水平拖动到垂直拖动