runge-kutta
10 views (last 30 days)
Show older comments
hello i have this equation y''+3y'+5y=1 how can i solve it by programming a runge kutta 4'th order method ? i know how to solve it by using a pen and paper but i can not understand how to programe it please any one can solve to me this problem ? i dont have any idea about how to use ODE and i read the help in matlab but did not understand how to solve this equation please any one can solve this and help me with it ? thanx
0 Comments
Accepted Answer
Richard Brown
on 23 Apr 2012
Writing a new answer so that I can use markup (MathWorks, please fix this!!). You need a couple of things - first, the time values
t = 0:h:100
You can now work out how many steps you need:
n = numel(t)
Now, we need an array in which to store your results: each column should correspond to a value from your t vector
x = zeros(2, n)
We know the initial condition, so we'll pop that in
x(:, 1) = [0; 0]
Now we need a loop to store the subsequent x values. The basic structure will look like this
for i = 2:n % We've already filled in the first column
k1 = h*f(x(:, i-1));
k2 = ...
k3 = ...
k4 = ...
% Now define the new x vector based on k1, ... , k4
x(:, i) = x(:, i-1) + ...
end
% Plot your results
plot(t, x)
And that should pretty much be all you need
9 Comments
Richard Brown
on 24 Apr 2012
No problem, you might want to accept the answer so that others can benefit from it
More Answers (1)
Richard Brown
on 22 Apr 2012
I'll give you the high level overview, you'll need to write the code though
- Turn it into a system of two first order equations (define a new variable z = y')
- Find a decent pseudocode representation of the algorithm, either from your lecture notes or from e.g. section 8.3 of Kincaid and Chaney, "Numerical Analysis", or Algorithm 5.2 of Burden and Faires, "Numerical Analysis", both of which are readily available introductory books that should be in your library.
- Try and code it up in MATLAB
- Report back if you have any problems
4 Comments
Richard Brown
on 23 Apr 2012
You've defined your system correctly, however I suggest you use a function, because you're going to need to evaluate it at many different x values, which can get confusing. Something like
f = @(x) [x(2); -3*x(2) - 5*x(1) + u];
You can then call f(x) (where x is a 2-element vector) to get your derivatives (also as a vector).
Wikipedia also has the basic RK4 here <http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods#Common_fourth-order_Runge.E2.80.93Kutta_method>, you should be able to use that pretty much the way it is written ...
See Also
Categories
Find more on Calculus 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!