【发布时间】:2019-09-15 01:19:51
【问题描述】:
我有这段代码,当我尝试运行它时,会出现以下消息:
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;
【问题讨论】:
-
我很快就发现了这个问题。但下次请先阅读minimal reproducible example。您发布的代码比重现问题所需的代码要长得多。创建重现错误的最小示例通常会帮助您自己找到问题。如果没有,其他人会更容易找到问题。
标签: matlab function switch-statement