Program does 5 calculations instead of one.

1 Ansicht (letzte 30 Tage)
Kokalz
Kokalz am 20 Jul. 2012
Hi Guys! I have written a simple script, that's supposed to do some loop calculations. Here's the script:
a=3;
initialVelocity = 38.8889;
brakeMass = 14;
velocity = (initialVelocity:a:54.72);
coolingCoef = (85+0.0017*velocity.^2);
A=0.4;
time=(velocity-initialVelocity)/a;
temperature = zeros(1,6);
temperature(1)=900;
for x=2:length(temperature)
dissipatedHeat = coolingCoef.*(temperature(x-1))
energy = dissipatedHeat.*time;
specificHeatCapacity = 457+0.14219*temperature(x-1);
temperatureDrop = energy/(brakeMass*specificHeatCapacity);
temperature(x)=temperature(x-1)-temperatureDrop(x);
end
When I run the proram it gives me series of answers. Like this:
dissipatedHeat =
1.0e+04 *
7.8814 7.9185 7.9583 8.0009 8.0462 8.0943
dissipatedHeat =
1.0e+04 *
7.7967 7.8334 7.8728 7.9149 7.9598 8.0074
dissipatedHeat =
1.0e+04 *
7.6280 7.6638 7.7024 7.7436 7.7875 7.8340
dissipatedHeat =
1.0e+04 *
7.3778 7.4125 7.4498 7.4896 7.5321 7.5771
dissipatedHeat =
1.0e+04 *
7.0511 7.0842 7.1199 7.1580 7.1985 7.2416
However, I just want the script to multiply value by value. So say on the first iteration it multiplies frst values from the vector and gives me the answer of temperature for second iteration. It the multiplies 2nd value from both matrices and so on. I think Matlab multiplies every member in a matrix by every member in other matrix. I tried adding a do before one of the values, but it does not help. Any ideas how to fix it? Think I'm just missing a small point somewhere,but can't find it. Thank you!

Akzeptierte Antwort

Jan
Jan am 20 Jul. 2012
Bearbeitet: Jan am 20 Jul. 2012
Perhaps you want:
...
dissipatedHeat = coolingCoef(x-1) * (temperature(x-1))
...
energy = dissipatedHeat * time(x); % or time(x-1)?
  3 Kommentare
bym
bym am 21 Jul. 2012
I think you should consider Jan's answer more carefully.
coolingCoef
time
are vectors.
Jan
Jan am 21 Jul. 2012
Bearbeitet: Jan am 21 Jul. 2012
velocity is a vector. Therefore coolingCoef and time are vectors. Therefore dissipatedHeat is a vector. Did you try my suggestion, which let you access one element per loop iteration?
Use the debugger and step through you code line by line to find out, what's going on.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Kokalz
Kokalz am 21 Jul. 2012
Still can't figure it out. Anyone? :(
  3 Kommentare
Kokalz
Kokalz am 21 Jul. 2012
Right. Basically I want the program to do a loop calculation so I can get a certain number of different values, that will then be plotted on the graph. The values of the ''for x=2:length....'' part are where I do the loop calculation. So it goes like this: I assign the first value for temperature> get values for dissipated heat > energy and so on, to get the second value of temperature. And then it starts all over to get the value of 3rd temperature. Hopefully that explains the pattern. As specified in the question I, for some reason, get five 1x6 matrices for dissipated heat instead of just one 1x6 value of dissipated heat. Matlab for some reason takes the bottom one. Also, the values I get there are completely different from the values I get plugging this formula to excel. I could upload the excel document on demand, because it will probably explain all the steps a bit better. Thank you very much for any help!
per isakson
per isakson am 21 Jul. 2012
Bearbeitet: per isakson am 21 Jul. 2012
Meta: Comment on comment
It is difficult to read and understand your comment and that is partly because it is not formatted. Now, I have copy&pasted to an editor formatted somewhat and printed.
Compare:
So it goes like this:
  • I assign the first value for temperature
  • get values for dissipated heat energy and so on, to get the second value of temperature.
  • And then it starts all over to get the value of 3rd temperature.

Melden Sie sich an, um zu kommentieren.


per isakson
per isakson am 22 Jul. 2012
Bearbeitet: per isakson am 22 Jul. 2012
Firstly, the code appears to be a strange blend of loop and vectorized (as Jan says).
Secondly, a short description of the underlying physics would help. Is it about a brake (of a moving vehicle), which heats and losses heat as the velocity of the vehicle changes?
Thirdly, I think it would be helpful to indicate physical units in comments to appropriate assignments.
Time out for now. I'll have a look at your code in my next break.

Kokalz
Kokalz am 23 Jul. 2012
Allright guys! I'm really sorry for making the whole thing really confusing. I treid Jan's suggestion on Friday and it did not work. I checked it today and noticed I made a very small mistake at some point, that fixed everything. Thank you very much for all your help and I'll try to make my questions more consistent in the future!

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by