필터 지우기
필터 지우기

How to run a loop in linear differential equations in symbolic atmosphere?

조회 수: 1 (최근 30일)
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.

채택된 답변

Star Strider
Star Strider 2014년 5월 10일
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
Learner
Learner 2014년 5월 11일
Many many thanks for your help and support. :)
It is working in a same way as I wanted, thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by