Why matlab is not some varriables within loops

2 次查看(过去 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
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
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