Inquiry on why matlab return certain values wrong

3 次查看(过去 30 天)
Hi, allow me be a bother one more time. Am really grateful for the assistance have recieved from this community so far. With the help from you have been able to debug most of my codes. The code is working very well for certain values but bad things happen when I reduce the interval between data. To understand my question consider the below code. if I let t=[0:1:30], with r=0.1 and T=5, the code reurns correct values. Se the attached graph and the code attached. However, any attmept to make any changes to the inputs, like reducing the time steps maybe to 0.1 so that t=[0:0.1:30], some other zeros appear whiich i really don't expect. See the second figure. What might be causing this? Have reviewed te equation and am certain, the equations are correct. Is there a trick I can use to avoid such?
Have also found out that Matlab is skeeping certain value of t, for example the fisrt unnecesary zero occur at t=0.6, t=0.7...upto t=0.9, then at t=1.0 and t=1.1, the correct output is given, then aftert that another set of zeros are the output. Why is the code behaving this way
clc
clear
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=(0:0.1:30)';
n=R/r;
%h=6.626e-34;
%me=9.109e-31;
%A=sqrt((h*f)/(R*acc1*me));
for i=1:1:numel(t)
m1=n*abs(sin(pi*t(i)/T));
if m1<2*cos(pi/6)
f0(i,1)=fix(m1);
else
m=1:1:fix((m1*r)/(2*r*cos(pi/6)));
for j=1:1:numel(m)
f1(j,1)=fix(m1*2*sin(acos((m(j)*sqrt(3)/m1))));
if j==numel(m)
mmax=m(end);
f10=m1*2*sin(acos((mmax*sqrt(3)/m1)));
f11=(fix(f10/2))/2;
f12=f11-fix(f11);
if f12==0
Na=1:1:((fix(f10/2)-2)/2);
for a=1:1:((fix(f10/2)-2)/2)
f13(a,1)=2*fix(2*((m1*r*sin(acos((2*r*Na(a))/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
if a==numel(Na)
f14=2*fix(2*((m1*r*sin(acos((r)/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
f15=fix(m1)+f14+sum(f13(1:a));
f1(j,1)=f15;
end
end
else
Nb=1:1:((fix(f10/2)-1)/2);
for b=1:1:((fix(f10/2)-1)/2)
f16(b,1)=2*fix(2*((m1*r*sin(acos((2*r*Nb(b))/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
if b==numel(Nb)
f17=fix(2*((m1*r)-(2*mmax*r*cos(pi/6)))/r);
f18=fix(m1)+f17+sum(f16(1:b));
f1(j,1)=f18;
end
end
f2=(sum(f1(1:j)));
f0(i,1)=f2;
end
end
end
end
if i==numel(t)
plot(t,f0)
end
end
The first figure and results when t=[0:1:30] which is correct
f0 =
0
118
319
319
118
0
118
319
319
118
0
118
319
319
118
0
118
319
319
118
0
118
319
319
118
0
118
319
319
118
0
>>
This is the sceond figure when time step is reduced to 0.1 so that t=[0:0.1:30]'
f0 =
0
1
3
8
20
30
0
0
0
0
118
138
0
0
0
0
0
0
286
0
319
331
0
345
348
0
348
345
0
331
319
0
286
0
0
0
0
0
0
138
118
0
0
0
0
30
20
8
3
1
0
1
3
8
20
30
0
0
0
0
118
138
0
0
0
0
0
0
286
0
319
331
0
345
348
0
348
345
0
331
319
0
286
0
0
0
0
0
0
138
118
0
0
0
0
30
20
8
3
1
0
1
3
8
20
30
0
0
0
0
118
138
0
0
0
0
0
0
286
0
319
331
0
345
348
0
348
345
0
331
319
0
286
0
0
0
0
0
0
138
118
0
0
0
0
30
20
8
3
1
0
1
3
8
20
30
0
0
0
0
118
138
0
0
0
0
0
0
286
0
319
331
0
345
348
0
348
345
0
331
319
0
286
0
0
0
0
0
0
138
118
0
0
0
0
30
20
8
3
1
0
1
3
8
20
30
0
0
0
0
118
138
0
0
0
0
0
0
286
0
319
331
0
345
348
0
348
345
0
331
319
0
286
0
0
0
0
0
0
138
118
0
0
0
0
30
20
8
3
1
0
1
3
8
20
30
0
0
0
0
118
138
0
0
0
0
0
0
286
0
319
331
0
345
348
0
348
345
0
331
319
0
286
0
0
0
0
0
0
138
118
0
0
0
0
30
20
8
3
1
0
  2 个评论
okoth ochola
okoth ochola 2024-2-23
Kindly check the below snippet can it have anaythig to do with it? Apparently I keep on getting suh warning at the side bar. Even though they're preventing my code from running. Anyone who has the solution or any leads,please assist, Thank you
Cris LaPierre
Cris LaPierre 2024-2-23
No, that warning has nothing to do with what you are observing.
What is your value or R?

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2024-2-23
No value is assigned to f0 in the following condition
if f12==0
Na=1:1:((fix(f10/2)-2)/2);
for a=1:1:((fix(f10/2)-2)/2)
f13(a,1)=2*fix(2*((m1*r*sin(acos((2*r*Na(a))/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
if a==numel(Na)
f14=2*fix(2*((m1*r*sin(acos((r)/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
f15=fix(m1)+f14+sum(f13(1:a));
f1(j,1)=f15;
end
end
else
Perhaps you meant to do this?
r= 0.1; %input('Enter the radius of each electron\n');
R= 10; %input('Enter the the radius of the electron path\n');
T= 5; %input('Input the desire period\n');
t= (0:.1:30)';
n=R/r;
%h=6.626e-34;
%me=9.109e-31;
%A=sqrt((h*f)/(R*acc1*me));
for i=1:1:numel(t)
m1=n*abs(sin(pi*t(i)/T));
if m1<2*cos(pi/6)
f0(i,1)=fix(m1);
else
m=1:1:fix((m1*r)/(2*r*cos(pi/6)));
for j=1:1:numel(m)
f1(j,1)=fix(m1*2*sin(acos((m(j)*sqrt(3)/m1))));
if j==numel(m)
mmax=m(end);
f10=m1*2*sin(acos((mmax*sqrt(3)/m1)));
f11=(fix(f10/2))/2;
f12=f11-fix(f11);
if f12==0
Na=1:1:((fix(f10/2)-2)/2);
for a=1:1:((fix(f10/2)-2)/2)
f13(a,1)=2*fix(2*((m1*r*sin(acos((2*r*Na(a))/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
if a==numel(Na)
f14=2*fix(2*((m1*r*sin(acos((r)/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
f15=fix(m1)+f14+sum(f13(1:a));
f1(j,1)=f15;
end
end
else
Nb=1:1:((fix(f10/2)-1)/2);
for b=1:1:((fix(f10/2)-1)/2)
f16(b,1)=2*fix(2*((m1*r*sin(acos((2*r*Nb(b))/(m1*r))))-(2*mmax*r*cos(pi/6)))/r);
if b==numel(Nb)
f17=fix(2*((m1*r)-(2*mmax*r*cos(pi/6)))/r);
f18=fix(m1)+f17+sum(f16(1:b));
f1(j,1)=f18;
end
end
end
% move this code outside the if-else statement #############
f2=(sum(f1(1:j)));
f0(i,1)=f2;
end
end
end
if i==numel(t)
plot(t,f0)
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by