A code and plot a graph for a projectile motion of a ball

조회 수: 13 (최근 30일)
TANAMAY KOTHARI
TANAMAY KOTHARI 2021년 6월 2일
편집: James Tursa 2021년 6월 11일
How do I write a code and plot a graph for a projectile motion of a ball thrown at 60 deg with a speed of 50 m/s using ode and runge kutta method.
Somebody please help
  댓글 수: 6
TANAMAY KOTHARI
TANAMAY KOTHARI 2021년 6월 2일
% x''=0, x(0)=0, x'(0)=Vx
% y''=-g, y'(0)=Vy, y(0)=0
clc
clear all
% constants
g=9.81; %gravity acc
v=50; % speed in m/s
angle= 60;%in deg
Vx=v*cosd(angle);
Vy=v*sind(angle);
h=0.1;
t(1)=0;
x(1)=0;
y(1)=0;
f=@(t,x,y) Vx*t;
g=@(t,x,y) Vy*t-0.5*g*t^2;
for i=1:100;
l1=h*f(t,x);
k1=h*g(t,y);
l2=h*f(t+h/2,x+l1/2);
k2=h*g(t+h/2,y+k1/2);
l3=h*f(t+h/2,x+l2/2);
k3=h*g(t+h/2,y+k2/2);
l4=h*f(t+h,x+l3);
k4=h*g(t+h,y+k3);
x(i+1)=x(i)+(l1+2*l2+2*l3+l4)/6;
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)/6;
t(i+1)=t(i)+h;
fprintf('i=,t=,x=,y=\n',i,t,x,y)
plot(x,y)
end
This is the code I was able to come up, but the plot that appears comes blank.
Scott MacKenzie
Scott MacKenzie 2021년 6월 2일
You state that "the plot that appears comes blank". But, actually, there is no plot generated because the plot function never executes. You've got bugs in your code. Good luck.
BTW, you might want to have a look here -- a related question.

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2021년 6월 11일
편집: James Tursa 2021년 6월 11일
Your main problem is that you don't have enough state variables. You have these two equations:
x'' = 0
y'' = -g
That's two 2nd order equations, so you need to have 2 * 2 = 4 state variables. The state variables you need to integrate are x, y, Vx, Vy. But you only integrate two of these state variables, x and y. You have no code for integrating Vx and Vy. You need to add code for integrating Vx and Vy.
You also are using the solution of x and y integration as the derivative functions, which is incorrect. For a Runge-Kutta method, just use the derivatives. The code would be patterned after:
x' = Vx
y' = Vy
Vx' = x'' = 0
Vy' = y'' = -g
You also use g for both gravity and a function handle, so to avoid confusion you might want to change one of these.

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by