How to enter this program in matlab?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
James Tursa
2014년 10월 24일
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:
답변 (3개)
Rick Rosson
2014년 10월 25일
Here's a start:
g = 9.81;
v0 = 5;
phi = ...
x = 0;
y = 0;
while y > 0
...
...
end
Rick Rosson
2014년 10월 25일
편집: Rick Rosson
2014년 10월 25일
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
Guillaume
2014년 10월 25일
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).
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!