Bonsoir,

j'ai développé une petite application visant à modéliser la trajectoire de rayons lumineux dans un espace courbe. Dans ma fonction principale, je fais appel au solver "ode" de Matlab (appelons le "ode n°1").
Dans la fonction de cet ode n°1, je fais aussi appel à un ode (ode n°2) dont les résultats sont interpolés via polyfit et transformés en fonctions symboliques, qui seront utilisées pour définir le système différentiel de l'ode n°1. Le problème à l'exécution se situe au niveau de l'ode n°2, j'obtiens l'erreur suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
  ??? Error using ==> subsref
Index exceeds matrix dimensions.
 
Error in ==> sym.subsref at 16
  y = builtin('subsref',struct(x),a);
 
Error in ==> geofried>syseqgeo2 at 352
dydtgeo2=[ y(2) ;
 
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.
 
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
 
Error in ==> geofried at 108
[s,ys2]=ode45(@syseqgeo2,[0 10000],init_2,options);
Une fois les équations symboliques récupérées dans une chaîne de caractères (ici dans str1, str2, str3 et str4), peut on définir le système différentiel (ode n°1) de la forme suivante ? :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
dydtgeo2=zeros(8,1);
dydtgeo2=[ y(2) ;                        
           str1 ;
           y(4) ;
           str2 ;
           y(6) ;
           str3 ;
           y(8) ;
           str4 ; ] ;
avec par exemple str1 qui vaut :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
(0.0000000033333333333333334030752027670949*y(4)^2*(0.0014032354134250921197235584259033*y(1)-35545667788043049762816.0)*(0.0000000000023387256890418201995392640431722*y(1)^2-118485559293476.83254272*y(1)+13507353759452999680.0))/(y(3)^2+1.0)+0.0000000033333333333333334030752027670949*y(3)^2*y(6)^2*(0.0014032354134250921197235584259033*y(1)-35545667788043049762816.0)*(0.0000000000023387256890418201995392640431722*y(1)^2-118485559293476.83254272*y(1)+13507353759452999680.0)+0.0000000033333333333333334030752027670949*y(3)^2*y(8)^2*sin(y(5))^2*(0.0014032354134250921197235584259033*y(1)-35545667788043049762816.0)*(0.0000000000023387256890418201995392640431722*y(1)^2-118485559293476.83254272*y(1)+13507353759452999680.0)
Est-ce que l'erreur "??? Error using ==> subsref
Index exceeds matrix dimensions" est due à la longueur des chaînes str1, str2, str3 et str4 ?

J'ai essayé de mettre en dur (avec un copier-coller, après avoir les avoir sauvegardées dans un fichier) la valeur des chaînes "str1, ...." dans le système différentiel mais j'obtiens la même erreur.

Je ne vois pas à quel moment je dépasse un index de matrice.

Voici le code en entier, quelques fonctions ne sont pas encore utilisées, l'ode n°1 est défini dans la fonction "syseqgeo2(s,y)" et l'ode n°2 dans "syseq1(t,y) " :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
 
function geofried
 
 
H0=71000/(3*10^(22));
cv=3*10^(8);
 
 
 
t_begin=380000*(3600*24*365);
t_final=40*10^(9)*(3600*24*365);
Omega1_m=0.3;
Omega1_red=0;
Omega1_k=1-Omega1_m-Omega1_red;
Omega2_m=0.3;
Omega2_red=0.7;
Omega2_k=1-Omega2_m-Omega2_red;
Omega3_m=5;
Omega3_red=0;
Omega3_k=1-Omega3_m-Omega3_red;
 
a1_0=0.001;
a1_prim_0=H0*(Omega1_m/a1_0+Omega1_k)^(1/2);
init1=[ a1_0 ; a1_prim_0 ];
a2_0=0.001;
a2_prim_0=H0*(Omega2_m/a2_0+Omega2_k)^(1/2);
init2=[ a2_0 ; a2_prim_0 ];
a3_0=0.001;
a3_prim_0=H0*(Omega3_m/a3_0+Omega3_k)^(1/2);
init3=[ a3_0 ; a3_prim_0 ];
options = odeset('Events',@events,'RelTol',10^(-13),'AbsTol',10^(-15));
 
 
 
 
[t1,y1]=ode45(@syseq1,[t_begin t_final],init1,options);
[t2,y2]=ode45(@syseq2,[t_begin t_final],init2,options);
[t3,y3]=ode45(@syseq3,[t_begin t_final],init3,options);
 
t1=t1/(3600*24*365*10^(9));
t2=t2/(3600*24*365*10^(9));
t3=t3/(3600*24*365*10^(9));
figure(1);
plot(t1,y1(:,1),'b',t2,y2(:,1),'r',t3,y3(:,1),'g'); 
xlabel('Time in Gyr');
ylabel('Relative size of the universe');
ylim([ 0 10]);
 
%%%%%%%%% Calcul des polynomes %%%%%%%%%%%%%%
 
P1=polyfit(t1,y1(:,1),20);
disp('P1='),disp(P1);
P2=polyfit(t2,y2(:,1),20);
P3=polyfit(t3,y3(:,1),20);
 
 
v1=polyval(P1,t1(:));
v2=polyval(P2,t2(:));
v3=polyval(P3,t3(:));
figure(2);
plot(t1,v1,'k',t2,v2,'k',t3,v3,'k');
 
 
syms t;
f1=poly2sym(P1,t);
f2=poly2sym(P2,t);
f3=poly2sym(P3,t);
 
fin=inline(char(f1));
disp('fin(1)='),disp(fin(1));
f0=fin(0);
 
dist=3000;
 
 
R0=1;
H0=65000;
cv=3*10^(8);
ctk=(2*cv)/(3*H0);
w1=ctk;
rayon=30;
alpha0=rayon/dist;
 
 
%for m=-1:2:1
m=-1;
z1=pi/2;
y1=pi/2-m*alpha0;
x1=dist/sin(y1);
 
xp=-1;
yp=0;
zp=0;
 
wp1=sqrt(R0*R0*(3*H0/(2*cv)*w1)^(4/3)*(xp^(2)+x1^(2)*yp^(2)+x1^(2)*(sin(y1))^(2)*zp^(2)));
 
 
wp2=sqrt(f0^2*(xp^(2)/(1+x1^(2))+x1^(2)*yp^(2)+x1^(2)*(sin(y1))^(2)*zp^(2)));    
 
 
options=odeset('NonNegative',3);
 
init_1=[ w1;wp1;x1;xp;y1;yp;z1;zp];
init_2=[ w1;wp2;x1;xp;y1;yp;z1;zp];
 
disp('ici1');
 
[s,ys1]=ode45(@syseqgeo1,[0 10000],init_1,options);
[s,ys2]=ode45(@syseqgeo2,[0 10000],init_2,options);
 
disp('ici3');
 
x_cart1=ys1(:,1);
y_cart1=ys1(:,3).*sin(ys1(:,5));
z_cart1=ys1(:,3).*cos(ys1(:,5));
 
%x_cart2=ys2(:,1);
%y_cart2=ys2(:,3).*sin(ys2(:,5));
%z_cart2=ys2(:,3).*cos(ys2(:,5));
 
figure(4);
 
hold on;
 
xlabel('Oy - Distance');
ylabel('Oz - Size');
 
%plot(y_cart1,z_cart1,'b',y_cart2,z_cart2,'r');
plot(y_cart1,z_cart1,'g');
 
 
%end
 
end
 
function dydtgeo1 = syseqgeo1(s,y) 
R0=1;
H0=65000;
cv=3*10^(8);
 
 dydtgeo1=zeros(8,1);
 dydtgeo1=[ y(2) ;                        
       -((2/3)*R0^(2)*((3*H0/(2*cv))^(4/3))*y(1)^(1/3))*(y(4)^(2)+abs(y(3))^(2)*y(6)^(2)+abs(y(3))^(2)*(sin(y(5)))^(2)*y(8)^(2)) ;
       y(4) ;
       -(4/(3*y(1)))*y(2)*y(4)+abs(y(3))*(y(6)^(2)-(sin(y(5)))^(2)*y(8)^(2));                            
       y(6) ;
       -(2/abs(y(3)))*y(4)*y(6)-(4/(3*y(1)))*y(2)*y(6)+((sin(2*y(5)))*(y(8)^(2))/2);                           
       y(8) ;
       -2*y(8)*((2/(3*y(1)))*y(2)+(cot(y(5)))*y(6)+(1/abs(y(3)))*y(4)) ];                   
 
 
end
 
%%%%%%%%%%%%%%%%%%%%% système différentiel correspondant à Omega_m=0.3 et
%%%%%%%%%%%%%%%%%%%%% k=-1 %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 
function dydtgeo2 = syseqgeo2(s,y) 
R0=1;
H0=65000;
cv=3*10^(8);
 
disp('ici2');
 
 
t_begin=380000*(3600*24*365);
t_final=40*10^(9)*(3600*24*365);
Omega1_m=0.3;
Omega1_red=0;
Omega1_k=1-Omega1_m-Omega1_red;  
a1_0=0.001;
a1_prim_0=H0*(Omega1_m/a1_0+Omega1_k)^(1/2);
init1=[ a1_0 ; a1_prim_0 ];
options = odeset('Events',@events,'RelTol',10^(-13),'AbsTol',10^(-15));
 
[t1,y1]=ode45(@syseq1,[t_begin t_final],init1,options);
 
t1=t1/(3600*24*365*10^(9));
P1=polyfit(t1,y1(:,1),2);
disp('P1_bis='),disp(P1);
syms t r theta phi y fact;
fact=1/(1+r^2);
 
f1=poly2sym(P1,t);
 
syms g11 g22 g33 g44;
 
g11=1;
g22=f1^(2)*fact;
g33=f1^(2)*r^(2);
g44=f1^(2)*r^(2)*(sin(theta))^(2);
 
 
%%%%%%%%% première formule pour les symboles de Christoffel %%%%%%%%%%%
 
syms Gamma;
 
 
for i=1:4
    for j=1:4
        for k=1:4
            Gamma(j,i,k)=0;
        end
    end
end
 
Gamma(1,1,1)=0;
Gamma(1,1,2)=0;
Gamma(2,1,1)=0;
Gamma(1,1,3)=0;
Gamma(3,1,1)=0;
Gamma(1,1,4)=0;
Gamma(4,1,1)=0;
 
Gamma(2,2,1)=1/(2*cv)*diff(log(abs(g22)),t);
disp('Gamma(2,2,1)='),disp(Gamma(2,2,1));
Gamma(1,2,2)=1/(2*cv)*diff(log(abs(g22)),t);
Gamma(2,2,2)=0;
Gamma(2,2,3)=0;
Gamma(3,2,2)=0;
Gamma(2,2,4)=0;
Gamma(4,2,2)=0;
 
Gamma(3,3,1)=1/(2*cv)*diff(log(abs(g33)),t);
Gamma(1,3,3)=1/(2*cv)*diff(log(abs(g33)),t);
Gamma(3,3,2)=1/2*diff(log(abs(g33)),r);
Gamma(2,3,3)=1/2*diff(log(abs(g33)),r);
Gamma(3,3,3)=0;
Gamma(3,3,4)=0;
Gamma(4,3,3)=0;
 
Gamma(4,4,1)=1/(2*cv)*diff(log(abs(g44)),t);
Gamma(1,4,4)=1/(2*cv)*diff(log(abs(g44)),t);
Gamma(4,4,2)=1/2*diff(log(abs(g44)),r);
Gamma(2,4,4)=1/2*diff(log(abs(g44)),r);
Gamma(4,4,3)=1/2*diff(log(abs(g44)),theta);
disp('Gamma(4,4,3)='),disp(Gamma(4,4,3));
Gamma(3,4,4)=1/2*diff(log(abs(g44)),theta);
Gamma(4,4,4)=0;
 
%%%%%%%%%%%%%%%%%% deuxième formule %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
Gamma(1,2,1)=0;
Gamma(1,3,1)=0;
Gamma(1,4,1)=0;
 
Gamma(2,1,2)=-1/(2*g11*cv)*diff(g22,t);
Gamma(2,3,2)=0;
Gamma(2,4,2)=0;
 
Gamma(3,1,3)=-1/(2*g11*cv)*diff(g33,t);
Gamma(3,2,3)=-1/(2*g22)*diff(g33,r);
Gamma(3,4,3)=0;
 
Gamma(4,1,4)=-1/(2*g11*cv)*diff(g44,t);
Gamma(4,2,4)=-1/(2*g22)*diff(g44,r);
Gamma(4,3,4)=-1/(2*g33)*diff(g44,theta);
 
%%%%%%%%%%%%%%%%% remplacer cv*t, r, theta, phi par y(1),y(3),y(5),y(7) %%%
 
for i=1:4
    for j=1:4
        for k=1:4
            Gamma(j,i,k)=subs(Gamma(j,i,k),{'t','r','theta','phi'},{'y(1)/cv','y(3)','y(5)','y(7)'});
        end
    end
end
 
 
syms dsec1 dsec2 dsec3 dsec4 y_array yp y1 y2 y3 y4 y5 y6 y7 y8 ;
y_array=[ y1 y2 y3 y4 y5 y6 y7 y8 ];
%yp=[ cv*dt_ds dr_ds dtheta_ds dphi_ds ];
dsec1=0;
dsec2=0;
dsec3=0;
dsec4=0;
 
i=1;
 
for j=1:4
    for k=1:4
    dsec1=dsec1+Gamma(j,i,k)*y_array(2*j)*y_array(2*k);    
    end
end
 
disp('desc1='),disp(dsec1);
 
i=2;
 
for j=1:4
    for k=1:4
    dsec2=dsec2+Gamma(j,i,k)*y_array(2*j)*y_array(2*k);    
    end
end
 
i=3;
 
for j=1:4
    for k=1:4
    dsec3=dsec3+Gamma(j,i,k)*y_array(2*j)*y_array(2*k);    
    end
end
 
i=4;
 
for j=1:4
    for k=1:4
    dsec4=dsec4+Gamma(j,i,k)*y_array(2*j)*y_array(2*k);    
    end
end
 
 
 
dsec1=subs(dsec1,{'cv','y2','y4','y6','y8'},{'3*10^(8)','y(2)','y(4)','y(6)','y(8)'});
dsec2=subs(dsec2,{'cv','y2','y4','y6','y8'},{'3*10^(8)','y(2)','y(4)','y(6)','y(8)'});
dsec3=subs(dsec3,{'cv','y2','y4','y6','y8'},{'3*10^(8)','y(2)','y(4)','y(6)','y(8)'});
dsec4=subs(dsec4,{'cv','y2','y4','y6','y8'},{'3*10^(8)','y(2)','y(4)','y(6)','y(8)'});
 
%disp('desc1_simplif='),disp(vpa(dsec1));
str1=(char(-vpa(dsec1)));
str2=(char(-vpa(dsec2)));
str3=(char(-vpa(dsec3)));
str4=(char(-vpa(dsec4)));
 
str1(isspace(str1))=[];
str2(isspace(str2))=[];
str3(isspace(str3))=[];
str4(isspace(str4))=[];
 
disp('str1='),disp(str1);
 
fid=fopen('ode.txt','w');
fprintf(fid,'%s\n','[ y(2) ;');
fprintf(fid,'%s\n',strcat(str1,';'));
fprintf(fid,'%s\n',' y(4) ;');
fprintf(fid,'%s\n',strcat(str2,';'));
fprintf(fid,'%s\n',' y(6) ;');
fprintf(fid,'%s\n',strcat(str3,';'));
fprintf(fid,'%s\n',' y(8) ;');
fprintf(fid,'%s\n',strcat(str4,';'));
fprintf(fid,'%s\n','] ;');
fclose(fid);
 
 
%ode2=textread('ode.txt','%s','delimiter','\n');
 
 
%disp('ode2='),disp(ode2);
 
 
dydtgeo2=zeros(8,1);
dydtgeo2=[ y(2) ;                        
           str1 ;
           y(4) ;
           str2 ;
           y(6) ;
           str3 ;
           y(8) ;
           str4 ; ] ;
 
 
disp('dydtgeo2='),disp(dydtgeo2);      
 
end
 
 
 
 
function dydt1 = syseq1(t,y) 
 
H0=71000/(3*10^(22));
cv=3*10^(8);
G=6.673*10^(-11);
cv=3*10^(8);
G=6.673*10^(-11);
rho_crit=3*H0^(2)/(8*pi*G);
Omega1_m=0.3;
Omega1_red=0;
Omega1_k=1-Omega1_m-Omega1_red;
 
 dydt1=zeros(2,1);
 dydt1=[ y(2) ;                        
 (-(H0^(2)/2)*(Omega1_m/y(1)^(3)-2*Omega1_red))*Omega1_m*(1/(H0^(2))*y(2)^2-Omega1_k-Omega1_red*y(1)^(2))^(-1);                
       ];
 
 
end
 
 
function dydt2 = syseq2(t,y) 
 
H0=71000/(3*10^(22));
cv=3*10^(8);
G=6.673*10^(-11);
cv=3*10^(8);
G=6.673*10^(-11);
rho_crit=3*H0^(2)/(8*pi*G);
Omega2_m=0.3;
Omega2_red=0.7;
Omega2_k=1-Omega2_m-Omega2_red;
 
 dydt2=[ y(2) ;                        
 (-(H0^(2)/2)*(Omega2_m/y(1)^(3)-2*Omega2_red))*Omega2_m*(1/(H0^(2))*y(2)^2-Omega2_k-Omega2_red*y(1)^(2))^(-1);                
       ];
 
 
end
 
function dydt3 = syseq3(t,y) 
 
H0=71000/(3*10^(22));
cv=3*10^(8);
G=6.673*10^(-11);
cv=3*10^(8);
G=6.673*10^(-11);
rho_crit=3*H0^(2)/(8*pi*G);
Omega3_m=5;
Omega3_red=0;
Omega3_k=1-Omega3_m-Omega3_red;
 
 
 dydt3=[ y(2) ;                        
 (-(H0^(2)/2)*(Omega3_m/y(1)^(3)-2*Omega3_red))*Omega3_m*(1/(H0^(2))*y(2)^2-Omega3_k-Omega3_red*y(1)^(2))^(-1);                
       ];
end
 
function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a 
% decreasing direction and stop integration.
value = y(1);     % Detect height = 0
isterminal = 1;   % Stop the integration
direction = -1;   % Negative direction only
end
Voyez vous à priori d'où pourrait venir cet erreur ?

Merci par avance.