Info

この質問は閉じられています。 編集または回答するには再度開いてください。

What is wrong with my code?

1 回表示 (過去 30 日間)
Ibrahim Butt
Ibrahim Butt 2020 年 10 月 25 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
% 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

回答 (2 件)

Cris LaPierre
Cris LaPierre 2020 年 10 月 25 日
Nothing. It's doing exactly what you've told it to do.
You specified a stepsize of 0 (variable h).
This causes t to be an empty vetor.
t=0:0:20
t = 1×0 empty double row vector
This causes your for loop to never run, since 1:(length(t)-1) simplifies to 1:-1.
  1 件のコメント
Ibrahim Butt
Ibrahim Butt 2020 年 11 月 3 日
Thank you for helping me.

Alan Stevens
Alan Stevens 2020 年 10 月 25 日
Your code works if you put in sensible numbers for step size and velocity.
Zero is not sensible!
  1 件のコメント
Ibrahim Butt
Ibrahim Butt 2020 年 11 月 3 日
Thank you for helping me.

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by