solve a system of ODE
2 views (last 30 days)
Show older comments
adriano romano
on 4 Feb 2016
Commented: adriano romano
on 4 Feb 2016
Hi, this is explicit Euler method to solve a ODE
function [x,t]=euleroesplicito(fcn,t0,x0,h,tend) n = fix((tend-t0)/h)+1; t = linspace(t0,t0+(n-1)*h,n); x = zeros(n,1); x(1) = x0; for i = 2:n x(i) = x(i-1) + h*feval(fcn,t(i-1),x(i-1)); end plot(t,x);
How can I extend this method to solve a system of two or more equations? Thank you.
0 Comments
Accepted Answer
Are Mjaavatten
on 4 Feb 2016
function [t,x]=euleroesplicito(fcn,t0,x0,h,tend)
n = fix((tend-t0)/h)+1;
t = linspace(t0,t0+(n-1)*h,n);
x = zeros(n,length(x0));
x(1,:) = x0;
for i = 2:n
x(i,:) = x(i-1,:) + h*feval(fcn,t(i-1),x(i-1,:))';
end
end
Note that I have reversed the sequence of outputs to make it conform to Matlab's ODE standard. Test on the equatiion for free fall:
g = -9.81;
[t,x] = euleroesplicito(@(t,x) [x(2);g],0,[10;0],0.01,1);
plot(t,x(:,1));
More 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!