フィルターのクリア

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 Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by