# Why my code is running but giving wrong values

1 visualizzazione (ultimi 30 giorni)
okoth ochola il 21 Feb 2024
Risposto: Voss il 21 Feb 2024
Hi, I have this code which have used to implement a periodic function of a phenomenon I observed in the laboratory. However the results of the code are not similar to my expectations. Basically, the code should pick a value in matrix "t" then perform operations in line 8 and 9. then make a decison based on line 10. If the conditions are statisfied, then operation in line 11 is executed, otherwise operations after the first else are executed. The code is running very well and giving outputs but the values are wrong, is it possible that the problem maybe arising from the ways idices i, and j are treated. So long as the condintion in line 10 is not satistfied, the values of 'i' and 'm1' passed to the subsequent loops must be maintained, I doubt if this is appening. Can this be the source of my problem? If yes, how can I manouver this problem? you can assume r=0.1, R=10 and T=10. Any clue will do better, thank you great community. I expected higher values like >500 even more than 1000
%this is the begining of my code. This is an implimentetation of a periodic
%function which describes a physical phenomenon observed in the lab.
r=0.1;%input('Enter the radius of each electron\n');
R=10;%input('Enter the the radius of the electron path\n');
T=10;%input('Input the desire period\n');
t=[0:1:100]'; %this is time steps and based on the time, the following operations are done;
for i=1:1:numel(t)
n=R/r;
m1 =n*abs(sin(pi*t(i)/T));
if m1<2*cos(pi/6) %this one checks the value of m1, and makes a decision based on that value as shown
f0(i,1)=fix(m1); %if the value is less than the value stated above, then the trunctated ddigit is stored in f0(i,1)
else %if not, then the following operations should be carried out with the value of 'i' being ststic in each iteration
m0=1:1:fix((m1*r)/(r*sqrt(3))); %note this operation is done based on the result from the 'if' ststement and the value of 'i' should not be changed till the opertation is done
for j=1:1:numel(m0)
%Maintatining the value of 'i' and m1 that was passed to this
%loop should be maintained throughout the operation in each
%iteration
f1(j,1)=n*sqrt(abs(sin(pi*t(i)/T)))*2*sin(acos((m0(j)*(sqrt(3))*abs(csc(pi*t(i)/T)/n))));
if j==numel(m0)
%if the last iteration of this subruotine is reached,then
%the following operations should be done
mmax=fix((m1*r)/(r*sqrt(3)));
f10=(n*sqrt(abs(sin(pi*t(i)/T))))*2*sin(acos((m0(j)*sqrt(3)*abs(csc(pi*t(i)/T)/n))));
%based on the value of f0, we check whether it's even or
%odd number by the following operations
f11=(fix(f10/2))/2;
f12=f11-fix(f11);
if f12==0
%if the number is even; then the following operations
%are done
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))/(n*r*sin(pi*t(i)/T)))))-(2*mmax*r*cos(pi/4)))/r);
f14=fix(2*((m1*r*sin(acos((r)/(n*r*sin(pi*t(i)/T)))))-(2*mmax*r*cos(pi/4)))/r);
if a==numel(Na)
f15=f14+sum(f13(a,1));
f1(j,1)=f15;
end
end
else
%if the number is odd then the following operations
%are done
Na=1:1:((fix(f10/2)-1)/2);
for a=1:1:((fix(f10/2)-1)/2)
f13(a,1)=2*fix(2*((m1*r*sin(acos((2*r*Na(a))/(n*r*sin(pi*t(i)/T)))))-(2*mmax*r*cos(pi/4)))/r);
f14=fix(2*((n*r*abs(sin(pi*t(i)/T)))-(2*mmax*r*cos(pi/4)))/r);
if a==numel(Na)
%add the values of f13 and store then in f1
f15=f14+sum(f13(a,1));
f1(j,1)=f15;
end
end
end
%sum f1 and add to the constatnt as shown in f2 then
%storevin f0
f2=1*(n*sqrt(abs(sin(pi*t(i)/T))))+sum(f1(j,1));
f0(i,1)=f2;
%after the end of this operation, the program should
%go the next value of i, in matrix t and repeat the
%operation ends
end
end
end
if i==numel(t)
display(f0)
tr=[1:1:numel(f0)];
plot(tr,f0)
end
end
f0 = 101×1
0 82.5893 138.6672 178.9454 202.5221 210.0000 202.5221 178.9454 138.6672 82.5893
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
okoth ochola il 21 Feb 2024
@Dyuman Joshi, this is the exact same result am getting. My problem is that the values are not correct and i can't figure out why

Accedi per commentare.

### Risposta accettata

Voss il 21 Feb 2024
There are a few places in the code where you use sum on a scalar value.
Two of them look like this
f15=f14+sum(f13(a,1));
% ^^^^^^^^ scalar
and one looks like this:
f2=1*(n*sqrt(abs(sin(pi*t(i)/T))))+sum(f1(j,1));
% ^^^^^^^ scalar
If these are accurate then sum is unnecessary, but I suspect they are not accurate based on this comment:
%add the values of f13 and store then in f1
f15=f14+sum(f13(a,1));
The comment suggests you are intending to sum multiple values, in which case perhaps you mean
%add the values of f13 and store then in f1
f15=f14+sum(f13(1:a));
% ^^^^^^^^ length-a vector
and
f2=1*(n*sqrt(abs(sin(pi*t(i)/T))))+sum(f1(1:j));
% ^^^^^^^ length-j vector
Making those changes produces results above 1000, as you expect. (Are they correct? I can't know the answer to that.)
%this is the begining of my code. This is an implimentetation of a periodic
%function which describes a physical phenomenon observed in the lab.
r=0.1;%input('Enter the radius of each electron\n');
R=10;%input('Enter the the radius of the electron path\n');
T=10;%input('Input the desire period\n');
t=[0:1:100]'; %this is time steps and based on the time, the following operations are done;
for i=1:1:numel(t)
n=R/r;
m1 =n*abs(sin(pi*t(i)/T));
if m1<2*cos(pi/6) %this one checks the value of m1, and makes a decision based on that value as shown
f0(i,1)=fix(m1); %if the value is less than the value stated above, then the trunctated ddigit is stored in f0(i,1)
else %if not, then the following operations should be carried out with the value of 'i' being ststic in each iteration
m0=1:1:fix((m1*r)/(r*sqrt(3))); %note this operation is done based on the result from the 'if' ststement and the value of 'i' should not be changed till the opertation is done
for j=1:1:numel(m0)
%Maintatining the value of 'i' and m1 that was passed to this
%loop should be maintained throughout the operation in each
%iteration
f1(j,1)=n*sqrt(abs(sin(pi*t(i)/T)))*2*sin(acos((m0(j)*(sqrt(3))*abs(csc(pi*t(i)/T)/n))));
if j==numel(m0)
%if the last iteration of this subruotine is reached,then
%the following operations should be done
mmax=fix((m1*r)/(r*sqrt(3)));
f10=(n*sqrt(abs(sin(pi*t(i)/T))))*2*sin(acos((m0(j)*sqrt(3)*abs(csc(pi*t(i)/T)/n))));
%based on the value of f0, we check whether it's even or
%odd number by the following operations
f11=(fix(f10/2))/2;
f12=f11-fix(f11);
if f12==0
%if the number is even; then the following operations
%are done
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))/(n*r*sin(pi*t(i)/T)))))-(2*mmax*r*cos(pi/4)))/r);
f14=fix(2*((m1*r*sin(acos((r)/(n*r*sin(pi*t(i)/T)))))-(2*mmax*r*cos(pi/4)))/r);
if a==numel(Na)
f15=f14+sum(f13(1:a));
f1(j,1)=f15;
end
end
else
%if the number is odd then the following operations
%are done
Na=1:1:((fix(f10/2)-1)/2);
for a=1:1:((fix(f10/2)-1)/2)
f13(a,1)=2*fix(2*((m1*r*sin(acos((2*r*Na(a))/(n*r*sin(pi*t(i)/T)))))-(2*mmax*r*cos(pi/4)))/r);
f14=fix(2*((n*r*abs(sin(pi*t(i)/T)))-(2*mmax*r*cos(pi/4)))/r);
if a==numel(Na)
%add the values of f13 and store then in f1
f15=f14+sum(f13(1:a));
f1(j,1)=f15;
end
end
end
%sum f1 and add to the constatnt as shown in f2 then
%storevin f0
f2=1*(n*sqrt(abs(sin(pi*t(i)/T))))+sum(f1(1:j));
f0(i,1)=f2;
%after the end of this operation, the program should
%go the next value of i, in matrix t and repeat the
%operation ends
end
end
end
if i==numel(t)
display(f0)
tr=[1:1:numel(f0)];
plot(tr,f0)
end
end
f0 = 101×1
1.0e+03 * 0 1.6814 4.4190 7.0201 8.9817 9.5977 8.9817 7.0201 4.4190 1.6814
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by