How can i make the Temp matrix to contain all the values of T calculated in both conditions

10 次查看(过去 30 天)
I want to make a Temp matrix which contains the T values calculated for tow conditions (H<=11) and (H>11). This code which i have written doesnt give me a matrix but gives a value. Also MATLAB says 'Size vector must be a row vector with real elements'. How do i solve this
  2 个评论
Walter Roberson
Walter Roberson 2019-4-22
I am having trouble understanding what your code is trying to do.
It looks to me as if you want to use one temperature until a 11 km have passed, and after that use T0-L*something as the temperature, with the something possibly being the distance ? And you want to take X as a ratio between the initial temperature and the adjusted temperature, and take Y as a constant, and calculate M based on those ? And you want to plot that M on the X axis, and the distance travelled (H) on the Y axis, instead of the more normal distance travelled (H) on the X axis and the resulting M on the Y axis ?
Yashita Singh
Yashita Singh 2019-4-22
Hello Yes you guessed it right.
So basically H is the height travelled Add the temperature varies differently with different heighys, so when height is below 11 the temperature varies as T = T0 -LH and after that height it's constant value of 216 K Then I want all these temperature values to be in a matrix which will help me calculate the Mach number at different temperatures (X) Then I want to see how these Mach values varies with Height by plotting a graph and see the trend
I am new to Matlab I might have used wrong commands so I wanted help to understand how should I write this correctly

请先登录,再进行评论。

采纳的回答

Bob Thompson
Bob Thompson 2019-4-22
H = [ 1:10:100]
L = 0.0065 %K/m
T0= 288.15
Temp= zeros(j)
for j= 1:length(H)
for B = H<=11 % till 11 km
T = T0 - L*B %K
for C= H> 11
T = 216.65 %constatnt -56.5deg
end
end
Temp(j)= T
end
There are a couple of things about your code. First, are you aware that H is only [1 11 21 31 ... 81 91]? This does not seem like a very wide range to be covering for an accurate mach calculations, specifically at low altitudes.
Second, and really the answer to your question, you don't need the for loops for B and C, you would be much better suited to use an if statement. I would modify your code as follows:
for j = 1:length(H)
if H(j) <= 11
Temp(j) = T0 - L*H(j);
else % Covers H > 11
Temp(j) = 216.65;
end
end
This uses the principle of indexing, where we are drawing our input from a specific location in the input array, and outputting into a specific indexed position on the output array. I'm assuming you already know how to do this because it appears you were using it for indexing Tem(j) = T before.
Alternatively, you can simply remove the for loop all together and use vector math to complete the calculations in a greater swoop.
Temp = T0 - L*H(H<=11);
Temp = [Temp; 216.65+0*H(H>11)]; % Using 0*H(H>11) is intended to give the correct number of elements, there are alternative ways of accomplishing this
This method using logic indexing and vectorization, where we have identified the elements we want with a logic statement in our index, and then operated on all of the values at one time by treating them as a vector. For your case this removes the need for loops, and is often regarded as more computationally efficient.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by