필터 지우기
필터 지우기

How to enter this program in matlab?

조회 수: 1 (최근 30일)
user1996
user1996 2014년 10월 24일
답변: user1996 2014년 10월 26일
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
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:
user1996
user1996 2014년 10월 25일
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.

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

답변 (3개)

Rick Rosson
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
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
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).
Rick Rosson
Rick Rosson 2014년 10월 25일
Yes, good point.

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


user1996
user1996 2014년 10월 26일
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.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by