How to run a loop in linear differential equations in symbolic atmosphere?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Learner
am 10 Mai 2014
Kommentiert: Star Strider
am 11 Mai 2014
Hi,
I have 6 symbolic functions i.e. y1 - y3 and c1 - c3, which I have used for defining linear ODE's.
The linear OD equations are as follows:
dy1/dt = (-c1*y1)+(c2*y2);
dy2/dt = (c3*y1)+(c2*y2)+(c3*y3);
dy3/dt = (c1*y1)+(c5*y2)+(c3*y3).
Now I want to define c's as constant values between two limits, i.e. c1=0.1 to 0.9; c2= 1 to 2.2 and c3=10 to 100.
Now what I want, to see effect of these equations with different constant values (c's), i.e. in loop form.
That is, I want to do two things:
1. Change c1 (from 0.1 to 0.9) and keep c2=1 and c3=10;
2. Change c1 (from 0.1 to 0.9) as well as c2 (from 1 to 2.2) and c3 (from 10 to 100).
And see values of y1, y2 and y3 in workspace or graph.
-----------------------------------------------------
I tried many times but all the time I get new type of error!! This time it is "In an assignment A(I) = B, the number of elements in B and I must be the same."!!
Please help me out. Thanks in advance.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 10 Mai 2014
The Symbolic Toolbox is not the best way to solve your problem. Use the numerical ODE solvers instead.
This will work, although you didn’t say anything about c5, so I assigned it a value of 1 to test the loops. I tested these to be sure they’d work, but I didn’t take the time to let the second loop complete. Be sure that the ‘tspan’ vector has the same length for all runs in a loop.
fun1 = @(t,y,c1,c2,c3,c5) [((-c1*y(1))+(c2*y(2))); ((c3*y(1))+(c2*y(2))+(c3*y(3))); ((c1*y(1))+(c5*y(2))+(c3*y(3)))];
% Loop #1:
T0 = clock;
tspan = linspace(0, 1, 25);
c1 = linspace(0, 0.9, 10);
c2 = 1;
c3 = 10;
c5 = 1;
for k1 = 1:length(c1)
[t,y] = ode45(@(t,y) fun1(t,y,c1(k1),c2,c3,c5), tspan, ones(1,3)*eps);
ty1(k1,:,:) = [t y];
end
T1 = clock;
% Loop #2:
tspan = linspace(0, 1, 25);
c1 = linspace( 0, 0.9, 10);
c2 = linspace( 1, 2.2, 10);
c3 = linspace(10, 100, 10);
c5 = 1;
for k1 = 1:length(c1)
for k2 = 1:length(c2)
for k3 = 1: length(c3)
[t,y] = ode45(@(t,y) fun1(t,y,c1(k1),c2(k2),c3(k3),c5), tspan, ones(1,3)*eps);
ty2(k1,k2,k3,:,:) = [t y];
end
end
end
T2 = clock;
ts = datestr(now, 'yyyymmdd-HHMMSS');
save(['LearnerODEmtxs_' ts '.mat'], 'ty1', 'ty2', 'T1', 'T2', 'T3')
This looks like it’s going to take a while, so I included a couple timers and a ‘save’ statement to store the results so you won’t have to run them again unless you change the conditions. The filename is unique for each run, and incorporates the date and time the file was created.
4 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!