Why does matlab give different answers in different scenarios
55 次查看(过去 30 天)
显示 更早的评论
Hi, I have this code pasted below, and I have also attached the data that I used. In the first code, if I run it in "bulk" that is Ra=Var2(6:end) a strange thing happens, In certain points of the data such as Var2(39) to Var2(42) the code gives different values from the values obtained if I manually key in the values one by one for in stance I just run Ra=Var2(39) or Ra=Var2(40) etc. For example, in the first scenarion, the code returned 43,45,47 and 46 for points in region Var2(39:42) in bulk scenario. Now when i run the same points one by one I get the following values 41, 43,44 ad 44 respectively. I find this kinda strange. What can be the problem? It appears when I do hand calaculations the second set of values are the actual values. Can this be a bug in my code, I have tried figuring it out without success please assist me. The same thing Happens with the second code I have also pasted . The fisrt scenarion gave 39, 41, 41 and 43. The second scenarion gave 39, 41, 41 and 43. using the same same point as previously. The last value in the second code is differet. What can the problem. Kindly help. The same happens matlab online and oofline matlab. Note that Var2 is just the second column of the data in p excell file. Also note that I imported the data from p(2:end) the fisrt value set was assumed so that you end up with a vector array of 154 by 1 after importing. Then you run data in the range 6:end. An leads towards resolution of this is higly welcomed, thank you.
the first code
clearvars -except Var2 Var1
Ra=Var2(6:end);
for k=1:1:numel(Ra)
A=zeros(15,1);
x=zeros(15,1);
C_N=zeros(15,1);
R=Ra(k);
r=1;
x(1)=0;%2*r*sind(acosd((R-r)/(2*r*(fix(R)-1))));
A(1)=R;
C_N(1)=fix(R/r);
for i=2:numel(A)
x(i)=2*r*sind(acosd((A(i-1)-r)/(2*r*(fix(A(i-1)/r)-1))));
A(i)=R*sind(acosd(sum(x)/R));
C_N(i)=2*fix(A(i)/r);
t=sum(x);
if A(i-1)<2
A(i)=0;
x(i)=0;
C_N(i)=0;
y=sum(x)
corrlast=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=corrlast;
C_N(i-1)=C_Ncorr;
elseif t>R && A(i-1)>=2
A(i)=0;
x(i)=0;
C_N(i)=0;
% Use A(i-1) which contains the previous valid value
k0=0.5*fix(A(i-1));
k1=k0-fix(k0);
Km=[1:1:fix(0.5*A(i-1))]';
if k1==0
for j=1:numel(Km)
corr(j)=2*fix(2*(sqrt(R^2-((2*Km(j)-1)*r+0)^2)-sum(x))/r);
if j==numel(Km) && corr(j)<1 && numel(Km)==1
corr(j)=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=sum(corr);
C_N(i-1)=C_Ncorr;
elseif j==numel(Km)
C_Ncorr=sum(corr);
disp(C_Ncorr);
C_N(i-1)=C_Ncorr;
end
end
else
for j=1:numel(Km)
corr(j)=2*fix(2*(sqrt(R^2-(2*Km(j)*r+0)^2)-sum(x))/r);
if j==numel(Km)
corrlast=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=sum(corr)+corrlast;
C_N(i-1)=C_Ncorr;
end
end
end
% Break out of the main loop once condition is met
break;
end
% display(C_N(1:i));
end
Circle_N(k)=sum(C_N);
if k==numel(Ra)
C_Nupper=Circle_N;
%display(C_Nupper);
end
end
%Epr7Cpp2
% Display results
%fprintf('Final k0 value: %.3f\n', k0);
%fprintf('Km matrix:\n');
%disp(Km);
%fprintf('Valid entries in A: %d\n', sum(A~=0));
%fprintf('A values:\n');
%disp(A(A~=0));9999999
The second code
%xlsxread('p')
clearvars -except Var2 Var1 C_Nupper Ra
Ra=Var2(6:39);
for k=1:1:numel(Ra)
A=zeros(15,1);
x=zeros(15,1);
C_N=zeros(15,1);
R=Ra(k);
r=1;
T1=2*r*sind(acosd((R-r)/(2*r*(fix(R/r)-1))));
B=R*sind(acosd(T1/R));
xo1=2*r*sind(acosd(((R*(2-fix(R))-r)/(2*r*(fix(R)-1)))+(B/(2*r))));
B2=R*sind(acosd(xo1/R));
x(1)=0;
x(2)=(B2/B)*xo1;
A(1)=R/r;
A(2)=R*sind(acosd(sum(x(2))/R));
C_N1=fix(R/r);
C_N(1)=C_N1;
C_N(2)=2*fix(A(2));
clearvars -except Var2 Var1 C_Nupper Ra A x C_N r R k
for i=3:numel(A)
T=2*r*sind(acosd((A(i-1)-r)/(2*r*(fix(A(i-1))-1))));
xtst=R*sind(acosd((T+sum(x))/R));
%if A(i-1)>=2x01=2*r*sind(acosd(((2-1)+()))T=2*r*sind(acosd((A(i-1)-r)/(2*r*(fix(A(i-1))-1))));
if A(i-1)>=2 && xtst>=2
T=2*r*sind(acosd((A(i-1)-r)/(2*r*(fix(A(i-1))-1))));
xtst=R*sind(acosd((T+sum(x))/R));
T2=2*r*sind(acosd((xtst-r)/(2*r*(fix(xtst)-1))));
tst=T+T2+sum(x);
if tst<R
B1=R*sind(acosd((T+sum(x))/R));
xo=2*r*sind(acosd(((A(i-1)*(2-fix(A(i-1)))-r)/(2*r*(fix(A(i-1))-1)))+(B1/(2*r))));
B3=R*sind(acosd((sum(x)+xo)/R));
x(i)=(B3/B1)*xo;
A(i)=R*sind(acosd(sum(x)/R));
C_N(i)=2*fix(A(i)/r);
%t=sum(x);
elseif tst>R %&& A(i-1)>=2
A(i)=xtst;
x(i)=T;
C_N(i)=0;
% Use A(i-1) which contains the previous valid value
k0=0.5*fix(A(i));
k1=k0-fix(k0);
Km=[1:1:fix(0.5*A(i))]';
if k1==0
for j=1:numel(Km)
corr(j)=2*fix(2*(sqrt(R^2-((2*Km(j)-1)*r+0.0)^2)-sum(x))/r);
if j==numel(Km) && corr(j)<1 && numel(Km)==1
corr(j)=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=sum(corr);
C_N(i)=C_Ncorr;
elseif j==numel(Km)
C_Ncorr=sum(corr);
disp(C_Ncorr);
C_N(i)=C_Ncorr;
end
end
else
for j=1:numel(Km)
corr(j)=2*fix(2*(sqrt(R^2-((2*Km(j)-1)*r+0.0)^2)-sum(x))/r);
if j==numel(Km)
corrlast=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=sum(corr)+corrlast;
C_N(i)=C_Ncorr;
end
end
end
% Break out of the main loop once condition is met
break;
end
else
A(i)=xtst;
x(i)=T;
C_N(i)=0;
y=sum(x);
corrlast=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=corrlast;
C_N(i)=C_Ncorr;% display(C_N(1:i));
break;
end
end
Circle_N(k)=sum(C_N);
if k==numel(Ra)
%gvf=0.5*(C_Nupper+Circle_N);
display(Circle_N');
%headers = {'Upper', 'Lower'};
%T = table(C_Nupper', Circle_N', 'VariableNames', headers);
%T = table(Circle_N)
%excelFile = 'Eps7cpp72g.xlsx';
%writetable(T, excelFile)
%display(gvf);
end
end
% Display results
%fprintf('Final k0 value: %.3f\n', k0);
%fprintf('Km matrix:\n');
%disp(Km);
%fprintf('Valid entries in A: %d\n', sum(A~=0));
%fprintf('A values:\n');
%disp(A(A~=0));
2 个评论
Rik
2025-8-10,10:53
You are about to level up as a programmer. You just discovered that not using functions to keep your workspace clean makes your life difficult and has you resorting to clearvars. You are also discovering that a lack of meaningful comments and the use of short variable names makes it difficult for others (including future you) to understand your code and suggest edits.
I suggest you split off parts of your code to separate function(s). That way you have a documented interface with reproducible behavior for input and output. Try to write the documentation and comments first, and then write the code. That way you are forced to think through the entire process, instead of having to reconstruct what your code is doing.
The additional benefits should be clear: functions allow you to update code in place, with the reassurance that the rest of your code will keep working as expected. Comments help you and others understand what your code is doing in case of unexpected behavior.
The code as you posted has too few comments and is insufficiently clear to me to attempt to explain what is going on. I expect you will solve your problem on your own after writing a few functions and adding comments (and perhaps making the variable names more self-documenting, the max variable length has been increased from 63 to 2048). If you don't, feel free to come back here to edit your question accordingly.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!