Why matlab is not some varriables within loops

1 Ansicht (letzte 30 Tage)
okoth ochola
okoth ochola am 15 Feb. 2024
Bearbeitet: VBBV am 21 Feb. 2024
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 15 Feb. 2024
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 am 15 Feb. 2024
@Dyuman Joshi You are right, m is an empty at first operation. Thanks

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

VBBV
VBBV am 21 Feb. 2024
Bearbeitet: VBBV am 21 Feb. 2024
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

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by