How to enter this program in matlab?

1 view (last 30 days)
user1996
user1996 on 24 Oct 2014
Answered: user1996 on 26 Oct 2014
Projectile motion equations describe: X= v0* cos(fi)*t Y= v0*sin(fi)*t-0,5*g*t Where g = 9.81 m / s2. Create a program that graphically shows the trajectory of the projectile entered the value of the angle and the initial speed of 5 m / s. (The calculation by adding the condition that the calculation ends if y <= 0 If !. plot function call in each step of the loop, it is desirable to define the fixed before the loop spans the axes)
  2 Comments
James Tursa
James Tursa on 24 Oct 2014
What have you done so far? Typically, you would make an attempt and post your code so we can comment on it and suggest corrections and improvements. See this link:
user1996
user1996 on 25 Oct 2014
I'm sorry. this is what i have entered: Clc clear all g=9.81; v0=5; (Fi)= input('Enter the value of the angle:'); t0=v0*sin(fi)/g; while y>0; if y<=0 break; X=v0*t*cos(fi); Y=v0*t*sin(fi)-g*t^2/2 End Plot(X,Y); Title('projetile motion'); xlabel('range X(m)); ylabel('height Y(m)); End I don't know which loop should I use. Because the first time I used For j=0:100; i=j+1; t(i)=j*t0/100; Then i realised that was wrong beacasue I haven't used condition y<=0 Thank you for your time to answer me.

Sign in to comment.

Answers (3)

Rick Rosson
Rick Rosson on 25 Oct 2014
Here's a start:
g = 9.81;
v0 = 5;
phi = ...
x = 0;
y = 0;
while y > 0
...
...
end

Rick Rosson
Rick Rosson on 25 Oct 2014
Edited: Rick Rosson on 25 Oct 2014
Your original MATLAB code
Here is the code as you posted it with a bit of improved formatting to make it more readable:
Clc
clear all
g=9.81;
v0=5;
(Fi)= input('Enter the value of the angle:');
t0=v0*sin(fi)/g;
while y>0;
if y<=0 break;
X=v0*t*cos(fi);
Y=v0*t*sin(fi)-g*t^2/2
End
Plot(X,Y);
Title('projetile motion');
xlabel('range X(m));
ylabel('height Y(m));
End
A few issues with your code
1. MATLAB is case-sensitive. Most keywords, commands, and built-in functions must be lowercase. So, in your code, please change:
  • Clc to clc
  • End to end
  • Plot to plot
  • Title to title
2. Please delete the semicolon at the end of the while statement. It is not necessary and may cause an error.
3. The input arguments that you are passing to xlabel and ylabel are supposed to be single-quoted strings, but they are missing the closing quotation mark. All strings must start and end with a single quote character.
4. Please delete the very last line of code. It is a simple end statement, but it appears to be unnecessary, and it will cause an error.
5. You have used the variable t inside the while loop but you have never defined or initialized t. Please define and initialize t before you use it. You will also want to increment the value of t during each iteration of the loop.
6. Likewise, please define and initialize y before the while loop, which uses y to evaluate the logical condition that controls when the loop terminates.
If you make all of these changes to your code, I believe the code will run in MATLAB. Although you have a very good start, you will need to add a few things to get the correct answer. You are almost there. Keep going.
  2 Comments
Guillaume
Guillaume on 25 Oct 2014
In addition, the line
if y<=0 break;
is unnecessary since inside the body of the while loop y is guaranteed to be >0 (otherwise the loop wouldn't be entered in the first place).

Sign in to comment.


user1996
user1996 on 26 Oct 2014
Thank you for your time, I will try to find what I am missig out. Yes I know that I don't need the conditon y<=0 when I have while y>0. I just put it because that stands in the task. Again thnaks.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!