声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1378|回复: 6

[编程技巧] 各位大虾帮忙看一下这个程序?

[复制链接]
发表于 2009-3-17 16:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
function x=GEPivshow(A,B,ptol)  
if nargin<3
    ptol=50*eps;
end
[m,n]=size(A);
if m~=n
    error('A matrix needs to be square');
end
nB=n+1;
AB=[A B];
fprintf('\nBegin forward elmination with Augmented system:\n');
disp(AB);
for i =1:n-1
     [pivot,p]=max(abs(AB(i,:)));   
     ip=p+i-1;
     if ip~=i,
      fprintf('\nSwap rows %d and %d');
      AB([i ip],:)=AB([ip i],:);
    end
   pivot = AB(i,i);   
  if abs(pivot)<ptol
    error('zero pivot encountered');
  end
     for k=i+1:n
         AB(k,i:nB)=AB(k,i:nB)-(AB(k,i)/pivot)*AB(i,i:nB);
     end
     fprintf('\nAfter elimination in column %d with pivot = %f\n',i,pivot);
     disp(AB);
end
x=zeros(n,1);
x(n)=AB(n,nB)/AB(n,n);
for i=n-1:-1:1
    x(i)=(AB(i,nB)-AB(i,i+1:n)*x(i+1:n))/AB(i,i);
end
回复
分享到:

使用道具 举报

发表于 2009-3-17 16:58 | 显示全部楼层
你让大家看什么,呵呵
程序有错还是运行结果不对
 楼主| 发表于 2009-5-17 10:05 | 显示全部楼层

求助程序错误

求助程序错误我写了一段遗传算法的程序,程序中间要用龙阁库塔法解一个微分方程,方程的各个系数是上面遗传算法中计算出来的,而且要在整个遗传算法的执行过程中,每产生一代都要解一次这个微分方程,可是程序在执行过程中,在解微分方程处总是报错,请各位大虾帮帮忙忙看看究竟是哪的错误?谢谢
             程序在附件中,其中gyddj.m是m文件,lcl.txt是gyddj.m中用到的文件

[ 本帖最后由 无水1324 于 2009-5-17 14:43 编辑 ]

gyddj.m

1.34 KB, 下载次数: 0

lcl.txt

9.48 KB, 下载次数: 0

 楼主| 发表于 2009-5-17 10:20 | 显示全部楼层

微分方程---程序错误

我写了一段遗传算法的程序,程序中间要用龙阁库塔法解一个微分方程,方程的各个系数是上面遗传算法中计算出来的,而且要在整个遗传算法的执行过程中,每产生一代都要解一次这个微分方程,可是程序在执行过程中,在接微分方程处总是报错,请各位大虾帮帮忙忙看看究竟是哪的错误?谢谢
             程序在附件中,其中gyddj.m是m文件,lcl.txt是gyddj.m中用到的文件
              gyyddj.m      
                                   fidin=fopen('lcl.txt');
fidout=fopen('dmatlab.txt','w');
while ~feof(fidin)
tline=fgetl(fidin);
if double(tline(1))>=48&&double(tline(1))<=57
fprintf(fidout,'%s\n\n',tline);
continue
end
end
fclose(fidout);
d=importdata('dmatlab.txt');
Tt=d(:,1);
U=d(:,2);
P=d(:,3);
Q=d(:,4);
NIND=40;%定义个体数
MAXGEN=1000;%迭代代数
PRECI=34;%二进制位数
GGAP=0.9;%代沟
NVAR=4;%待辨识参数个数
N=249;
G=zeros(NIND,1);
B=zeros(NIND,1);
X=zeros(NIND,1);
TT=zeros(NIND,1);
Ps=zeros(NIND,1);
Qs=zeros(NIND,1);
E0=zeros(NIND,1);
C=zeros(NIND,1);
E=zeros(NIND,1);
FieldD=[rep([PRECI],[1,NVAR]);rep([0,0,0,0;0.8,0.1,0.1,2.0],[1,1,NVAR,NVAR]);rep([1;0;1;1],[1,NVAR])];
Chrom=crtbp(NIND,NVAR*PRECI);%初始种群
gen=0;%代计数器
trace=zeros(MAXGEN,2);%迭代性能追踪
y=bs2rv(Chrom,FieldD);
G=y(:,1);
B=y(:,2);
X=y(:,3);
TT=y(:,4);
for i=1:NIND
Ps(i,1)=(U(1).^2)*G(i);
Qs(i,1)=(U(1).^2)*B(i);
end
Pd=P(1)-Ps;
Qd=Q(1)-Qs;
for i=1:NIND
E0(i)=((Pd(i).^2+(Qd(i)-U(1)*U(1)/X(i)).^2).^0.5)*X(i)/U(1);
C(i)=E0(i)/((U(1)*U(1)-(Pd(i)*X(i)/E0(i)).^2).^0.5);
end
function gyddj
   t0=[0.029000:0.004:1.020991];
   for i=1:NIND,
   y0(i)=E0(i);
   a=TT(i);
   b=C(i);
   c=U(1);
   d=Pd(i);
   e=X(i);
   [t,x]=ode45(@(t,x)myfun(t,x,a,b,c,d,e),t0,y0);
   end
   function y=myfun(t,x,a,b,c,d,e)
   y=[1/a*((1)+b*(c*c-(d*e/x(1)).^2).^0.5)]';






                                             lcl.txt


      t               U                P              Q
0.025000,0.963928,1.0000000,0.6765180
0.029000,0.964097,1.0014780,0.6760770
0.033000,0.964028,1.0024740,0.6757380
0.037000,0.963911,1.0028050,0.6754380
0.041000,0.964118,1.0027090,0.6757210
0.045000,0.964115,1.0019220,0.6760260
0.049000,0.964089,1.0005390,0.6765610
0.053000,0.964025,0.9996800,0.6766990
0.057000,0.963861,0.9992950,0.6764530
0.061000,0.963960,0.9995640,0.6764990
0.065000,0.963862,1.0001810,0.6759030
0.069000,0.963937,1.0009430,0.6757300
0.073000,0.964196,1.0018520,0.6756480
0.077000,0.964270,1.0019030,0.6755930
0.081000,0.964535,1.0014890,0.6761280
0.085000,0.964243,1.0003590,0.6758470
0.089000,0.964845,0.9996280,0.6769530
0.093000,0.964501,0.9985010,0.6769750
0.097000,0.964037,0.9977810,0.6766700
0.101000,0.964078,0.9981420,0.6766260
0.105000,0.964107,0.9989700,0.6765600
0.109000,0.964044,1.0003680,0.6758700
0.113000,0.964035,1.0014680,0.6753650
0.117000,0.963996,1.0018040,0.6751670
0.121000,0.964057,1.0016830,0.6751860
0.125000,0.963927,1.0004890,0.6750230
0.129000,0.964117,0.9990880,0.6757370
0.133000,0.964088,0.9979200,0.6758630
0.137000,0.963969,0.9972460,0.6756560
0.141000,0.963979,0.9974030,0.6754700
0.145000,0.964065,0.9986590,0.6752530
0.149000,0.963928,0.9998970,0.6745170
0.153000,0.964064,1.0015780,0.6747130
0.157000,0.964082,1.0025630,0.6743860
0.161000,0.964172,1.0026400,0.6743360
0.165000,0.963802,1.0010770,0.6737770
0.169000,0.964031,1.0008450,0.6743840
0.173000,0.963699,0.9997910,0.6741380
0.177000,0.963320,0.9989420,0.6740150
0.181000,0.963408,0.9991050,0.6744080
0.185000,0.963418,1.0001640,0.6744040
0.189000,0.963440,1.0011640,0.6741440
0.193000,0.963533,1.0022790,0.6734480
0.197000,0.963706,1.0028640,0.6733940
0.201000,0.963621,1.0032780,0.6729200
0.205000,0.910961,0.7564150,0.5580520
0.209000,0.842021,0.4520810,0.3308130
0.213000,0.768718,0.2096710,0.1557830
0.217000,0.697572,0.0665660,-0.100353
0.221000,0.618293,-0.041932,-0.244000
0.225000,0.593737,0.0485230,-0.300415
0.229000,0.580279,0.1756580,-0.271658
0.233000,0.569889,0.3003350,-0.283230
0.237000,0.559781,0.3767850,-0.199710
0.241000,0.551187,0.4162890,-0.184345
0.245000,0.545425,0.4159540,-0.173939
0.249000,0.541024,0.4377930,-0.144998
0.253000,0.537799,0.4445050,-0.100091
0.257000,0.534191,0.4512090,-0.058048
0.261000,0.530823,0.4843150,-0.006487
0.265000,0.527860,0.4963640,0.0197990
0.269000,0.526083,0.4942550,0.0327420
0.273000,0.525072,0.4915160,0.0515780
0.277000,0.523211,0.4829760,0.0677550
0.281000,0.522040,0.4754910,0.0865250
0.285000,0.521493,0.4710460,0.1088260
0.289000,0.520434,0.4651720,0.1166110
0.293000,0.518566,0.4590010,0.1226140
0.297000,0.518935,0.4528710,0.1311510
0.301000,0.517400,0.4460120,0.1353200
0.305000,0.517241,0.4413080,0.1405160
0.309000,0.516968,0.4375770,0.1447180
0.313000,0.516029,0.4337890,0.1471200
0.316999,0.516523,0.4312960,0.1505850
0.320999,0.516307,0.4281720,0.1529750
0.324999,0.516234,0.4258680,0.1555540
0.328999,0.515059,0.4218680,0.1570170
0.332999,0.515083,0.4199240,0.1585640
0.336999,0.514936,0.4184270,0.1597970
0.340999,0.514951,0.4175510,0.1610900
0.344999,0.514984,0.4173650,0.1621020
0.348999,0.515861,0.4192590,0.1635120
0.352999,0.516029,0.4201050,0.1641040
0.356999,0.527387,0.4384530,0.1732860
0.360999,0.556056,0.5248380,0.2006940
0.364999,0.612862,0.6178270,0.2135370
0.368999,0.670924,0.7944350,0.3856700
0.372999,0.739019,0.9678780,0.6534530
0.376999,0.790415,1.0947650,0.9026290
0.380999,0.831177,1.0979790,1.1190270
0.384999,0.852719,1.1281740,1.3394650
0.388999,0.869659,1.0704950,1.3326160
0.392998,0.877672,1.0531680,1.2280530
0.396998,0.893666,1.0644660,1.1582920
0.400998,0.903390,1.0851520,1.1060500
0.404998,0.905388,1.0875040,1.0610660
0.408998,0.914294,1.0912400,1.0269040
0.412998,0.919164,1.0935190,0.9853390
0.416998,0.921508,1.1000740,0.9417740
0.420998,0.927134,1.1123530,0.9133020
0.424998,0.928450,1.1206540,0.8834170
0.428998,0.930136,1.1271030,0.8582850
0.432998,0.934763,1.1344870,0.8410330
0.436998,0.935163,1.1418960,0.8182660
0.440998,0.937543,1.1458250,0.8058720
0.444998,0.938323,1.1486730,0.7897150
0.448998,0.939897,1.1489320,0.7782960
0.452998,0.941970,1.1473900,0.7707970
0.456998,0.943567,1.1459620,0.7627650
0.460998,0.945181,1.1429790,0.7570290
0.464998,0.946640,1.1409540,0.7500740
0.468998,0.947542,1.1363560,0.7446960
0.472997,0.948973,1.1315060,0.7406600
0.476997,0.949926,1.1250990,0.7373670
0.480997,0.950488,1.1173190,0.7336420
0.484997,0.952332,1.1080890,0.7333600
0.488997,0.953390,1.0989590,0.7313890
0.492997,0.953193,1.0883480,0.7283110
0.496997,0.953532,1.0778010,0.7269140
0.500997,0.953945,1.0693100,0.7244390
0.504997,0.954833,1.0609320,0.7240710
0.508997,0.955246,1.0533100,0.7225000
0.512997,0.955192,1.0463390,0.7195120
0.516997,0.955824,1.0395310,0.7185210
0.520997,0.955828,1.0327970,0.7164560
0.524997,0.956520,1.0264740,0.7152890
0.528997,0.956912,1.0202760,0.7142250
0.532997,0.957271,1.0147940,0.7134020
0.536997,0.958284,1.0102850,0.7136840
0.540997,0.958640,1.0068300,0.7126690
0.544997,0.958913,1.0046760,0.7111060
0.548997,0.959151,1.0036720,0.7095180
0.552996,0.959298,1.0030490,0.7077870
0.556996,0.959686,1.0026440,0.7067020
0.560996,0.959891,1.0020380,0.7049720
0.564996,0.959915,1.0005940,0.7034370
0.568996,0.960166,0.9992880,0.7029220
0.572996,0.959892,0.9979520,0.7018340
0.576996,0.959378,0.9966240,0.7009300
0.580996,0.959413,0.9966790,0.6999500
0.584996,0.959481,0.9978890,0.6986240
0.588996,0.959487,0.9996190,0.6972370
0.592996,0.959741,1.0014420,0.6968250
0.596996,0.960201,1.0035490,0.6961380
0.600996,0.960415,1.0044810,0.6951850
0.604996,0.960615,1.0049180,0.6945070
0.608996,0.960646,1.0044720,0.6943500
0.612996,0.960491,1.0037110,0.6942030
0.616996,0.960708,1.0032410,0.6946340
0.620996,0.960664,1.0028290,0.6943080
0.624996,0.960868,1.0029920,0.6938680
0.628995,0.961157,1.0039630,0.6936120
0.632995,0.961313,1.0052360,0.6932120
0.636995,0.961451,1.0059310,0.6929320
0.640995,0.961409,1.0060270,0.6927390
0.644995,0.961431,1.0055550,0.6925720
0.648995,0.961632,1.0048770,0.6928710
0.652995,0.961191,1.0029070,0.6927280
0.656995,0.961475,1.0019130,0.6929140
0.660995,0.961483,1.0021690,0.6922710
0.664995,0.961323,1.0026110,0.6915770
0.668995,0.961391,1.0033750,0.6910670
0.672995,0.961580,1.0046300,0.6903820
0.676995,0.961660,1.0049590,0.6903840
0.680995,0.961836,1.0049470,0.6901380
0.684995,0.962091,1.0042910,0.6902580
0.688995,0.962281,1.0032500,0.6905990
0.692995,0.961960,1.0013710,0.6909740
0.696995,0.962136,1.0001280,0.6915950
0.700995,0.962053,0.9997750,0.6913260
0.704994,0.962184,1.0005360,0.6904890
0.708994,0.962141,1.0017350,0.6896160
0.712994,0.962316,1.0029900,0.6892030
0.716994,0.962328,1.0040640,0.6885290
0.720994,0.962310,1.0043080,0.6880850
0.724994,0.962315,1.0040820,0.6874990
0.728994,0.962538,1.0037960,0.6869990
0.732994,0.962345,1.0027370,0.6867860
0.736994,0.962709,1.0020490,0.6878660
0.740994,0.962940,1.0017680,0.6877680
0.744994,0.962856,1.0019150,0.6866800
0.748994,0.962974,1.0029460,0.6862240
0.752994,0.962877,1.0040380,0.6858930
0.756994,0.962768,1.0048480,0.6852510
0.760994,0.962557,1.0048260,0.6847980
0.764994,0.962837,1.0049820,0.6843470
0.768994,0.963001,1.0055150,0.6834430
0.772994,0.962857,1.0041880,0.6836120
0.776994,0.963087,1.0034140,0.6841510
0.780994,0.963086,1.0032930,0.6841440
0.784993,0.963206,1.0040930,0.6834790
0.788993,0.962957,1.0061740,0.6804540
0.792993,0.963166,1.0077290,0.6801130
0.796993,0.963301,1.0089670,0.6796910
0.800993,0.963262,1.0091660,0.6792830
0.804993,0.963426,1.0094470,0.6782770
0.808993,0.963631,1.0088220,0.6780390
0.812993,0.963539,1.0079800,0.6780320
0.816993,0.963524,1.0070850,0.6782120
0.820993,0.963745,1.0070770,0.6780250
0.824993,0.963302,1.0074030,0.6768580
0.828993,0.963314,1.0088240,0.6758430
0.832993,0.963439,1.0102560,0.6755810
0.836993,0.963597,1.0109850,0.6757930
0.840993,0.963696,1.0116370,0.6754000
0.844993,0.963997,1.0116850,0.6752880
0.848993,0.964288,1.0111610,0.6756960
0.852993,0.963816,1.0093770,0.6754970
0.856993,0.963805,1.0087060,0.6758370
0.860992,0.963692,1.0084200,0.6755500
0.864992,0.963669,1.0090660,0.6747930
0.868992,0.963645,1.0101270,0.6744150
0.872992,0.963969,1.0122100,0.6739650
0.876992,0.964129,1.0134550,0.6736820
0.880992,0.964290,1.0137890,0.6736610
0.884992,0.964578,1.0138670,0.6734670
0.888992,0.964622,1.0124840,0.6743580
0.892992,0.964622,1.0112460,0.6750360
0.896992,0.964601,1.0099760,0.6757450
0.900992,0.964614,1.0095190,0.6757860
0.904992,0.964520,1.0092260,0.6757110
0.908992,0.964404,1.0100330,0.6752580
0.912992,0.964290,1.0107930,0.6746170
0.916992,0.964208,1.0113280,0.6740730
0.920992,0.964372,1.0114970,0.6738450
0.924992,0.964434,1.0109320,0.6737050
0.928992,0.964508,1.0097480,0.6745140
0.932992,0.964256,1.0082630,0.6745990
0.936992,0.964150,1.0069670,0.6747430
0.940991,0.964094,1.0065060,0.6749400
0.944991,0.964312,1.0065650,0.6751180
0.948991,0.964496,1.0078980,0.6744460
0.952991,0.964525,1.0089010,0.6744340
0.956991,0.964871,1.0101860,0.6743210
0.960991,0.964939,1.0105600,0.6741330
0.964991,0.965377,1.0109380,0.6746700
0.968991,0.965590,1.0102820,0.6752380
0.972991,0.965692,1.0093950,0.6754040
0.976991,0.965673,1.0084710,0.6757800
0.980991,0.965751,1.0083970,0.6756330
0.984991,0.965680,1.0085250,0.6757720
0.988991,0.965596,1.0096800,0.6751970
0.992991,0.965284,1.0106360,0.6745490
0.996991,0.965221,1.0117640,0.6744030
1.000991,0.965501,1.0127540,0.6743630
1.004991,0.964772,1.0123340,0.6732330
1.008991,0.964003,1.0106030,0.6730300
1.012991,0.965021,1.0098010,0.6747820
1.016991,0.962466,1.0076600,0.6700830
发表于 2009-5-17 12:40 | 显示全部楼层
求助完整格式:出错代码和出错提示
 楼主| 发表于 2009-5-17 16:55 | 显示全部楼层

补充

运行后报错:??? Error: File: gyddj.m Line: 50 Column: 1
Function definitions are not permitted at the prompt or in scripts.
    程序中间要用龙阁库塔法解一个微分方程,方程的各个系数是上面遗传算法中计算出来的,而且要在整个遗传算法的执行过程中,每产生一代都要解一次这个微分方程,可是程序在执行过程中,在解微分方程处总是报错,请各位大虾帮帮忙忙看看究竟是哪的错误?
发表于 2009-5-17 17:46 | 显示全部楼层
方式之一若是函数请另存一档试试

[ 本帖最后由 ChaChing 于 2009-5-17 17:48 编辑 ]
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-24 15:53 , Processed in 0.066990 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表