How to use if statement in ode45? With one value changing w.r.t time.
1 view (last 30 days)
Show older comments
I have a 1st order differential equation in which 1 value is constant and 1 is changing w.r.t to time.
dp/dt= q/c
where c =55
q=600 for 0 to 1 second and q=0 for 1 to 2 second.Total time is 2 sec.
I think to use if statement but not sure how to implement it with time. I have made a ode45 function program in matlab but the result of graph is linear directly proportional.
function dq = ode45(t,y)
q=600;c=55;
dq=q/c;
end
0 Comments
Answers (1)
Jan
on 27 Dec 2015
Edited: Jan
on 27 Dec 2015
No, do not integrate a non-smooth function, see http://www.mathworks.com/matlabcentral/answers/59582#answer_72047 .
Note: Do not call the function to be integrated "ode45", because this is the function name of the compiler.
A numerical correct approach:
q = 600;
y0 = 3.1415; % Your initial value
fcn = @(t,y) yourFcn(t, y, q);
[T1, Y1] = ode45(fcn, [0, 1], y0);
q = 0;
y0 = Y1(end, :);
fcn = @(t,y) yourFcn(t, y, q);
[T2, Y2] = ode45(fcn, [1, 2], y0);
T = cat(1, T1(1:end-1), T2); % The time t=1 occurs twice
Y = cat(1, Y1(1:end-1, :), Y2);
And:
function dq = yourFcn(t,y,q)
c = 55;
dq = q/c;
end
1 Comment
MG
on 14 Oct 2020
Thanks a lot . This algorithom solved my three DOF problem ( six first order ODEs) with three different conditions
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!