Why matlab is not some varriables within loops
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello everyone, am faced with a little challege, I wrote the following code to help me do some research, but the problem is that, malab cannot see f2, yet it's there, what come be the problme? Below is my code
r=input('Enter the radius of each electron\n')
R=input('Enter the the radius of the electron path\n')
T=input('Input the desire period\n')
t=[1:0.1:10]';
for i=1:1:numel(t)
n=R*abs(sin(pi*t(i)/T))/r;
m=1:1:fix((n*r)/(r*sqrt(3)));
for j=1:1:numel(m)
f1(j,1)=2*sin(acos((m(j)*sqrt(3)*abs(csc(pi*t(i)/T)/n))));
if j==numel(m)
mmax=fix((n*r)/(r*sqrt(3)));
f1(j,1)=((r+n-mmax)/2*r)*2*sin(acos((m(j)*sqrt(3)*abs(csc(pi*t(i)/T)/n))));
f2=(1+sum(f1(j,1)));
display(f2)
end
end
f0(i,1)=f2*n*sqrt(abs(sin(pi*t(i)/T)));
if i==numel(t)
display(f0)
end
end
%below is the error that i get
%%
%Undefined function or variable 'f2'.
%Error in epr7accountingforincompleteci (line 17)
%f0(i,1)=f2*n*sqrt(abs(sin(pi*t(i)/T)));
Below is the error
Undefined function or variable 'f2'.
Error in epr7accountingforincompleteci (line 17)
f0(i,1)=f2*n*sqrt(abs(sin(pi*t(i)/T)));
댓글 수: 2
Dyuman Joshi
2024년 2월 15일
f2 will only be defined if the condition is satisfied or the inner for loop runs.
I suspect m might be an empty double. Check if that is the case or not.
If m is not empty, specify what you are trying to do and provide the inputs so that we run your code, reproduce the error you got and subsequently provide suggestions/solutions.
채택된 답변
VBBV
2024년 2월 21일
편집: VBBV
2024년 2월 21일
r=0.1
R=10
T=10
t=[1:0.1:10]';
for i=1:1:numel(t)
n=R*abs(sin(pi*t(i)/T))/r;
m=1:1:fix((n*r)/(r*sqrt(3)));
for j=1:1:numel(m)
f1(j,1)=2*sin(acos((m(j)*sqrt(3)*abs(csc(pi*t(i)/T)/n))));
if j==numel(m)
mmax=fix((n*r)/(r*sqrt(3)));
f1(j,1)=((r+n-mmax)/2*r)*2*sin(acos((m(j)*sqrt(3)*abs(csc(pi*t(i)/T)/n))));
f2=(1+sum(f1)); % sum operation over entire array f1
% display(f2);
end
end
f0(i,1)=f2*n*sqrt(abs(sin(pi*t(i)/T)));
if i==numel(t)
%display(f0);
end
end
vpa(real(f0),4) , vpa(real(f2),3)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!