Filter löschen
Filter löschen

runge-kutta

10 Ansichten (letzte 30 Tage)
rana
rana am 22 Apr. 2012
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

Akzeptierte Antwort

Richard Brown
Richard Brown am 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 Kommentare
rana
rana am 23 Apr. 2012
i really soooooooo thankfull for you Richard
thanx for your help
Richard Brown
Richard Brown am 24 Apr. 2012
No problem, you might want to accept the answer so that others can benefit from it

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Richard Brown
Richard Brown am 22 Apr. 2012
I'll give you the high level overview, you'll need to write the code though
  1. Turn it into a system of two first order equations (define a new variable z = y')
  2. 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.
  3. Try and code it up in MATLAB
  4. Report back if you have any problems
  4 Kommentare
Richard Brown
Richard Brown am 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 ...
rana
rana am 23 Apr. 2012
thanx
about the basic.. i know it and i know how to solve runge kutta by using its equ. but my problem to to programe these equ. in matlab
and about using function what did you mean ? i did not get that very well !!!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by