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

 採用された回答

Richard Brown
Richard Brown 2012 年 4 月 23 日

2 投票

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 件のコメント

rana
rana 2012 年 4 月 23 日
what does this mean f(x(:, i-1)
Richard Brown
Richard Brown 2012 年 4 月 23 日
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 2012 年 4 月 23 日
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 2012 年 4 月 23 日
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 2012 年 4 月 23 日
Yes, that should be fine now
rana
rana 2012 年 4 月 23 日
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 2012 年 4 月 23 日
No, the curves are for y and its first derivative. If you only want y, then plot(t, x(1, :))
rana
rana 2012 年 4 月 23 日
i really soooooooo thankfull for you Richard
thanx for your help
Richard Brown
Richard Brown 2012 年 4 月 24 日
No problem, you might want to accept the answer so that others can benefit from it

サインインしてコメントする。

その他の回答 (1 件)

Richard Brown
Richard Brown 2012 年 4 月 22 日

3 投票

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 件のコメント

rana
rana 2012 年 4 月 23 日
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 2012 年 4 月 23 日
and i also can not find these books that you told about
Richard Brown
Richard Brown 2012 年 4 月 23 日
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 2012 年 4 月 23 日
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 !!!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeNumerical Integration and Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by