For-loop error: Unable to perform assignment because the left and right sides have a different number of elements

2 views (last 30 days)
I am currently trying to calculate the different processen in a combustion engine. For that I so calculated the change of volume, pressure and temperature. I'm now trying to calculate the heatflow.
Vphi,pphi and Tphi are all 1x7201 doubles.
Using the function gives me the error "Unable to perform assignment because the left and right sides have a different number of elements." in the line "warmth(i) = warmth(i-1) + dQ;". After a lot of troubleshooting I can't get it to work as a function. If I use it as a normal script it works without a problem for some reason. I would really like to use it as a function though.
Any help is highly appreciated.
function warmth = Warmth(Vphi, pphi, Tphi)
global dk rs s n
c_m = (s * n)/ 30;
warmth = zeros(1,rs+1);
dQ = 0;
for i = 2:length(Vphi)
alpha = dk^(-0.22) * 0.013 * pphi(i)^0.8 * Tphi(i) * 6.18 * c_m;
A = Vphi(i)/(dk/2);
dQ = alpha * A * (Tphi(i)-Tphi(i-1));
warmth(i) = warmth(i-1) + dQ;
end
end
  2 Comments
Matt J
Matt J on 30 Mar 2019
Edited: Matt J on 30 Mar 2019
This works fine
global dk rs s n
[Vphi, pphi, Tphi]=deal(rand(1,7201));
[dk s n]=deal(1);
rs=10;
warmth_output = Warmth(Vphi, pphi, Tphi)
function warmth = Warmth(Vphi, pphi, Tphi)
global dk rs s n
c_m = (s * n)/ 30;
warmth = zeros(1,rs+1);
dQ = 0;
for i = 2:length(Vphi)
alpha = dk^(-0.22) * 0.013 * pphi(i)^0.8 * Tphi(i) * 6.18 * c_m;
A = Vphi(i)/(dk/2);
dQ = alpha * A * (Tphi(i)-Tphi(i-1));
warmth(i) = warmth(i-1) + dQ;
end
end

Sign in to comment.

Answers (0)

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!