Runge-Kutta 2

3 ビュー (過去 30 日間)
Nagy Csaba Norbert
Nagy Csaba Norbert 2021 年 4 月 21 日
コメント済み: Nagy Csaba Norbert 2021 年 4 月 22 日
Hi, i have a homwork for school including the Runge-Kutta 2.
I have to do it in symbolic, i would be very greatfull if someone can help me in this taks.
intervalmin = 0;
intervalmax = 1;
h1 = 0.1;
g = 0.02;
X0 = [0, 0];
[t1, x1] = fuggveny(intervalmin, X0(1), X0(2), h1, intervalmax);
[t2, x2] = fuggveny(intervalmin, X0(1), X0(2), g, intervalmax);
syms x(t);
dz = diff(x,t);
ode = diff(x,t,2) + 5.*diff(x,t,1) + 4.*x(t) == 3 - 2.*t - t.^2;
cond1 = x(0) == 1;
cond2 = dz(0) == 1;
RK2(t) = dsolve(ode,cond1,cond2);
plot(t1, x1, '-y');
hold on;
plot(t2, x2, '--r');
hold on;
fplot(RK2,'-*b');
hold on
legend('h=0.1','h=0.02','ode')
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on;
function dX = f(t, x1, x2)
X1 = x2;
X2 = -5.*x2 + 4.*x1 + 3 - 2.*t - t.^2;
dX = [X1, X2];
end
function [t, x] = fuggveny(intervalmin, X0_1, X0_2, h, intervalmax)
t = (intervalmin:h:intervalmax);
X1 = zeros(size(t));
X2 = zeros(size(t));
X1(1) = X0_1;
X2(1) = X0_2;
for i = 1:1:length(t) - 1
k1 = f(t(i), X1(i), X2(i));
k2 = f(t(i) + h/2, X1(i) + h/2 * k1(1), X2(i) + h/2 * k1(2));
k3 = f(t(i) + h, X1(i) - h*k1(1) + 2*h*k2(1), X2(i) - h*k1(2) + 2*h*k2(2));
X1(i + 1) = X1(i) + h/6 * (k1(1) + 4*k2(1) + k3(1));
X2(i + 1) = X2(i) + h/6 * (k1(2) + 4*k2(2) + k3(2));
end
x = X1;
end

採用された回答

James Tursa
James Tursa 2021 年 4 月 21 日
編集済み: James Tursa 2021 年 4 月 21 日
The + 4.*x1 should be - 4.*x1 in your derivative function. Also, using both X1 and x1 and X2 and x2, even though MATLAB is case sensitive and these are different variables, is confusing to read. Suggest using different names such as x1dot and x2dot, or even just form the derivative vector directly. E.g.,
function dX = f(t, x1, x2)
dX = [x2; -5.*x2 - 4.*x1 + 3 - 2.*t - t.^2];
end
Also, just to clarify, you are asked to solve this both symbolically and numerically?
Not sure why the assignment says Runge-Kutta II when it looks like you are using a 3rd order method.
  3 件のコメント
James Tursa
James Tursa 2021 年 4 月 22 日
Are you getting errors? The output is not what you expected? Or ...?
Nagy Csaba Norbert
Nagy Csaba Norbert 2021 年 4 月 22 日
I don't get any error messages. The problem is that, i don't know what the output should to be look like. My teacher noticed my work with that comment, "there are too many X s in my task.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by