How do you print data from a loop into a Matrix?

I am running a program that aims to simulate the flight of a rocket and it involves running a loop that simulates the rocket at x times (.0001,.0002,etc.). I need to be able to take the data from the loop not at each time interval, but every ten time intervals ie .001,.002,etc.
Originally I attempted to place an "if" statement within the loop, defining deltat as a array of values ranging from 0 to 100 at intervals of .001, but I keep receiving an error that claims that the left side is an invalid target for assignment. Is there a better way to go about doing this?
while mwater > 0
Vair = Vbottle - (mwater/rhowater)
Pair = (mairini * R * T)/(Mair * Vair)
mdotwater = .01
mwater = mwater - (mdotwater * deltat)
mtotal = mrocket + mwater + mairini
vwater = C*(2*(Pair-Patm)/(rhowater))^(.5)
Fthrust = mdotwater * vwater
Fgravity = (mtotal * 9.81)
Fdrag = (.5)*(Cd)*(rhoatm)*(Aprojected)*(vrocket)^2
Fnet = Fthrust - Fgravity - Fdrag
arocket = (Fnet)/(mtotal)
deltat = deltat + .0001;
end

6 comentarios

the line
deltat = deltat + .0001;
seems to imply that deltat is not an array but a scalar... Btw, which line gives you the error and which error exactly? If you provide the input data, people may be able to reproduce your error.
Matthew
Matthew el 17 de Abr. de 2014
Editada: Matthew el 17 de Abr. de 2014
Sorry, I'm new to Matlab. Regardless, I should have been a clearer with my question.
The deltat isn't the problem in and of itself. It serves the task of modifying the values above as time changes within the loop. The issue I'm having is that I need to take data at various times within the loop (in this case every 10th interval which occurs at .001, .002, etc) for each equation and place it into a new matrix.
The error I am receiving relates to the method by which I first attempted to do this:
if deltat = [0:.001:100]
z = 5
end
The z = 5 is arbitrary, just designed to see if the line above is functioning properly. It will later be replaced by an xlswrite command to place the data into an excel file.
why don't you do:
if(mod(deltat,0.001)==0)
This checks if the current deltat is a multiple of 0.001 (as you seem to need).
Image Analyst
Image Analyst el 17 de Abr. de 2014
You forgot to paste in your error message. You just paraphrased and snipped out a small chunk of it. We want all the red text - all of it, don't leave any out. And you also forgot to attach your data file like Sara asked for.
Matthew
Matthew el 17 de Abr. de 2014
Editada: Matthew el 17 de Abr. de 2014
Here is everything I have so far, including the excel file that the code pulls the data from. Sara's method does give me values with which to print the data, but it keeps overwriting itself in the matrix. I need each data point to be saved in a separate cell at each interval.
%Data Input
filename = 'RocketData.xlsx';
num = xlsread(filename);
%Data Definitions
deltat = num(1,1);
printing = num(2,1);
Vbottle = .002;
f = num(4,1);
Dn = num(5,1)*.0254;
Anozzle = num(6,1)*.00064516;
Aprojected = num(7,1)*.00064516;
mpayload = num(8,1)*.001;
mrocket = num(9,1)*.001;
Pairini = num(10,1)*6894.752728;
Cd = num(11,1);
C = num(12,1);
Li = num(13,1)*.0254;
g = 9.81;
c = 343;
Mair = .029;
gamma = num(17,1);
Patm = num(18,1)*101.325;
rhowater = 1000;
Tamb = num(20,1) + 273.15;
R = 8.314;
%Phase 1 Initial Calculated Values
T = Tamb;
rhoatm = (Mair*Pairini)/(R*T);
rhoair = (Mair*Pairini)/(R*T);
Vairini = (1-f)*Vbottle;
mairini = Pairini*Vairini;
mwaterini = f*Vbottle*rhowater;
mtotal = mrocket + mpayload + mwaterini + Mair;
t = 0;
x = 0;
vrocket = 0;
Vini = (1-f)*Vbottle;
Tini = Tamb;
%Phase 1 Data Initial Calculations
mwater = mwaterini;
while mwater > 0
Vair = Vbottle - (mwater/rhowater);
Pair = (mairini * R * T)/(Mair * Vair);
mdotwater = .01;
mwater = mwater - (mdotwater * deltat);
mtotal = mrocket + mwater + mairini;
vwater = C*(2*(Pair-Patm)/(rhowater))^(.5);
Fthrust = mdotwater * vwater;
Fgravity = (mtotal * 9.81);
Fdrag = (.5)*(Cd)*(rhoatm)*(Aprojected)*(vrocket)^2;
Fnet = Fthrust - Fgravity - Fdrag;
arocket = (Fnet)/(mtotal);
deltat = deltat + .0001
if deltat = [0:.001:100]
xlswrite('stuff.xlsx',arocket,1,'A1')
end
end
José-Luis
José-Luis el 17 de Abr. de 2014
Editada: José-Luis el 17 de Abr. de 2014
I'll go off on a tangent here. Using mod on doubles makes me cringe. Matlab uses forced rounding to make it work. At least from my point of view, that is unexpected behavior, and one should always be careful to read the fine print of the functions one will use. Given that Matlab is so vast, that makes for a lot of reading.
I'm done ranting for the day.

Iniciar sesión para comentar.

 Respuesta aceptada

Sara
Sara el 17 de Abr. de 2014
You always write in the same cell, that's the issue. Before the while loop, define a variable to count how many times you print, e.g., counter = 0; Then, each time the if statement is true, increment it:
counter = counter + 1;
Then replace
xlswrite('stuff.xlsx',arocket,1,'A1')
with
xlswrite('stuff.xlsx',arocket,1,['A',num2str(counter)])
In that way you print in a new cell each time.

4 comentarios

Matthew
Matthew el 17 de Abr. de 2014
That works beautifully! After I check and make sure the mod command you suggested earlier is working everything should be fine with the code I have.
Thanks for the help!
Matthew
Matthew el 17 de Abr. de 2014
I am still having one issue. The mod function you suggested prints values at the following times:
.001 .002 .005 .009 .036 .148
As opposed to what is needed: .001 .002 .003 .004 .005 etc.
Ok, the issue is in deltat = deltat + .0001. If you look at the numbers, they are not what you'd expect for numerical reasons. Put:
kk = 0;
before the while loop, increment it in the while loop (before incrementing deltat):
kk = kk + 1;
and replace the deltat = deltat + .0001 with:
deltat = 1e-4*kk;
That should solve it.
Matthew
Matthew el 18 de Abr. de 2014
Solve it it did.
Thanks again for all the help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 17 de Abr. de 2014

Comentada:

el 18 de Abr. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by