Index exceeds number of array elements

I'm really stuck with this task I've been given. When I run this script of mine written to answer the formula's shown, on line 23 it tells me index exceeds number of array elements. Which it does using h(i+1)-hi(i) , with both of those having 9 numbers in it there will be 8 differences. I'm asked to use formula 4 to get the Pi numbers you see in the table. I get the first 8 values correct, but can't seem to get the 9th number in the formula because index exceeds number of array elements. What I cant figure out is how to get 9 numbers when it has the the diff hi function in there which only has 8 values. I made the Ai column have 9 values by having Ai_1 = [ai, 0], but im not allowed to just insert the final Pi values in. I have attatched a copy of my script in hopes it gives some clarity as well as all the formula's and stuff listed.
To sum it all up I need my Pi values to match up or get very close to the ones listen in table 5, and my pi values (formula shown in files) to get pretty close as well and then print it in a table like 5. Any help is greatly appreciated!!!
Edit: I realise my Fprintf might be incorrect for what Im trying to get at the end.

6 commentaires

KSSV
KSSV le 12 Sep 2020
Copy paste your code here. This error occurs when you try to extract more number of elements then present in the array.
Liam Crocker
Liam Crocker le 12 Sep 2020
Modifié(e) : Liam Crocker le 12 Sep 2020
hi = [0 , 11 , 20, 32 , 47, 51 , 71 , 84.852 , 90 ];
Ti = [288.15, 216.65 , 216.65, 228.65 , 270.67, 270.65 , 214.65 , 186.946, 186.946] ;
ai = diff(Ti)./diff(hi) ;
go = 9.80666;
Rs = .287054;
B =(go/Rs);
Re = 6356.766;
syms Z % tells matlab Z is the symbol for an unkown variable
Re = 6356.766;
Pi= 101325.0000;
Ai_1 = [ai,0];
Pr=Pi,
Hh=diff(hi); %code taken from formula for Ai
TT=Ti;
for i=1:length(hi)%i means 9 or 1 to the length of hi which is 9 long, so it will have 9 values
H_1(i) = solve((Re*Z)/(Re+Z)==hi(i), Z);
H = double(H_1); % ensures my values are not in fraction form when called
fprintf('%1.3f\n', H(i)); %use the double(Hi)to call one of the 9 values for Z, uses 1.3f because I want 3 decimal places as seen in the table
end
for i= 1:length(Ai_1)
if Ai_1(i) ==0
Pr(i+1)= (Pr(i).*exp(-B*Hh(i)/TT(i)))
else Pr(i+1)=(Pr(i)*(TT(i)/(TT(i)+Ai_1(i)*Hh(i))).^(B/Ai_1(i)))
fprintf('%10.8f\n', Pr)
end
end
P_at_h = Pr./(Rs.*TT)
for i= 1:length(ai)
if Ai_1(i) ==0
Ph = P_at_h(i).*(exp.*(-B*Hh(i)/TT(i)));
else Ph(i)=(P_at_h(i)*(TT(i)/(TT(i)+ai(i)*Hh(i))).^(B/Ai_1(i)));
fprintf('%10.8f\n', Ph(i))
end
end
for i = 1:length(ai)
T_at_h = Ti(i)+ai(i)*(h-hi(i))
end
%There is only 8 values in the array diff/hi which I have called Hh, but thats whats puzzling me about this formula, how do I get 9 values when it includes the difference formula which only gives 8 values
You reduce the number of loop by 1.
length(ai)-1
Or use gradient.
Liam Crocker
Liam Crocker le 12 Sep 2020
Modifié(e) : Liam Crocker le 12 Sep 2020
Thank you! I cannot seem to accept your answer on the website for some reason though.
KSSV
KSSV le 12 Sep 2020
Modifié(e) : KSSV le 12 Sep 2020
I have commented it....for comment there is no option to accept.
Liam Crocker
Liam Crocker le 12 Sep 2020
Well thank you so much you fixed my problem once again KSSV! Really appreciate all the help you are giving me!

Connectez-vous pour commenter.

Réponses (0)

Catégories

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by