Reformat of code, Rewriting code and adding some changes

Does anyone have any ideas of how I code write this code differently? Like is there any substitues for the matlab based functions that I am calling, Even if it makes the code longer Ill accept the answer.
function [t,cp]=CpRange(tmin,tmax,inc)
inc=round(inc,3);%rounding to 3 decimal points
t=tmin:inc:tmax;
cp=zeros(size(t));
for i=1:length(cp)
cp(i)=CO2(t(i));
end
end

4 commentaires

What is CO2? And what is your objective in wanting to rewrite this? To speed it up?
CO2 represents a data calculation
here is the other function for CO2 and yes to speed it up
function cp=CO2(t)
R=.1889;
if t>=300 && t<=1000
cp=0.24007797e+01+0.87850957e-02*t-0.6607878e-05*t^2+0.20021861e-08*t^3+0.63274039e-15*t^4;
elseif t>1000 && t<=3000
cp=0.44080e+1+0.309817e-02*t-0.123925e-05^t^2+0.227413e-09*t^3-0.155259e-13+t^3;
end
cp=cp*R;
end
Vectorizing the CO2 calculation should speed it up. You can use logical indexing to do that:
function [t,cp]=CpRange(tmin,tmax,inc)
inc=round(inc,3);%rounding to 3 decimal points
t=tmin:inc:tmax;
cp=zeros(size(t));
R=.1889;
idx = t>=300 & t<=1000;
cp(idx) = 0.24007797e+01 ...
+0.87850957e-02*t(idx) ...
-0.6607878e-05*t(idx).^2 ...
+0.20021861e-08*t(idx).^3 ...
+0.63274039e-15*t(idx).^4;
idx = t>1000 & t<=3000;
cp(idx) = 0.44080e+1 ...
+0.309817e-02*t(idx) ...
-0.123925e-05.^t(idx).^2 ...
+0.227413e-09*t(idx).^3 ...
-0.155259e-13+t(idx).^3;
cp=cp*R;
Also, check your second formula (for t > 1000 and <= 3000); I think maybe there are two typos (^t^2 should perhaps be *t^2 and +t^3 should perhaps be *t^4).
Thank you Mr. BenJamin that really helped me (:

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by