Looping and indexing with global variables
4 次查看(过去 30 天)
显示 更早的评论
I am running a code I found attached to a research paper for a project I am working on and am running into an issue where the last loop of the main call function breaks the code. The for loop iterates over global variable s and runs other functions based on s. The function genalgodiffr runs, but breaks when calling mainfun2 and says that the index in position 1 exceeds array bounds when trying to index arrays to determine the value of diff_c. mainfun2 runs, and it will print the value of s = 1, so I know the code is running up until this line. I have also tried making s a local variable and making it a function paramater, but to no avail. Can anyone offer some tips on what to look at from here? Let me know if you need to see anything more of the code. Thank you.
%These are the loops where all variables used in diff_c are defined
for i=0:30
rb(i+1,1)= rb_max-i*.001638;
ra(i+1,1)= rb(i+1,1)*tv(i+1,1);
xa(i+1,1)= 2*(ra(i+1,1)-ra_min)*tand(alp)+.003;
xb(i+1,1)= 2*(rb_max-rb(i+1,1))*tand(alp);
bv(i+1,1)= (atand((ra(i+1,1)-rb(i+1,1))/l_h));
th_a(i+1,1)= 180+2*(atand((ra(i+1,1)-rb(i+1,1))/l_h)) ;
th_b(i+1,1)= 180-2*(atand((ra(i+1,1)-rb(i+1,1))/l_h));
th_sb(i+1,1)= th_b(i+1,1)*0.8;
end
for i=0:30
k(i+1,1)= k0*((rb(i+1,1)/rb(end-1,1))^2);
k1(i+1,1)= k(i+1,1)* ((tand(alp+ph))/tand(alp));
k2(i+1,1)= k(i+1,1)*((tand(alp-ph))/tand(alp));
p(i+1,1)= sqrt(((1.5-(f*tand(alp)))^2)-(1.5-(f*tand(alp)))+(2*k(i+1,1)*(1+(f*cotd(alp)))));
c_b(i+1,1)=tv_l(i+1,1)*c_a;
om(i+1,1)= sqrt((k1(i+1,1)-p(i+1,1))*(p(i+1,1)-k2(i+1,1))*cosd(alp+ph)*cosd(alp-ph))/(p(i+1,1)*cosd(ph));
zb(i+1,1)= 0.0174*th_b(i+1,1)+((k1(i+1,1)-k2(i+1,1))/(2*(p(i+1,1)-k2(i+1,1))))*(((sinh(om(i+1,1)*th_sb(i+1,1)*0.0174))/(om(i+1,1)))-th_sb(i+1,1));
za(i+1,1)= 0.0174*th_a(i+1,1);
end
for i=0:29
diff_ra(i+1,1)=ra(i+2,1)-ra(i+1,1);
diff_xa(i+1,1)=2*diff_ra(i+1,1)*tand(alp);
diff_rb(i+1,1)=rb(i+2,1)-rb(i+1,1);
diff_xb(i+1,1)=2*diff_rb(i+1,1)*tand(alp);
diff_za(i+1,1)=za(i+2,1)-za(i+1,1);
diff_zb(i+1,1)=zb(i+2,1)-zb(i+1,1);
diff_k(i+1,1)=k0*2*(rb(i+1,1)/(rb(end-1,1)^2))*diff_rb(i+1,1);
diff_th_a(i+1,1)=2*(cosd(bv(i+1,1))^2)* ((diff_ra(i+1,1)*((1/l_h)-(1/(tv(i+1,1)*l_h))))+(ra(i+1,1)/(tv(i+1,1)*tv(i+1,1)*l_h)));
diff_th_b(i+1,1)= -diff_th_a(i+1,1) ;
diff_cb(i+1,1)= -c_a/(tv(i+1,1)^2);
diff_p(i+1,1)= (1+(f*cotd(alp))*diff_k(i+1,1))/p(i+1,1);
end
%This is the for loop with global variable s
for s = 1:30
[n,fval2]= genalgodiffr;
fvaldiff(s+1,1)=fval2;
v1 = ka(s,1)+ n(1)*(tv(s+1,1)-tv(s,1));
v2 = r(s,1)+n(2)*(tv(s+1,1)-tv(s,1));
v3 = deL(s,1)+n(3)*(tv(s+1,1)-tv(s,1));
ka(s+1,1) = ka(s,1) + n(1)*(tv(s+1,1)-tv(s,1));
r(s+1,1)= r(s,1)+n(2)*(tv(s+1,1)-tv(s,1));
deL(s+1,1) = deL(s,1)+n(3)*(tv(s+1,1)-tv(s,1));
end
%This is the function genalgodiffr() that will run. mainfun2 is called with ga()
function [y,fval]= genalgodiffr()
global tv s;
nvars= 3;
rng('default');
lb= [70 .0010 1]./(tv(s+1,1)-tv(s,1));
ub= [150 .0015 2]./(tv(s+1,1)-tv(s,1));
options= gaoptimset('Generation',150,'FitnessLimit',0,'PopulationSize',70,'CrossoverFraction',.6);
[y,fval]= ga(@mainfun2, nvars,[],[],[],[],lb,ub,[],options);
end
%This is mainfun2
function diff_w = mainfun2(y)
global fx n cs deL xa xb za zb kb alp d ra c_a diff_xb diff_za diff_zb diff_ra diff_xa diff_ca tv v1 v2 v3 s;
deL(2,1)
deL(s+1,1)
deL(s,1)
xb(s+1,1)
xb(s,1)
za(s+1,1)
zb(s+1,1)
m(1,1)=cs(4);
a=(kb*deL(s+1,1)*(((180*(xb(s+1,1)-xb(s,1)))/(deL(s+1,1)-deL(s,1)*3.14*.0385)))*za(s+1,1))/(d*zb(s+1,1));
c=((c_a*za(s+1,1)*(cotd(alp)))/(2*ra(s+1,1)));
if s==1
ka(1,1) =cs(1);
r(1,1) =cs(3);
deL(1,1)=cs(5);
else
ka(s,1) = v1+n(1)*(tv(s+1,1)-tv(s,1));
r(s,1) = v2+n(2)*(tv(s+1,1)-tv(s,1));
deL(s,1)= v3+n(3)*(tv(s+1,1)-tv(s,1));
end
if s==1
di=(ka(s,1)*xa(s,1));
else
for l=2:s
fx(l,1)=ka(l,1)*(xa(l,1)-xa(l-1,1));
end
di=sum(fx);
end
e=4*m(1,1)*r(s,1)*(y(2)*(tv(s+1)-tv(s)))/(xa(s+1,1)-xa(s,1));
w= sqrt((a+c+di)/e)+3400;
diff_a= y(3)*(xb(s+1,1)-xb(s,1))/(y(3)*(tv(s+1,1)-tv(s,1)))*kb*za(s+1,1)/(d*zb(s+1,1))+(((deL(s,1)*3.14/180)*y(3)*(sec(atan((xb(s+1,1)-xb(s,1))*180/(y(3)*3.14*(tv(s+1,1)-tv(s,1))))))^2)*(kb*za(s+1,1)/(zb(s+1,1)*d)))+diff_za(s,1)*kb*(((deL(s,1)+y(3)*(tv(s+1,1))-tv(s,1)))*((xb(s+1,1)-xb(s,1))/(y(3)*(tv(s+1,1)-tv(s,1)))))/(zb(s+1,1)*d)-(diff_zb(s,1)*kb*deL(s,1))-(diff_zb(s,1)*kb*(((deL(s,1)+y(3)*(tv(s+1,1)-tv(s,1)))*((xb(s+1,1)-xb(s,1))/((y(3)*(tv(s+1,1)-tv(s,1)*za(s+1,1))/(d*(zb(s+1,1))^2)-(kb*((deL(s,1)+y(3)*(tv(s+1,1)-tv(s,1)))*diff_xb(s,1)*((deL(s,1)+y(3)*(tv(s+1,1)-tv(s,1)))*3.14*.0385))))*za(s+1,1))/((d*zb(s+1,1)*(180*(xb(s+1,1))^2)))))));
%This is the line where the code breaks
diff_c = ((diff_ca(s,1)*za(s+1,1)*cotd(alp))/(2*ra(s+1,1)))+ ((diff_za(s,1)*c_a*cotd(alp))/(2*ra(s+1,1)))-((diff_ra(s,1)*c_a*za(s+1,1)*cotd(alp))/(2*(ra(s+1,1)))^2);
diff_di=(y(1)*xa(s,1))+(diff_xa(s,1)*ka(s,1));
diff_e=(y(2)*4*m(1,1)*(y(2)-r(s,1))/(xa(s+1)-xa(s)))+(y(2)*4*m(1,1)*r(s,1)*(secd(atand(y(2)-r(s,1))/(xa(s+1)-xa(s))))^2);
diff_w= ((((a+c+di)*diff_e)/(e^2))-((diff_a+diff_c+diff_di)/e))/((2)*w);
end
2 个评论
dpb
2020-6-13
That's a mess for sure...w/o being able to try to run the whole thing, not much chance anybody can just look at part of the code and have a clue -- particularly with all the globals all over everywhere -- they confuse things immensely as far as knowing "who's who in the zoo".
At least run the code and paste the complete error message in context to give a few hints...
回答(1 个)
Divya Gaddipati
2020-6-15
Hi,
Seems like the variable "diff_ca" is empty. Try initializing it to an array of zeros first time the function mainfun2 is called.
if isempty(diff_ca)
diff_ca = zeros(n,1);
end
Also, make sure to check the sizes of all the variables on which you are performing operations.
Hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!