我想用matlab中的fsolve函数解非线形方程组,被调用的m文件怎么样才能用主函数里的变量,因为主函数是循环的这个变量每次都不一样,总是提示识别不了变量d,事关毕业,万分感谢,小弟新号25积分全部送上了,就是下面程序里的变量d ,是个数组
主程序:
for j=2:10
D1=2*(t0/z0)*inv(CC);
C=(t0/z0)*inv(CC);
a1=(V(1,:,j-1)')-D1*(I(1,:,j-1)');
a2=(V(nz+1,:,j-1)')+D1*(I(nz,:,j-1)');
ooo=zeros(180,180);
D=inv([C,ooo;ooo,C]);
b=D*[a1;a2];
%%%%%%%%%%%%%%%%%%%
n=180;
Y=zeros(n+1,n+1);
%%%%%%%%%%%%%%%第一行
Y(1,1)=D(1,1);
for i=2:n
Y(1,i)=D(1,i)+D(1,i+n-1);
end
Y(1,n+1)=D(1,2*n);
%%%%%%%%%%%%%%%%%第n+1行
Y(n+1,1)=D(2*n,1);
for i=2:n
Y(n+1,i)=D(2*n,i)+D(2*n,i+n-1);
end
Y(n+1,n+1)=D(2*n,2*n);
%%%%%%%%%%%%%%%%第一列
for i=2:n
Y(i,1)=D(i,1)+D(n+i-1,1);
end
%%%%%%%%%%%%%%%%%第n+1列
for i=2:n
Y(i,n+1)=D(i,2*n)+D(n+i-1,2*n);
end
%%%%%%%%%%%%%%%%%其他行列
for i=2:n
for q=2:n
Y(i,q)=D(i,q)+D(i,n+q-1)+D(n+i-1,q)+D(n+i-1,n+q-1);
end
end
b=ones(180);
d=zeros(181,1);
d(1)=b(1);
for i=2:180
d(i)=b(i)+b(179+i);
end
d(181)=b(360);
global d
%%%%%%%%%%%%%%%%%%%%%%%应用优化工具箱解非线形方程组
X0=50*ones(181,1);
T=fsolve(@Tfun,X0,optimset('display','off'));
V(nz+1,180,j)=X(181);
for iii=1:180
V(1,iii,j)=X(iii);
end
for i=1:179
V(nz+1,i,j)=V(1,i+1,j);
end
for i=2:nz
V(i,:,j)=(V(i,:,j-1)'-p3*(I(i,:,j-1)'-I(i-1,:,j-1)'))';
end
for i=1:nz
I(i,:,j)=I(i,:,j-1)'-p4*(V(i+1,:,j)'-V(i,:,j)');
end
end
被调用的程序是:
function Q=fun(T)
Q=zeros(181,1);
Q=Y*T-d;
Q(2)=Q(2)+((T(3)-T(2))/vm)^32;
Q(3)=Q(3)+((T(4)-T(3))/vm)^32-((T(3)-T(2))/vm)^32;
Q(4)=Q(4)-((T(4)-T(3))/vm)^32;