Friday, June 29, 2012

MATLAB - Projectile motion by Euler's method


gravity
%Projectile motion
%By Mahesha MG
v0= input('Initial velocity(m/s): ');
a= input('Angle of projection(in degrees): ');
h= input('Time step (in seconds): ');

a=a*pi/180;
g=9.8;  %acceleration due to gravity in m/s^2
xmax=v0^2*sin(2*a)/g;
ymax=v0^2*sin(a)^2/(2*g);
td=2*v0*sin(a)/g;           %total time
x1=0;
y1=0;
figure('color','white');
for t=0:h:td+h
    x=v0*t*cos(a);          %analytic solution for x
    y=v0*t*sin(a)-g*t^2/2;  %analytic solution for y

    plot(x,y,'r*',x1,y1,'bo')
    hold all
    xlim([0 1.1*xmax]);
    ylim([0 1.1*ymax]);
    title('Projectile Motion By Mahesha MG');
    xlabel('Distance in meter');
    ylabel('Height in meter');
    getframe;
    x1=x1+h*v0*cos(a);      %Euler's method for x

    y1=y1+h*(v0*sin(a)-g*t);%Euler's method for y
end
Published with MATLAB® 7.13


(a) Time step (h) = 0.04s

(b) Time step (h) = 0.02s

(c) Time step (h) = 0.01s

(d) Time step (h) = 0.001s

4 comments:

  1. seeing is believing...visualization makes the difference..

    ReplyDelete
  2. Very nice, thank you. I will show it to my students this week ...

    ReplyDelete
  3. func = inline('x^4-11*x+8');
    dfunc = inline('4*x^3-11');
    root = NewRap(func,dfunc,1.8 ,2, 1.0e05)
    % newton raphson method for finding a root of f(x)=0
    % USAGE: root = NewRap(func,dfunc,a,b.tol)
    % INPUT
    % func = handle of function that returns f(x)
    % dfunc = handle of function that returns f'(x)
    % a,b = brackets (limits) of the root
    % tol = error tolerance (default is 1.0e6*eps)
    % OUTPUT
    % root = zero of f(x) (root = NaN if there is no convergence)
    if nargin < 5; tol = 1.0e6*eps;end
    fa = feval(func,a); fb = feval(func,b);
    if fa == 0; root = a; return; end
    if fb ==0; root = b; return; end
    if fa*fb> 0.0
    error('Root was not bracketed in the interval(a,b)')
    end
    x = (a+b)/2.0;
    for i = 1:30
    fx feval(func,x);
    if abs(fx) < tol; root = x; return; end
    % Tighten brackets
    if fa*fx < 0.0; b = x;
    else a = x;
    end
    % Newton- Raphson method
    dfx = feval(dfunc,x);
    if abs(dfx) == 0; dx = b-a;
    else dx = -fx/dfx;
    end
    x = a + dx;
    end
    % Convergance check
    if abs(dx)< tol*max(b,1.0)
    root = x; return
    end


    Dear Sir,
    kindly run this program, this program is not running.

    ReplyDelete
    Replies
    1. Hi
      I didn't get the logic adopted here. Is it by Newton-Raphson method?
      If so, go to following link.
      http://numericalcomputation.blogspot.in/2013/03/matlab-newton-raphson-method.html

      Delete