Inquiry on why matlab return certain values wrong

4 views (last 30 days)
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 Comments
okoth ochola
okoth ochola on 23 Feb 2024
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 on 23 Feb 2024
No, that warning has nothing to do with what you are observing.
What is your value or R?

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 23 Feb 2024
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

More Answers (0)

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!