MATLAB: Unable to perform assignment because the left and right sides have a different number of elements.

array elementsdifferent number of elementsleft and right sidesMATLABrocketrocket weightunable to perform assignment because the left and right sides have a different number of elements

I'm trying to calculate the weight of a rocket as a function of time,t. The weight decreases linearly over time due to the burning of the rocket fuel. I am getting the error mentioned in the title whenever I try to incorporate my user-defined function 'findWeight' into the main script. The relevant code is indicated by 'PART 7' and 'PART 8' below.
findWeight function:
function weight = findWeight(massTotal,startingMassFuel,burnTime,t,g)
for t = 0:burnTime
weight = (massTotal - ((startingMassFuel/burnTime)*t))*g;
end
for t = burnTime+1:t(end)
weight = (massTotal - startingMassFuel);
end
end
Main Script:
massEmptyRocket = 1500; % kg


startingMassFuel = 7000; % kg
massHorse = 75; %kg
massTotal = massEmptyRocket + startingMassFuel +massHorse; % kg
engineThrust = 130000; %N
gravity = 9.81; %m/s/s
startingdensity = 1.225; %kg/m3
dragCoeficient = .05;
startingdrag = 0; % no drag
startingWeight = massTotal * gravity; %Constant for now
dt = .1; % timeStep duration in seconds
endTime = 150; % Seconds
acceleration = zeros(1,endTime/dt); %Create empty vector of zeros for Acceleration Data the length of the simulation
time = zeros(1,endTime/dt); %Create empty vector of zeros for time Data
burnTime = 60; %Time Engines are firing for.
airdensity = zeros(1,endTime/dt);
weight = zeros(1,endTime/dt);
rocketdiameter = 2; %m
DensityData = xlsread('DensityData');
for t = 1:1:(endTime/dt) % t is the current TimeStep
time(t) = t*dt; % update the time vector with the new time step
%Calculate the velocity and Position, by integrating.
velocity = cumtrapz(time,acceleration);
position = cumtrapz(time,velocity);
airdensity(t) = findDensity(position,t,DensityData)*1000;
drag(t) = findDrag(airdensity,velocity,dragCoeficient,rocketdiameter,t);
%PART 7: Write a function (findWeight.m) which calculates the weight of the rocket at each time step.
% Assume to fuel mass decrease linearly from the launch time to the end of the burn.
weight(t) = findWeight(massTotal,startingMassFuel,burnTime,t,gravity);
massTotal(t) = weight(t)/gravity;
%Equation of motion
if t <= (burnTime/dt) %while the engines are firing
%PART 8: Change code to use time varying for drag, weight and massTotal calculated in part 5-7. Replace equation with
%acceleration(t) = (engineThrust - drag(t) - weight(t)) /massTotal(t);
acceleration(t) = (engineThrust - drag(t) - weight(t)) /massTotal(t);
% PART 8: Change code to use time varying for drag, weight and Mass Total calculated in Parts 5-7. Replace equation with
%acceleration(t) = (- drag(t) - weight(t)) /massTotal(t);
else %when the engines have stopped firing
acceleration(t) = (-drag(t) - weight(t)) /massTotal(t);
end
for i = 1:length(position)
if i>1 && position(i)<0
flightTime = time(i);
break
end
end
end

Best Answer

I've not tried to find where your error is coming from, particularly as you've omitted the most important information: the full text of the error, which would have told us which line raises the error. As Adam said, use the debugger to find what is happening.
After a quick glance through your functions:
  • In finddensity, avoid a succession of if ... elseif ... elseif ... without terminating by a final else, not elseif. If neither the if or any of the elseif is true, your densityh will never get a value. That could happen if position(t) is NaN.
  • The findweight function is complete nonsense. Both loops overwrite height at each step, so only the last step will have an effect. The second loop is never going to be executed anyway, since the input t has been overwritten by the loop variable t. In effect, all the function does is calculate:
weight = massTotal - startingMassFuel*g; %result of last iteration of 1st loop
  • Be careful with the spacing in your expressions. massEmptyRocket + startingMassFuel +massHorse is not going to cause a problem in your code but it would for example in the expression:
x = [1, massEmptyRocket + startingMassFuel +massHorse]
which will produce a very different result to
x = [1, massEmptyRocket + startingMassFuel + massHorse]
simply because of a missing space between + and massHorse.