runge-kutta

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

 Accepted Answer

Richard Brown
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

rana
rana on 23 Apr 2012
what does this mean f(x(:, i-1)
Richard Brown
Richard Brown on 23 Apr 2012
evaluate the function f, that I defined in a comment on my previous answer, using the i-1th column of the matrix x
rana
rana on 23 Apr 2012
ok .. see this now .. is it true :
clc
clear all
x1=0;
x2=0;
u=1;
h=0.1;
x1=y(t);
dx1=x2;
dx2=(-3*x2)-(5*x1)+u;
t=0:h:100;
n = numel(t);
x = zeros(2, n);
x(:, 1) = [0; 0];
f = @(x) [x(2); -3*x(2) - 5*x(1) + u];
for i = 2:n % We've already filled in the first column
k1 = h*f(x(:, i-1));
k2 = h*f(x(:,i-1)+0.5*k1);
k3 = h*f(x(:,i-1)+0.5*k2);
k4 = h*f(x(:,i-1)+k3);
x(:, i) = x(:, i-1) + ((1/6)*(k1+(2*k2)+(2*k3)+k4)*h);
end
plot(t, x)
rana
rana on 23 Apr 2012
there was am error with these steps
so i delet them
%x1=y(t);
%dx1=x2;
%dx2=(-3*x2)-(5*x1)+u;
Richard Brown
Richard Brown on 23 Apr 2012
Yes, that should be fine now
rana
rana on 23 Apr 2012
yeeeeeeeees it woooooorks
but only one problem still there ... the figure ploted having two curves ... one for the response and the other i think it is for (error) .... how can i cancel this error curve and only the response curve is occure ?
Richard Brown
Richard Brown on 23 Apr 2012
No, the curves are for y and its first derivative. If you only want y, then plot(t, x(1, :))
rana
rana on 23 Apr 2012
i really soooooooo thankfull for you Richard
thanx for your help
Richard Brown
Richard Brown on 24 Apr 2012
No problem, you might want to accept the answer so that others can benefit from it

Sign in to comment.

More Answers (1)

Richard Brown
Richard Brown on 22 Apr 2012

3 votes

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 Comments

rana
rana on 23 Apr 2012
ok thanx richard
that is what i did
clc
clear all
x1=0;
x2=0;
u=1;
h=0.1;
x1=y(t);
dx1=x2;
dx2=(-3*x2)-(5*x1)+u;
for i=1:h:100
t=i+h;
plot(t,x1)
next i
end
the problem now how to enter the values of k's to the program ?
and does my steps true or not ?
thanx for your help ... may be i will bather you alittel because i am not profitional in matlab
rana
rana on 23 Apr 2012
and i also can not find these books that you told about
Richard Brown
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 ...
rana
rana on 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 !!!

Sign in to comment.

Categories

Find more on Numerical Integration and 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!