필터 지우기
필터 지우기

Need help with coding problem!

조회 수: 5 (최근 30일)
Michael Bortolot
Michael Bortolot 2018년 2월 22일
댓글: Walter Roberson 2018년 2월 22일
The question asks:
The ideal motion of a simple single-stage rocket can be modeled as follows:
When thrust is being produced, the velocity of the rocket after it is launched from a stationary position is given by
( ) ( ) [Eq. 1]
where is the specific impulse in seconds (a function of the chemical propellant type), is the gravitational constant, is the initial mass of the rocket (structure and fuel) and ( ) is the mass of the rocket at time t. The mass of the rocket at time t can be determined based on the rate at which the fuel is burned ( , in units of mass/second)
( ) .
Once all of the fuel is burned, the rocket acts as a simple projectile (v = vo – gt)
Table 1 gives the parameters for the problem. Use these parameters and the equations above to calculate and plot the mass, velocity, and height of the rocket as a function of time. Also print the burn time (occurs at maximum velocity). Total Mass (Rocket + Fuel) 0.4 kg Mass of Fuel 35.8 g Burn Rate 12.78 g/s ISP 200 s Table 1: Model rocket parameters
Before beginning to write your script program, produce pseudocode for the problem. You will need to submit your pseudocode in addition to your MATLAB code.
Hints: 1.) Choose a time increment t of .001 s. 2.) Create vectors of mass, velocity, and height as you increment time. Use the same index variable for all of these. 3.) Use a while-loop to indicate that while there is still fuel, Eq. 1 should be used. Within that loop, increase your index variable, and calculate mass, time, velocity, and height. These will be discretized based on the time increment. 4.) Use a second while-loop to calculate these quantities after fuel is expired. 5.) Use the “subplot” function to create the 3x1 plot.
Note: There are many ways to do this problem. It is recommended that you use while-loops here specifically for practice.
so far here is my code:
b=.01278 %rate of fuel being burned - in kg/s
i=200 %specific impulse
m0=.4 %initial weigh in kg
g=9.8
t=(0:.001:40)
mT=m0-b*t
fuel=find(mT>.3642)
fuelindex=mT(fuel)
nofuel=find(mT<.3642)
nofuelindex=mT(nofuel)
x=0
while x<=fuel
x=x+1
h=t-.5*g.*t.^2
v= i.*g.*log(m0./fuelindex) - g.*t
end
x=0
while x<=nofuel
x=x+1
h=t-.5*g.*t.^2
v=-g*t
end
it is providing me with the error,
Matrix dimensions must agree.
Error in Untitled2 (line 22)
v= i.*g.*log(m0./fuelindex) - g.*t
PLEASE HELP
  댓글 수: 1
Jan
Jan 2018년 2월 22일
You simply copied your homework assignment by copy&paste and do not care, that parts like "position is given by ( ) ( ) [Eq. 1]" and "( ) . " are complete nonsense. This is not neat in a forum.

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

답변 (2개)

Walter Roberson
Walter Roberson 2018년 2월 22일
while x<=fuel
but your x is a scalar and fuel is a vector. x<=fuel is a vector. When you if or while a vector, then the test is considered true only if all entries tested are true, the same as
while all(x<=fuel)
That is going to lead you to problems.
Also in the loop
while x<=fuel
x=x+1
h=t-.5*g.*t.^2
v= i.*g.*log(m0./fuelindex) - g.*t
end
you do not use v or h for further calculations inside the loop, so you might as well not calculate them inside the loop and might as well instead calculate them based upon the final x value, since each time you are overwriting both of them. But your second loop does not use the values calculated and has the same issue of not using the values it is in the middle of calculating, so there does not appear to be any reason to calculate them at all.
The difficulty you are having with dimensions not agreeing is because fuelindex is a subset of the length of t, so a calculation with fuelindex on one side, subtract a longer vector with all of the times, is a mismatch of sizes.
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 2월 22일
Because you are asked to use while loops, you should be using a structure outlined by
set initial conditions
loop while there is propellant remaining
amount of propellant to burn this step is the minimum of the remaining propellant and the standard burn rate. This will be the standard burn rate except possibly the time step that it runs out
duration time of this step is the amount to burn this step divided by the standard burn rate. This will be the standard time step except possibly the time step in which the propellant runs out
calculate impulse for this step based upon amount to burn this step and the current mass
increase acceleration based on impulse minus g
increase velocity by acceleration times duration of this time step
increase position by velocity times duration of this time step
decrease stored propellant by amount burned
end loop
take note of current velocity: it is now the highest it will be
loop while position is above ground
duration of this time step is fixed (really the last one should be shorter because it should end when you hit the ground, but that is a bit of a nuisance to calculate)
acceleration is g (downwards)
take note of current velocity and current position
increase velocity by acceleration times duration of this time step
If the previous velocity was positive and the new one is not positive then the previous position is (approximately) the maximum height
increase position by velocity times duration of this time step
end loop
because the acceleration is constant negative, the positive velocity left after the first loop will slow and eventually become negative, so your position will eventually start to decrease and eventually the ground will be hit.
This model does not account for the curvature of the Earth or for decreasing gravity with height.

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


Michael Bortolot
Michael Bortolot 2018년 2월 22일
Thank you, but I'm still very confused on how to fix those problems. How do I calculate a v and h for both with/without fuel and concatenate them? what exactly do I write in each "while loop", how do I change X so that it runs the while loop?

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by