My problem is it runs forever !
This is Runge Kutta 4 method.
Thanks for helping me !!!
syms t1
syms y1
f = 20*t1 - y1*t1/10 - 0.1*y1^2;
t = [ 0 ]; % declare array t with t(1) = 0
y = zeros(1,11,'sym'); % declare array y with y(1) = 0
h = 0.1;
t1 = 1;
n = (t1-t(1))/h; % number of times need to caculate
% create multy t from t1 to t(n+1)
for i = 1:n
t(i+1) = t(i) + h;
end
% Runge Kutta formular
for i=1:n
k1 = subs(subs(f,t1,t(i)),y1,y(i));
k2 = subs(subs(f,t1,t(i)+0.5*h),y1,y(i)+0.5*k1);
k3 = subs(subs(f,t1,t(i)+0.5*h),y1,y(i)+0.5*k2);
k4 = subs(subs(f,t1,t(i)+h),y1,y(i)+k3);
y(i+1) = y(i) + (h/6)*(k1+2*k2+2*k3+k4);
end
disp(y(11));

1 件のコメント

KSSV
KSSV 2018 年 12 月 24 日
YOu need to rethink on your code......why you want to use syms?

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

回答 (1 件)

madhan ravi
madhan ravi 2018 年 12 月 24 日
編集済み: madhan ravi 2018 年 12 月 24 日

1 投票

Just download the file exchange below and adapt it to your need :https://www.mathworks.com/matlabcentral/fileexchange/29851-runge-kutta-4th-order-ode

1 件のコメント

madhan ravi
madhan ravi 2018 年 12 月 24 日
If my answer helped you solve the question make sure to accept the answer so that people know the question is solved.

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

カテゴリ

ヘルプ センター および File ExchangeDebugging and Improving Code についてさらに検索

タグ

質問済み:

2018 年 12 月 24 日

コメント済み:

2018 年 12 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by