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;endfor t = burnTime+1:t(end) weight = (massTotal - startingMassFuel);endend
Main Script:
massEmptyRocket = 1500; % kg
startingMassFuel = 7000; % kgmassHorse = 75; %kg
massTotal = massEmptyRocket + startingMassFuel +massHorse; % kgengineThrust = 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 endend
Best Answer