MATLAB: Solving ode for particle falling under influence of gravity.

In summary, you will need to modify the dydt(2) equation and add prompts for user input in order to solve for a projectile with an angle of 60 degrees and initial velocity of 100m/s, and then calculate and plot the analytical solutions for comparison.
  • #1
adwodon
13
0
Ok so I am a student and we've started doing matlab, unforunately I was ill for the last session so I am currently doing it at home myself. The question is unassessed (we are given a few questions then a final assessed question with each session), so if any solutions could have a brief explanation of where they've come from so I understand what's going on.

The first question of the session (our 3rd session 'solving ode's with MATLAB') is as follows:

Using the following given function (projectile.m):

function dydt=projectile(t,y)
g=9.8;
dydt=zeros(2,1); %a column vector
dydt(1)=y(2);
dydt(2)=-g;
end

and the following commands:
>> [T,Y]=ode45(@projectile,[0 12],[0 10]);
>> plot(T,Y(:,1),'-',T,Y(:,2),'-.')

a) Modify the function so that you now solve differential equations for a projectile that is launched at an angle to the horizontal of 60 degrees with an initial velocity of 100m/s at x=0m and y=0m. Set the calculation span of 20 seconds with increments of 0.1 seconds.

My attempt:
[T,Y]=ode45(@projectile,[0:0.1:20],[0 100]);

I know that this will change the velocity and the time span, but how I do I change the angle, and make sure its at x=0?

b) Include the call to the ODE solver function in a separate m-file script as well as the commands to plot the trajectory of the projectile i.e. y vs x. Use the script to prompt you to input the initial conditions including velocity, angle and intial x an y positions as well as the time interval over which you find the solutions.

My attempt:
As I've not done the first part I've not really started this, I am not sure what the first part is asking but I am guessing for the inputs I just do something along the lines of:

t_initial=input('Time Start = ')
t_increment=input('Time Increment = ')
t_final=input('Time Finish = ')
y0=input('Initial y = ')
v=input('Velocity = ')
[T,Y]=ode45(@projectile,[t_initial:t_increment:t_final],[y0 v]);

Then add the part for angle and initial x when I know what they are.

c) To check your solution, overlay a plot of the x and y positions calculated from the analytical solutions to these equations.

Not sure on this one
Matlab is all pretty new to me so I am having a bit of trouble just understanding what some of the questions are asking.
 
Physics news on Phys.org
  • #2
My main question is how do I modify the function to solve for a projectile with an angle of 60 degrees and initial velocity of 100m/s?For part a) you need to modify the dydt(2) equation to include the angle and initial velocity. The dydt(2) equation is the equation that determines the acceleration in the y direction. The equation should be modified to include the angle and initial velocity as follows:dydt(2)=-g*sin(60)-v0*cos(60);where v0 is the initial velocity. For part b) you will need to add a prompt that asks the user to input the angle and initial velocity, so that the dydt(2) equation can be modified accordingly. You will also need to modify the time interval and initial y position to match the problem parameters. For part c) you will need to calculate the analytical solutions for the x and y positions and then plot them alongside the numerical solutions (using the same x-axis). This will allow you to visually compare the two solutions and check the accuracy of your numerical solution.
 
  • #3
Any help would be appreciated.



Sure, let me try to help you understand and solve these questions. First, let's break down the given function and commands to understand what is happening.

The function "projectile" is defining the differential equations that describe the motion of a projectile under the influence of gravity. The first line of the function sets the value of the acceleration due to gravity (g) to 9.8 m/s^2. The next line creates a column vector (dydt) with two rows, which will store the values of the derivatives of the two variables (position and velocity) with respect to time. The third line sets the first element of the vector (dydt(1)) to be equal to the second element of the input vector (y(2)), essentially setting the derivative of position equal to velocity. The fourth line sets the second element of the vector (dydt(2)) to be equal to the acceleration due to gravity, which will be constant throughout the motion of the projectile.

The "ode45" command is used to solve the differential equations defined in the "projectile" function. The first input is the name of the function, followed by the time interval over which the solution is to be calculated (in this case, from 0 seconds to 12 seconds), and the initial conditions for the position and velocity of the projectile. The output of this command is two vectors, T and Y, which contain the time values and corresponding values of position and velocity at those times, respectively. The "plot" command is then used to plot the position (Y(:,1)) and velocity (Y(:,2)) as a function of time (T).

Now, let's move on to solving the questions.

a) To modify the function for a projectile launched at an angle of 60 degrees, we need to take into account the horizontal and vertical components of the initial velocity. We can do this by using trigonometry and defining the initial velocity in terms of its components (x and y). The modified function would look like this:

function dydt=projectile(t,y)
g=9.8;
dydt=zeros(2,1); %a column vector
dydt(1)=y(3); %the third element of y is the horizontal component of velocity
dydt(2)=y(4); %the fourth element of y is the vertical component of velocity
dydt(3)
 

Related to MATLAB: Solving ode for particle falling under influence of gravity.

1. What is MATLAB?

MATLAB is a high-level programming language and interactive environment used for scientific computing, data analysis, and algorithm development.

2. What is an ODE?

An ODE (ordinary differential equation) is a mathematical equation that describes how a variable changes with respect to another variable. In this case, the ODE describes the particle's position and velocity as it falls under the influence of gravity.

3. How does MATLAB solve ODEs?

MATLAB has built-in functions for solving ODEs, such as the ode45 function which uses a Runge-Kutta method to numerically solve the ODE.

4. What is the significance of solving the ODE for a particle falling under gravity?

Solving the ODE for this scenario allows us to predict the position and velocity of the particle at any given time, which can be useful in understanding the motion of objects under the influence of gravity and in designing systems that involve such motion.

5. Are there any limitations to using MATLAB for solving ODEs?

While MATLAB is a powerful tool for solving ODEs, it may not be the best choice for certain complex or highly specialized problems. In these cases, other software or numerical methods may be more suitable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
910
  • Engineering and Comp Sci Homework Help
Replies
1
Views
971
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
452
Back
Top