System of ODE: derivative inside equation
1 view (last 30 days)
Show older comments
Hi. I have to solve this system of ODE:
How should I treat x'_3 in the second equation? I tried two different approaches but I get different results:
function equazione()
[t,x] = ode45(@(t,x) F(t,x), [0, 3], [2, 1, 1]);
plot(t,x(:,1))
end
function xp = F(t,x)
xp = zeros(3,1);
xp(1) = x(2);
xp(2) = -t * x(1) - exp(t) * x(2) + 3 * cos(2 * t) + xp(3);% <- See this last term
xp(3) = exp(t);
end
and
function equazione()
[t,x] = ode45(@(t,x) F(t,x), [0, 3], [2, 1, 1]);
plot(t,x(:,1))
end
function xp = F(t,x)
xp = zeros(3,1);
xp(1) = x(2);
xp(2) = -t * x(1) - exp(t) * x(2) + 3 * cos(2 * t) + exp(t);% <- See this last term
xp(3) = exp(t);
end
Red line: 1st approach Blue line: 2nd approach
%
1 Comment
Torsten
on 24 Feb 2017
The second approach is the correct one.
In the first approach, you only add 0 to -t * x(1) - exp(t) * x(2) + 3 * cos(2 * t) because xp is preallocated as zeros(3,1). This is obvioulsy incorrect.
Best wishes
Torsten.
Answers (0)
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!