Borrar filtros
Borrar filtros

Having issue trying to solve a group of errors.

3 visualizaciones (últimos 30 días)
K.Knowles
K.Knowles el 12 de Oct. de 2023
Comentada: Walter Roberson el 12 de Oct. de 2023
First of all this is for a section of an assessment, the objective I am trying to achieve with the matlab code is to produce approxiate values for the mass of propellant at each stage of a rocket, whilst minimising the total weight. However i've have hit a wall with the coding as i am getting the following errors:
I will admit coding isn't my strong suit, however I would like to improve. I do also want to apologise in advance for including a decent chunk of coding, but I am currently just stumped on how to fix this this, or if what ive written is the right way to go about the process.
Just for clarification if needed, I am using an amended Tsiolkovsky Equation, using a mass fraction, to eliminate the structural mass from the equations.
I have also used approximate values for the specific impulses at each stage, they are not the actual values but are mearly placeholders for the moment, same goes with the mass fraction values.
The minimum required total Delta V is 7,914.67 m/s
Thank you in advance for any help provided.
% Provide numerical values for symbolic variables
initial_guess_for_mP1 = 500; % Initial guess for mP1
initial_guess_for_mP2 = 400; % Initial guess for mP2
initial_guess_for_mP3 = 300; % Initial guess for mP3
initial_guess_for_DeltaV1 = 2000; % Initial guess for DeltaV1
initial_guess_for_DeltaV2 = 2500; % Initial guess for DeltaV2
initial_guess_for_DeltaV3 = 3000; % Initial guess for DeltaV3
% Define symbolic variables
syms mP1 mP2 mP3 DeltaV1 DeltaV2 DeltaV3
% Equations
m3 = 300 + 0.15*mP3 + mP3;
DeltaV3 = 9.81*330*log((300 + 0.15*mP3 + mP3) / (300 + 0.15*mP3));
m2 = m3 + 0.10*mP2 + mP2;
DeltaV2 = 9.81*300*log((m3 + 0.10*mP2 + mP2) / (m3 + 0.10*mP2));
m1 = m2 + 0.05*mP1 + mP1;
DeltaV1 = 9.81*250*log((m2 + 0.05*mP1 + mP1) / (m2 + 0.05*mP1));
% Function to calculate intermediate results
calculate_results = @(mP1, mP2, mP3) double([subs(m1, [mP1, mP2, mP3]), subs(m2, [mP1, mP2, mP3]), subs(m3, [mP1, mP2, mP3])]);
% Calculate the total weight
total_weight = m1 + m2 + m3;
% Lower bounds for propellant masses
lb = [0, 0, 0]; % Lower bounds for mP1, mP2, and mP3 (non-negative propellant masses)
% Initial guesses
initial_guess = [initial_guess_for_mP1, initial_guess_for_mP2, initial_guess_for_mP3];
% Optimization options
options = optimoptions('fmincon', 'Display', 'iter'); % Display optimization progress
% Solve the optimization problem with the constraint function
[result, fval] = fmincon(@(x) total_weight(x), initial_guess, [], [], [], [], lb, [], @constraint_function, options);
% Extract the optimized values for mP1, mP2, and mP3
mP1_solution = result(1);
mP2_solution = result(2);
mP3_solution = result(3);
% Substitute symbolic variables with optimized values
mP1 = mP1_solution;
mP2 = mP2_solution;
mP3 = mP3_solution;
m3 = double(subs(m3, [mP1, mP2, mP3]));
m2 = double(subs(m2, [mP1, mP2, mP3]));
m1 = double(subs(m1, [mP1, mP2, mP3]));
total_weight_functon(x) = m1 + m2 + m3;
% Display the results
disp('Optimal Propellant Masses (mP1, mP2, mP3):');
disp([mP1_solution, mP2_solution, mP3_solution]);
% Define the total weight function
function f = total_weight_function(x)
mP1 = x(1);
mP2 = x(2);
mP3 = x(3);
m3 = 300 + 0.15*mP3 + mP3;
m2 = m3 + 0.10*mP2 + mP2;
m1 = m2 + 0.05*mP1 + mP1;
f = m1 + m2 + m3;
end
% Define the constraint function
function [c, ceq] = constraint_function(propellant_masses)
mP1 = propellant_masses(1);
mP2 = propellant_masses(2);
mP3 = propellant_masses(3);
m3 = 300 + 0.15*mP3 + mP3;
DeltaV3 = 9.81*330*log((300 + 0.15*mP3 + mP3) / (300 + 0.15*mP3));
m2 = m3 + 0.10*mP2 + mP2;
DeltaV2 = 9.81*300*log((m3 + 0.10*mP2 + mP2) / (m3 + 0.10*mP2));
m1 = m2 + 0.05*mP1 + mP1;
DeltaV1 = 9.81*250*log((m2 + 0.05*mP1 + mP1) / (m2 + 0.05*mP1));
% Inequality constraint
c = DeltaV1 + DeltaV2 + DeltaV3 - 7914.67;
% No equality constraint (ceq is empty)
ceq = [];
end

Respuestas (1)

Walter Roberson
Walter Roberson el 12 de Oct. de 2023
total_weight = m1 + m2 + m3;
That is a variable
[result, fval] = fmincon(@(x) total_weight(x), initial_guess, [], [], [], [], lb, [], @constraint_function, options);
That constructs an anonymous function that tries to index the variable
total_weight_functon(x) = m1 + m2 + m3;
That would be an error because x is not defined when you try to use x to index into a variable named total_weight_functon . (Note: if x had been a symbolic variable then that syntax would be for a symbolic function definition -- but x is not symbolic)
function f = total_weight_function(x)
That function name, total_weight_function, is suspiciously similar to the variable you tried to create earlier, total_weight_functon (that ends in "on" instead of "ion"). It is not clear what your intention is.
I would suggest to you that your fmincon wanted to refer to the function total_weight_function instead of to the variable total_weight
  4 comentarios
K.Knowles
K.Knowles el 12 de Oct. de 2023
In regards to you answer provided @Walter Roberson I deleted the "function f = total_weight_function(x)" and amended the fmincon to the total_weight_function, I now get a value for "f" but have also gotten new errors
Walter Roberson
Walter Roberson el 12 de Oct. de 2023
The mass of each stage is independent of the mass of the other stages.
I think you are probably minimizing the wrong thing. I think you should be minimizing the total cost (not the total weight) and total_weight_function should be calculating sum of (weight of propellent times cost per weight unit) -- but meanwhile the constraint function should be calculating
  • how far you get with propellant_masses(1) weight of whatever kind of propellent is used for stage 1, when the energy is applied to (weight of stage 1 + weight of stage 2 + weight of stage 3 + propellent masses(2) + propellent masses(3) + declining propellent masses(1); PLUS
  • how far you get with propellant_masses(2) weight of whatever kind of propellent is used for stage 2, when the energy is applied to (weight of stage 2 + weight of stage 3 + propellent masses(3) + declining propellent masses(2); PLUS
  • how far you get with propellent_masses(3) weight of whatever kind of propellent is used for stage 3, when the energy is applied to (weight of stage 3 + declining propellent masses(3))
and constraining that total distance to be as high as orbital escape (or whereever the rocket has to reach.)
You can minimize weights of propellents directly only under the circumstance that they have the same cost per unit and the same energy density
It looks like you are calculating deltaV in your constraint function instead of distance... that might possibly work as well.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by