我是从区里粘贴过来的代码,运行后一直提示未定义gravity
clc,clear
qij=[0,190.4,25.6,18.6,10.4,10.8,1.2,16,7,37.2;
190.4,0,2067.1,2570.5,90,222,40.6,2600,690,7791.3;
25.6,2067.1,0,298,17.3,27.5,2.8,206.3,67.1,769.3;
18.6,2570.5,298,0,30.2,60.1,4.5,380.2,112.6,1370.5;
10.4,90,17.3,30.2,0,2.3,3.4,15,7.5,60;
10.8,222,27.5,60.1,2.3,0,1.0,31.2,10,116.2;
1.2,40.6,2.8,4.5,3.4,1.0,0,3.6,1.8,23;
16,2600,206.3,380.2,15,31.2,3.6,0,129.5,238.2;
7,690,67.1,112.6,7.5,10,1.8,129.5,0,152.5;
37.2,7791.3,769.3,1370.5,60,116.2,23,238.2,152.5,0];
Oi=[316,16480,3420,5232,252,481,82,3620,1178,13500];
P=[397.758,64396.486,4602.853,11421.111,389.489,629.584,86.326,6330.679,1829.193,25366.844];
Dj=[316,16480,3420,5232,252,481,82,3620,1178,13500];
A=[397.758,64396.486,4602.853,11421.111,389.489,629.584,86.326,6330.679,1829.193,25366.844];
dij=[0,153,872,166,361,113,750,489,399,1065;
153,0,829,302,477,252,627,661,285,1149;
872,829,0,835,360,598,289,520,1095,1056;
166,302,835,0,495,257,889,383,401,890;
361,477,360,495,0,248,591,427,745,1363;
113,252,598,257,248,0,736,458,508,1137;
750,627,289,889,591,736,0,757,905,1754;
489,661,520,383,427,458,757,0,738,1061;
399,285,1095,401,745,508,905,738,0,1007;
1065,1149,1056,890,1363,1137,1754,061,1007,0];
Cij=[0,153,872,166,361,113,750,489,399,1065;
153,0,829,302,477,252,627,661,285,1149;
872,829,0,835,360,598,289,520,1095,1056;
166,302,835,0,495,257,889,383,401,890;
361,477,360,495,0,248,591,427,745,1363;
113,252,598,257,248,0,736,458,508,1137;
750,627,289,889,591,736,0,757,905,1754;
489,661,520,383,427,458,757,0,738,1061;
399,285,1095,401,745,508,905,738,0,1007;
1065,1149,1056,890,1363,1137,1754,061,1007,0];
Z=-0.5;
n=size(qij,1);
Dj = Dj;
fc = dij;
delta = 0.01;
O2 = [625.993,89203.342,11598.294,28609.611,924.484,2357.686,58.156,20729.284,2881.15,55532.632];
O35 = O2';
D35 = O35;
GM_Mat_pre_25 = gravity(P,A,fc,delta);
GM_Mat_pre_35 = gravity(O35,D35,fc,delta);
function GM_Mat=gravity(O,D,fc,delta)
%This function solves the doubly constrained gravity model during trip
%distribution
%O input vector of traffic demand in each zone
%D input vector of trip end in each zone
%fc input travel impedance function
%GM_Mat output trip distribution matrix of the input OD zone
%% index initialization
m=size(O,2);
%define vectors of factor a and b with first iteration(fi) and next
%iteration(ni) respectively
a_fi=zeros(1,size(O,2));
b_fi=zeros(1,size(D,2));
a_ni=zeros(1,size(O,2));
b_ni=zeros(1,size(D,2));
%first let all elements in a_fi=1
a_fi(:)=1;
%% iteration commences
%set judging factor flag=0
flag=0;
%use flag to judge when should the loop stop
while flag==0
%compute b_fi with a_fi
for i=1:m
for j=1:m
b_fi(i)=b_fi(i)+a_fi(j)*D(j)*fc;
end
b_fi(i)=1/b_fi(i);
end
%compute a_ni with b_fi
for i=1:m
for j=1:m
a_ni(i)=a_ni(i)+b_fi(j)*D(j)*fc;
end
a_ni(i)=1/a_ni(i);
end
%compute b_ni with a_ni
for i=1:m
for j=1:m
b_ni(i)=b_ni(i)+a_ni(j)*D(j)*fc;
end
b_ni(i)=1/b_ni(i);
end
%convergence test
for i=1:m
if a_ni(i)/a_fi(i)>1-delta && a_ni(i)/a_fi(i)<1+delta && b_ni(i)/b_fi(i)>1-delta && b_ni(i)/b_fi(i)<1+delta
judge(i)=1;
%judge is, namely, a matrix to store index for judging
else
judge(i)=0;
end
end
if prod(judge)==1
flag=1;%if all elements in judge equal to 1, iteration stops
else
flag=0;
a_fi=a_ni;
b_fi=b_ni;%else, let a_ni be a_fi, b_ni be b_fi, iteration continues
end
end
%% compute the trip ditribution matrix(with a_ni and b_ni) and print it out
for i=1:m
for j=1:m
GM_Mat(i,j)=a_ni(i)*O(i)*b_ni(j)*D(j)*fc;
end
end
end
未定义gravity
我是第一次使用matlab,具体问题不是很懂·,尝试按照提示查找但是不太行
希望能得到专业人士的解答
你好,把下面这段代码复制粘贴到新建的m文件中,然后保存为gravity.m文件,同时删除你原先代码文件里面的function GM_Mat = gravity……之后的所有行,然后点击运行你原先的代码
function GM_Mat=gravity(O,D,fc,delta)
%This function solves the doubly constrained gravity model during trip
%distribution
%O input vector of traffic demand in each zone
%D input vector of trip end in each zone
%fc input travel impedance function
%GM_Mat output trip distribution matrix of the input OD zone
%% index initialization
m=size(O,2);
%define vectors of factor a and b with first iteration(fi) and next
%iteration(ni) respectively
a_fi=zeros(1,size(O,2));
b_fi=zeros(1,size(D,2));
a_ni=zeros(1,size(O,2));
b_ni=zeros(1,size(D,2));
%first let all elements in a_fi=1
a_fi(:)=1;
%% iteration commences
%set judging factor flag=0
flag=0;
%use flag to judge when should the loop stop
while flag==0
%compute b_fi with a_fi
for i=1:m
for j=1:m
b_fi(i)=b_fi(i)+a_fi(j)*D(j)*fc(i,j);
end
b_fi(i)=1/b_fi(i);
end
%compute a_ni with b_fi
for i=1:m
for j=1:m
a_ni(i)=a_ni(i)+b_fi(j)*D(j)*fc(i,j);
end
a_ni(i)=1/a_ni(i);
end
%compute b_ni with a_ni
for i=1:m
for j=1:m
b_ni(i)=b_ni(i)+a_ni(j)*D(j)*fc(i,j);
end
b_ni(i)=1/b_ni(i);
end
%convergence test
for i=1:m
if a_ni(i)/a_fi(i)>1-delta && a_ni(i)/a_fi(i)<1+delta && b_ni(i)/b_fi(i)>1-delta && b_ni(i)/b_fi(i)<1+delta
judge(i)=1;
%judge is, namely, a matrix to store index for judging
else
judge(i)=0;
end
end
if prod(judge)==1
flag=1;%if all elements in judge equal to 1, iteration stops
else
flag=0;
a_fi=a_ni;
b_fi=b_ni;%else, let a_ni be a_fi, b_ni be b_fi, iteration continues
end
end
%% compute the trip ditribution matrix(with a_ni and b_ni) and print it out
for i=1:m
for j=1:m
GM_Mat(i,j)=a_ni(i)*O(i)*b_ni(j)*D(j)*fc(i,j);
end
end
end