필터 지우기
필터 지우기

Why matlab is not some varriables within loops

조회 수: 1 (최근 30일)
okoth ochola
okoth ochola 2024년 2월 15일
편집: VBBV 2024년 2월 21일
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
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.
okoth ochola
okoth ochola 2024년 2월 15일
@Dyuman Joshi You are right, m is an empty at first operation. Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

VBBV
VBBV 2024년 2월 21일
편집: VBBV 2024년 2월 21일
Hi @okoth ochola, You need to sum the operation over entire array f1 inside the for loop
r=0.1
r = 0.1000
R=10
R = 10
T=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)
ans = 
ans = 
1.0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by