What is wrong with my code?

% Itfinds the velocity using RK 4 method
clc; % Clears the screen
clear all;
F = @(t,v) 9.81 - ((0.28/85) * (v^2));
t0 = input('Insert the value of the initial time: ');
v0 = input('Insert the value of the initial velocity: ');
tfinal = 20;
h = input('Insert the value of the step size: ');
v = RK_4(F,t0,h,tfinal,v0);

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 25 日

0 投票

You need to write the RK_4 function. Right now, RK_4 is the name of your current script. You can find one implementation of the Runge Kutta-4 algorithm here: https://www.mathworks.com/matlabcentral/fileexchange/29851-runge-kutta-4th-order-ode

4 件のコメント

Ibrahim Butt
Ibrahim Butt 2020 年 10 月 25 日
Can you please tell me what is wrong with my code now?
% It finds the velocity using RK 4 method
clc; % Clears the screen
clear all;
h = input('Insert the value of the step size: '); % step size
t = 0:h:20; % Calculates till v(20)
v = zeros(1,length(t));
v(1) = input('Insert the value of the initial velocity: '); % initial condition
F_xy = @(v,t) 9.81 - ((0.28/85) * (v^2));
for i=1:(length(t)-1) % calculation loop
k_1 = F_xy(t(i),v(i));
k_2 = F_xy(t(i)+0.5*h,v(i)+0.5*h*k_1);
k_3 = F_xy((t(i)+0.5*h),(v(i)+0.5*h*k_2));
k_4 = F_xy((t(i)+h),(v(i)+k_3*h));
v(i+1) = v(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h % main equation
end
Ameer Hamza
Ameer Hamza 2020 年 10 月 25 日
In function handle
F_xy = @(v,t) 9.81 - ((0.28/85) * (v^2));
You have specified v as first input and t as second. But inside the for-loop, you are doing the opposite. Apart from that, your equations seems correct.
Ibrahim Butt
Ibrahim Butt 2020 年 11 月 3 日
Thank you very much for helping me in correcting my mistakes and clearing my doubts.
Ameer Hamza
Ameer Hamza 2020 年 11 月 4 日
I am glad to be of help!!!

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2020 年 10 月 25 日

コメント済み:

2020 年 11 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by