### Configure Hot Start Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedOptionAuto.html Set the initial guess method for the solving process. ```matlab options.start='...'; ``` -------------------------------- ### Main script: solve and simulate (main_DoubleIntegratorTracking.m) Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleDITracking.html Fetches the problem definition, gets options and solver settings, solves the NLP, and simulates the solution using ode113 with a step size of 0.01. ```matlab [problem,guess]=DoubleIntegratorTracking; % Fetch the problem definition options= problem.settings(30); % Get options and solver settings [solution,MRHistory]=solveMyProblem( problem,guess,options); [ tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 0.01 ); ``` -------------------------------- ### Solve and simulate in main_BatchFermentor.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBatchFermentor.html Main script that fetches the problem definition, gets solver options, solves the NLP, and simulates the solution. The last line performs an open-loop simulation using the ode113 solver with a step size of 0.01, applying the obtained input trajectory to the dynamics defined in BatchFermentor_Dynamics_Sim.m. ```matlab [problem,guess]=BatchFermentor; % Fetch the problem definition options= problem.settings(300); % Get options and solver settings [solution,MRHistory]=solveMyProblem( problem,guess,options); [ tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 0.01 ); ``` -------------------------------- ### Set Adigator path Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedOptionMultiPhase.html Specifies the installation path of Adigator when options.derivatives='adigator' is selected. The path can be relative (e.g. '../../adigator') or exact (e.g. 'D:\adigator'). ```matlab options.adigatorPath='...'; ``` -------------------------------- ### main_Bangbang.m: solve and plot Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang.html Main script main_Bangbang.m: fetches the problem definition, gets options and solver settings, solves the resultant NLP, and generates plots for the solution. ```matlab [problem,guess]=BangBang; % Fetch the problem definition options= problem.settings(30); % Get options and solver settings [solution,MRHistory]=solveMyProblem( problem,guess,options); genSolutionPlots(options, solution); ``` -------------------------------- ### Set up automatic regularization: problem.data.penalty.values and problem.data.penalty.i Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblemSingle.html Sets up the automatic regularization function. The sequence of weights is defined in problem.data.penalty.values and the starting index in problem.data.penalty.i. The parameter can be called with vdat.penalty.values(vdat.penalty.i) in problem formulation functions. ```matlab problem.data.penalty.values=[weight_1, weight_2, ... weight_n]; problem.data.penalty.i=1; %starting weight ``` -------------------------------- ### Load MPC controllers in ICLOCS_NLPSolver.m Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedCLSim.html In the controller main function ICLOCS_NLPSolver.m, load different MPC controllers based on a condition. For each controller, set opt_step (re-optimization interval, 0 for ASAP) and opt_t_min (stop re-optimizing if solution.tf is smaller). The example shows controller 2 and the default controller 1. ```matlab if ConditionForController2 opt_step=...; % time interval for re-optimization for controller 2, set to zero for update ASAP opt_t_min=0; % stop re-optimizing if solution.tf is smaller than opt_t_min [ tfpu ] = ICLOCS_NLPSolver_Phase2(tpx0); ... else % default controller opt_step=0.01; % time interval for re-optimization, set to zero for update ASAP opt_t_min=0; % stop re-optimizing if solution.tf is smaller than opt_t_min [ tfpu ] = ICLOCS_NLPSolver_Phase1(tpx0); end ``` -------------------------------- ### Configure per-phase discretization options Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblemMultiPhase.html Set the discretization options for each phase, allowing different methods per phase. For example, use the h method with Nps mesh intervals for phase 1 and the hp method with Nps intervals and polynomial order Npd for phase 2. ```matlab phaseoptions{1}=problem.phases{1}.settings(Nps); % h method for phase 1 phaseoptions{2}=problem.phases{2}.settings(Nps,Npd); % hp method for phase 2 ... ``` -------------------------------- ### Set options for pseudo-spectral methods with custom meshing Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainSinglePhase.html Sets solver options for pseudo-spectral methods using two arrays for customized meshing. For example, settings([-1 0.3 0.4 1],[4 5 3]) creates 3 segments on the normalized intervals with specified polynomial orders. ```matlab options= problem.settings([-1 0.3 0.4 1],[4 5 3]); ``` -------------------------------- ### Set resCostWeight for penalty barrier method Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedOptionSingle.html For the penalty barrier method, provide a sequence of weights used for regularization. The example suggests a sequence like 1./(2*[1 0.1 0.01 0.001]). ```matlab options.resCostWeight='...'; ``` -------------------------------- ### Define static parameters for multiphase problem in BangBang.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Defines the static parameters as part of the optimization variables. The parameters are set to empty in this example. ```matlab problem.mp.parameters.pl=[]; problem.mp.parameters.pu=[]; guess.mp.parameters=[]; ``` -------------------------------- ### Set options for pseudo-spectral methods with scalar inputs Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainSinglePhase.html Sets solver options for pseudo-spectral methods using scalar numbers for Nps equally spaced intervals with polynomial orders of Npd. For example, settings(5,4) means 5 LGR intervals each of polynomial degree 4. ```matlab options= problem.settings(Nps,Npd); ``` -------------------------------- ### Define system dynamics in BatchFermentor_Dynamics_Internal.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBatchFermentor.html Defines the system dynamics for the batch fermentor example. The function returns the state derivative vector dx, which is used by the solver to evaluate the differential equations. No path constraints are present, so only dx is returned. ```matlab x1 = x(:,1);x2 = x(:,2);x3 = x(:,3);x4 = x(:,4); u1 = u(:,1); h1 = 0.11*(x3./(0.006*x1+x3)); h2 = 0.0055*(x3./(0.0001+x3.*(1+10*x3))); dx(:,1) = (h1.*x1-u1.*(x1./500./x4)); dx(:,2) = (h2.*x1-0.01*x2-u1.*(x2./500./x4)); dx(:,3) = (-h1.*x1/0.47-h2.*x1/1.2-x1.*(0.029*x3./(0.0001+x3))+u1./x4.*(1-x3/500)); dx(:,4) = u1/500; ``` -------------------------------- ### Configure simulation and fetch first problem Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedCLSim.html In the initialization script, configure the simulation time step, initial mesh points, the settings function handle for the first controller, and fetch the first optimal control problem definition. ```matlab tstep=0.1; % time step of simulation N_node=20; % initial number of mesh points settings_func=@settings_MinTimeClimbBryson_Phase1; % Function handle for the settings file of the first controller [problem,guess]=MyProblem_Phase1; % Fetch the problem definition ``` -------------------------------- ### Initialize iteration control variables Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainMR.html Sets initial values for the maximum absolute error, starting iteration, and maximum iteration count (50) for the mesh refinement loop. ```matlab maxAbsError=1e9; % the initial (before start) maximum absolute local eeror i=1; % starting iteration imax=50; % maximum number of mesh refinement iterations ``` -------------------------------- ### Execute Main Script main_TwoLinkRobotArm.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleRobotArm.html Fetches the problem definition, solves the NLP, and simulates the solution. ```matlab [problem,guess]=TwoLinkRobotArm; % Fetch the problem definition options= problem.settings(20); % Get options and solver settings [solution,MRHistory]=solveMyProblem( problem,guess,options); [ tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 0.01 ); ``` -------------------------------- ### Main script main_BangbangTwoPhase.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Fetches the problem definition, applies solver settings, solves the NLP, and generates solution plots. ```matlab [problem,guess,options.phaseoptions]=BangBangTwoPhase; % Fetch the problem definition options.mp= settings_BangbangTwoPhase; % Get options and solver settings [solution,MRHistory]=solveMyProblem( problem,guess,options); genSolutionPlots(options, solution); ``` -------------------------------- ### Set initial conditions and bounds in myProblem.m Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblemSingle.html Provide the initial conditions and their bounds. To fix an initial state, set the upper and lower bounds equal; to leave it free or bounded, set different bounds and use problem.states.x0=[]. ```matlab problem.states.x0=[x1(t0) ... xn(t0)]; ``` ```matlab problem.states.x0l=[x1(t0)_lowerbound ... xn(t0)_lowerbound]; problem.states.x0u=[x1(t0)_upperbound ... xn(t0)_upperbound]; ``` -------------------------------- ### Solve the problem and simulate solution in main_OrbitRaising.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleOrbitRaising.html Fetches the problem definition, sets options, solves the NLP, and simulates the solution using ode113 with a step size of 0.01. Note that the simulation uses the dynamics defined in WindshearGoAround_Dynamics_Sim.m. ```matlab [problem,guess]=WindshearGoAround; % Fetch the problem definition % options= problem.settings(5,8); % for hp method options= problem.settings(20); % for h method [solution,MRHistory]=solveMyProblem( problem,guess,options); [ tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 0.01 ); ``` -------------------------------- ### Initialize and fetch problem with options Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainMR.html Initializes the workspace, fetches the problem definition, and selects solver options. The active line uses settings_hp with a segment vector and breakpoints; commented alternatives show settings_auto and settings_h. ```matlab clear all;close all;format compact; global sol; sol=[]; % Initialize solution structure [problem,guess]=myProblem; % Fetch the problem definition %%%% Choice among the ones belwo %%%% % options= settings_auto(40); % options= settings_h(40); % options= settings_hp(5,4); options= settings_hp([4 5 3],[-1 0.3 0.4 1]); % Get options and solver settings ``` -------------------------------- ### Execute main_CarParking.m solver script Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleParallelParking.html Fetches the problem definition, configures solver settings, solves the NLP, and performs an open-loop simulation using the ode113 solver. ```matlab [problem,guess]=CarParking; % Fetch the problem definition options= problem.settings(200); % Get options and solver settings [solution,MRHistory]=solveMyProblem( problem,guess,options); [ tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 0.01,[5;2]); ``` -------------------------------- ### Solve commercial flight profile problem Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleCommericalFlight.html Main script that fetches the problem definition, gets solver options with 60 collocation points, solves the NLP, and returns the solution and iteration history. The comment lines indicate the purpose of each call. ```MATLAB [problem,guess]=CommericalFlightProfile; % Fetch the problem definition options= problem.settings(60); % Get options and solver settings [solution,MRHistory]=solveMyProblem( problem,guess,options); ``` -------------------------------- ### Main Execution Script Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleMinFuelClimb.html Fetches the problem, solves the NLP, and simulates the resulting trajectory. ```matlab [problem,guess]=MinEnergyClimbBryson; % Fetch the problem definition % options= problem.settings(10,4); % for hp method options= problem.settings(20); % for h method [solution,MRHistory]=solveMyProblem( problem,guess,options); [ tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 0.01 ); ``` -------------------------------- ### Load problem and guess from myProblem.m Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainSinglePhase.html Loads the formulated optimal control problem and initial guess from the user-defined myProblem.m file. This is the first step before setting options and solving. ```matlab [problem,guess]=myProblem; ``` -------------------------------- ### Define function handles in myProblem.m Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblemSingle.html Set up the problem definition file by providing function handles for the internal dynamics, simulation dynamics, optional analytic derivatives, and the settings file. The handles for analytic derivatives are optional; the settings handle is required. ```matlab InternalDynamics=@myProblem_Dynamics_Internal; SimDynamics=@myProblem_Dynamics_Sim; ``` ```matlab problem.analyticDeriv.gradCost=@gradCost; problem.analyticDeriv.hessianLagrangian=@hessianLagrangian; problem.analyticDeriv.jacConst=@jacConst; ``` ```matlab problem.settings=@settings_myProblem; ``` -------------------------------- ### Load solver settings for the multi-phase problem Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainMultiPhase.html Loads the corresponding solver settings from settings_myMultiPhaseProblem into options.mp. ```matlab options.mp= settings_myMultiPhaseProblem; ``` -------------------------------- ### Define Boundary Conditions and Simple Bounds Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleMinFuelClimb.html Sets the initial and final values for altitude, speed, flight path angle, and mass, along with their respective physical constraints. ```matlab % Boundary Conditions alt0 = 0; altf = 19994.88; speed0 = 129.314; speedf = 295.092; fpa0 = 0; fpaf = 0; mass0 = 19050.864; % Simple Bounds altmin = 0; altmax = 21031.2; speedmin = 5; speedmax = 1000; fpamin = -40*pi/180; fpamax = 40*pi/180; massmin = 22; massmax = 20410; alphamin = -pi/4; alphamax = pi/4; ``` -------------------------------- ### Configure Problem Definition in WindshearGoAround.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleWindshear.html Sets up function handles for dynamics, specifies derivative settings, and links the settings file. ```matlab InternalDynamics=@WindshearGoAround_Dynamics_Internal; SimDynamics=@WindshearGoAround_Dynamics_Sim; ``` ```matlab problem.analyticDeriv.gradCost=[]; problem.analyticDeriv.hessianLagrangian=[]; problem.analyticDeriv.jacConst=[]; ``` ```matlab problem.settings=@settings_WindshearGoAround; ``` -------------------------------- ### Time variable configuration for BangBangTwoPhase_Phase1.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Configures initial and final time bounds and guesses for the first phase of the two-phase problem. ```matlab % Initial time. problem.time.t0_idx=1; problem.time.t0_min=problem_mp.time.t_min(problem.time.t0_idx); problem.time.t0_max=problem_mp.time.t_max(problem.time.t0_idx); guess.t0=guess_mp.time(problem.time.t0_idx); % Final time. Let tf_min=tf_max if tf is fixed. problem.time.tf_idx=2; problem.time.tf_min=problem_mp.time.t_min(problem.time.tf_idx); problem.time.tf_max=problem_mp.time.t_max(problem.time.tf_idx); guess.tf=guess_mp.time(problem.time.tf_idx); ``` -------------------------------- ### main_Hypersensitive.m - Solve and Simulate Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleHypersensitive.html Fetches problem, solves NLP, and simulates solution using ode113 with step size 0.01. ```matlab [problem,guess]=Hypersensitive; % Fetch the problem definition options= problem.settings(1,8); % for hp method % options= problem.settings(40); % for h method [solution,MRHistory]=solveMyProblem( problem,guess,options); tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 0.01 ); ``` -------------------------------- ### BangBang.m: initial state bounds Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang.html Sets the lower and upper bounds on the initial state conditions in BangBang.m. ```matlab problem.states.x0l=[0 0]; problem.states.x0u=[0 0]; ``` -------------------------------- ### Add MPC controller folders to path Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedCLSim.html In the initialization script (e.g., initSim.m), add the MPC controller folders to the MATLAB path so their files can be found. ```matlab addpath('MPCController1'); addpath('MPCController2'); ... ``` -------------------------------- ### Store Problem Parameters Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleMinFuelClimb.html Initializes the problem data structure with physical constants and lookup table data. ```matlab problem.data.CDdat = CDdat; problem.data.Clalphadat = Clalphadat; problem.data.etadat = etadat; problem.data.M = MachLT; problem.data.M2 = MachLTAero; problem.data.alt = AltLT; problem.data.T = ThrustLT; problem.data.Re = 6378145; problem.data.mu = 3.986e14; problem.data.S = 49.2386; problem.data.g0 = 9.80665; problem.data.Isp = 1600; problem.data.H = 7254.24; problem.data.rho0 = 1.225; problem.data.Atomsrho = Atomsrho; problem.data.Atomssos = Atomssos; [aa,mm] = meshgrid(AltLT,MachLT); problem.data.aa = aa; problem.data.mm = mm; ``` -------------------------------- ### Open-loop simulation with default ODE solver Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainSinglePhase.html Performs open-loop simulation of the obtained solution using the default ODE solver. Returns time, state, and input variables. ```matlab [ tv, xv, uv ] = simulateSolution( problem, solution); ``` -------------------------------- ### Store and declare variables in WindshearGoAround.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleWindshear.html Initializes auxiliary data structures, sets initial conditions for flight parameters, and defines variable bounds for the simulation. ```matlab % Store relevant data auxdata.a=a; auxdata.b=b_var; auxdata.c=c; auxdata.e=e; auxdata.d=d; auxdata.beta_0=beta_0; auxdata.beta_0_dot=beta_0_dot; auxdata.A_0=A_0; auxdata.A_1=A_1; auxdata.A_2=A_2; auxdata.B_0=B_0; auxdata.B_1=B_1; auxdata.B_2=B_2; auxdata.C_0=C_0; auxdata.C_1=C_1; auxdata.C_2=C_2; auxdata.g=g_var; auxdata.m=mg/g_var; auxdata.delta=delta; auxdata.rho=rho; auxdata.S=S; auxdata.alpha_star=alpha_star; auxdata.alpha_max=alpha_max; auxdata.W1_poly=W1_poly; auxdata.W2_poly=W2_poly; auxdata.W1_dot_poly=W1_dot_poly; auxdata.W2_dot_poly=W2_dot_poly; auxdata.beta_poly=beta_poly; auxdata.C_L_poly=C_L_poly; % initial conditions pos0 = 0; alt0 = 600; % Initial altitude (ft) speed0 = 239.7; % Initial speed (ft/s) fpa0 = -0.03925; % Initial flight path angle (rad) fpaf = 0.12969; % Final flight path angle (rad) alpha0 = 0.1283; % Initial angle of attack (rad) % variable bounds posmin = 0; posmax = 1e04; altmin = 1e02; altmax = 1000; speedmin = 0; speedmax = inf; fpamin = -inf; fpamax = inf; alphamin = -alpha_max; alphamax = alpha_max; umin = -0.05236; umax = 0.05236; ``` -------------------------------- ### Single solve workflow with main.m Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMain.html Standard workflow for a single solve: load the problem, set solver options (fixed-order h or hp methods), transcribe and solve the NLP, then construct the solution and generate plots. The output command's last argument selects which figures to produce (1: all, 2: state/input, 3: multipliers, 4: errors). ```matlab [problem,guess]=myProblem; ``` ```matlab options= settings_h(...); ``` ```matlab options= settings_hp(...,...); ``` ```matlab [infoNLP,data,options]=transcribeOCP(problem,guess,options); [solution,status,data] = solveNLP(infoNLP,data); ``` ```matlab [solution]=output(problem,solution,options,data,...); ``` -------------------------------- ### Time variable configuration for BangBangTwoPhase_Phase2.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Configures initial and final time bounds and guesses for the second phase of the two-phase problem. ```matlab % Initial time. problem.time.t0_idx=2; problem.time.t0_min=problem_mp.time.t_min(problem.time.t0_idx); problem.time.t0_max=problem_mp.time.t_max(problem.time.t0_idx); guess.t0=guess_mp.time(problem.time.t0_idx); % Final time. Let tf_min=tf_max if tf is fixed. problem.time.tf_idx=3; problem.time.tf_min=problem_mp.time.t_min(problem.time.tf_idx); problem.time.tf_max=problem_mp.time.t_max(problem.time.tf_idx); guess.tf=guess_mp.time(problem.time.tf_idx); ``` -------------------------------- ### Set initial state conditions Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleOrbitRaising.html Provides the initial state vector x0 for the problem. ```matlab problem.states.x0=[1 0 0 1]; ``` -------------------------------- ### Enable flexible mesh with options.adaptseg Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedOptionhp.html Enables the flexible mesh method, which automatically adjusts interval placement during optimization to accommodate discontinuities like bang-bang inputs. ```matlab options.adaptseg=1; ``` -------------------------------- ### BangBang.m: initial state conditions Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang.html Sets the initial conditions for the state variables in BangBang.m. ```matlab problem.states.x0=[0 0]; ``` -------------------------------- ### Define Boundary Conditions in CarParking.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleParallelParking.html Initializes state variables and defines limits for position, velocity, acceleration, and steering angle. ```matlab % Boundary Conditions posx0 = -5.14; posy0 = 1.41; theta0=deg2rad(6); v0=0; % Initial velocity (m/s) a0=0; % Initial accelration (m/s^2) phi0 = deg2rad(0); % Initial steering angle (rad) % Limits on Variables xmin = -10; xmax = 5; ymin = -SW; ymax = CL; vmin = -v_max; vmax = v_max; amin = -a_max; amax = a_max; thetamin = -inf; thetamax = inf; phimin = -phi_max; phimax = phi_max; ``` -------------------------------- ### State variable configuration for BangBangTwoPhase_Phase2.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Defines initial conditions, state bounds, terminal state bounds, and trajectory guesses for the second phase. ```matlab % Initial conditions for system. problem.states.x0l=[0 -200]; problem.states.x0u=[300 200]; % State bounds problem.states.xl=[0 -200]; problem.states.xu=[310 200]; % Terminal state bounds. problem.states.xfl=[0 0]; problem.states.xfu=[0 0]; % Guess the state trajectories with [x0 xf] guess.states(:,1)=[100 0]; guess.states(:,2)=[-50 0]; ``` -------------------------------- ### Configure Problem Handles in CarParking.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleParallelParking.html Sets the function handles for internal and simulation dynamics, configures derivative settings, and links the settings file. ```MATLAB InternalDynamics=@CarParking_Dynamics_Internal; SimDynamics=@CarParking_Dynamics_Sim; ``` ```MATLAB problem.analyticDeriv.gradCost=[]; problem.analyticDeriv.hessianLagrangian=[]; problem.analyticDeriv.jacConst=[]; ``` ```MATLAB problem.settings=@settings_CarParking; ``` -------------------------------- ### Data storage configuration for BangBangTwoPhase_Phase1.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Assigns data storage parameters for the first phase. ```matlab problem.data=problem_mp.data; ``` -------------------------------- ### Specify Phase Settings Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleGoddardRocket2.html Configures the three distinct phases of the optimal control problem and their respective settings. ```matlab [problem.phases{1},guess.phases{1}] = GoddardRocket_Phase1(problem.mp, guess.mp); [problem.phases{2},guess.phases{2}] = GoddardRocket_Phase2(problem.mp, guess.mp); [problem.phases{3},guess.phases{3}] = GoddardRocket_Phase3(problem.mp, guess.mp); phaseoptions{1}=settings_GoddardRocketThreePhase_Phase1(10); phaseoptions{2}=settings_GoddardRocketThreePhase_Phase2(10); phaseoptions{3}=settings_GoddardRocketThreePhase_Phase3(10); ``` -------------------------------- ### Define State Variables Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleParallelParking.html Configures initial conditions, state bounds, rate bounds, error tolerances, and terminal state bounds for the parking problem. ```matlab problem.states.x0=[posx0 posy0 v0 theta0 phi0]; ``` ```matlab problem.states.x0l=[posx0 posy0 v0 theta0 phi0]; problem.states.x0u=[posx0 posy0 v0 theta0 phi0]; ``` ```matlab problem.states.xl=[xmin ymin vmin thetamin phimin]; problem.states.xu=[xmax ymax vmax thetamax phimax]; ``` ```matlab problem.states.xrl=[-inf -inf -inf -inf -inf]; problem.states.xru=[inf inf inf inf inf]; ``` ```matlab problem.states.xErrorTol_local=[0.1 0.1 0.1 deg2rad(0.5) deg2rad(0.5)]; problem.states.xErrorTol_integral=[0.1 0.1 0.1 deg2rad(0.5) deg2rad(0.5)]; ``` ```matlab problem.states.xConstraintTol=[0.1 0.1 0.1 deg2rad(0.5) deg2rad(0.5)]; problem.states.xrConstraintTol=[0.1 0.1 0.1 deg2rad(0.5) deg2rad(0.5)]; ``` ```matlab problem.states.xfl=[l_rear ymin 0 theta0-deg2rad(5) -deg2rad(1)]; problem.states.xfu=[SL ymax 0 theta0+deg2rad(5) deg2rad(1)]; ``` ```matlab guess.time=[0 guess.tf/3 guess.tf*2/3 guess.tf]; guess.states(:,1)=[posx0 SL+l_rear l_rear SL-l_axes-l_front]; guess.states(:,2)=[posy0 posy0 -SW/2 -SW/2]; guess.states(:,3)=[v0 0 0 0]; guess.states(:,4)=[theta0 theta0 theta0 0]; guess.states(:,5)=[phi0 0 0 0]; ``` -------------------------------- ### Configure mintimeinterval option Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedOptionAuto.html Defines the minimum mesh interval size in actual time allowed for the mesh refinement process. ```text options.mintimeinterval=...; ``` -------------------------------- ### BangBang.m: initial state trajectory guess Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang.html Provides an initial guess for the state trajectory (optional but recommended) in BangBang.m. ```matlab guess.states(:,1)=[0 300]; guess.states(:,2)=[0 0]; ``` -------------------------------- ### Input variable configuration for BangBangTwoPhase_Phase2.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Defines input bounds and control action guesses for the second phase. ```matlab % Input bounds problem.inputs.ul=[-1]; problem.inputs.uu=[2]; % Bounds on first control action problem.inputs.u0l=[-1]; problem.inputs.u0u=[2]; % Guess the input sequences with [u0 uf] guess.inputs(:,1)=[-1 2]; ``` -------------------------------- ### Solve Goddard Rocket Three-Phase Problem in main_GoddardRocketThreePhase.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleGoddardRocket2.html This script fetches the problem definition and solver settings before executing the NLP solver. ```matlab options.mp= settings_GoddardRocketThreePhase; % Get options and solver settings [problem,guess,options.phaseoptions]=GoddardRocketThreePhase; % Fetch the problem definition [solution,MRHistory]=solveMyProblem( problem,guess,options); ``` -------------------------------- ### Set initial state bounds with problem.states.x0l/x0u Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblem.html Defines lower and upper bounds on the initial state values. Setting both bounds equal fixes the initial states. ```matlab problem.states.x0l=[x1(t0)_lowerbound ... xn(t0)_lowerbound]; problem.states.x0u=[x1(t0)_upperbound ... xn(t0)_upperbound]; ``` -------------------------------- ### Load the multi-phase problem definition Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainMultiPhase.html Loads the formulated optimal control problem from myMultiPhaseProblem.m into the variables problem, guess, and options.phaseoptions. ```matlab [problem,guess,options.phaseoptions]=myMultiPhaseProblem; ``` -------------------------------- ### BangBang.m: initial input trajectory guess Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang.html Provides an initial guess for the input trajectory (optional but recommended) in BangBang.m. ```matlab guess.inputs(:,1)=[1 -2]; ``` -------------------------------- ### Initialize phases of multi-phase OCP Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblemMultiPhase.html Initialize each phase of the multi-phase optimal control problem by calling the corresponding phase function (e.g., myMultiPhaseProblem_Phase1) with the shared problem.mp and guess.mp structures. The results are stored in the phases cell arrays. ```matlab [problem.phases{1},guess.phases{1}] = myMultiPhaseProblem_Phase1(problem.mp, guess.mp); [problem.phases{2},guess.phases{2}] = myMultiPhaseProblem_Phase2(problem.mp, guess.mp); ... ``` -------------------------------- ### Static parameter bounds for BangBangTwoPhase_Phase1.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Sets the bounds and initial guess for static parameters in the first phase. ```matlab % Parameters bounds. problem.parameters.pl=problem_mp.parameters.pl; problem.parameters.pu=problem_mp.parameters.pu; guess.parameters=guess_mp.parameters; ``` -------------------------------- ### GoddardRocket_Phase1.m data storage and cost function Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleGoddardRocket2.html Configures data storage and sets the boundary cost for the first phase. ```matlab problem.data=problem_mp.data; ``` ```matlab boundaryCost=0; ``` -------------------------------- ### Pre-process Data in MinEnergyClimbBryson.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleMinFuelClimb.html Pre-processes look-up tables for atmosphere, aerodynamics, and propulsion data using piecewise splines for use in ICLOCS2. ```matlab % Lookup Table 1: U.S. 1976 Standard Atmosphere (altitude, density and pressure) AtomsData = [-2000 1.478e+00 3.479e+02 0 1.225e+00 3.403e+02 2000 1.007e+00 3.325e+02 4000 8.193e-01 3.246e+02 6000 6.601e-01 3.165e+02 8000 5.258e-01 3.081e+02 10000 4.135e-01 2.995e+02 12000 3.119e-01 2.951e+02 14000 2.279e-01 2.951e+02 16000 1.665e-01 2.951e+02 18000 1.216e-01 2.951e+02 20000 8.891e-02 2.951e+02 22000 6.451e-02 2.964e+02 24000 4.694e-02 2.977e+02 26000 3.426e-02 2.991e+02 28000 2.508e-02 3.004e+02 30000 1.841e-02 3.017e+02 32000 1.355e-02 3.030e+02 34000 9.887e-03 3.065e+02 36000 7.257e-03 3.101e+02 38000 5.366e-03 3.137e+02 40000 3.995e-03 3.172e+02 42000 2.995e-03 3.207e+02 44000 2.259e-03 3.241e+02 46000 1.714e-03 3.275e+02 48000 1.317e-03 3.298e+02 50000 1.027e-03 3.298e+02 52000 8.055e-04 3.288e+02 54000 6.389e-04 3.254e+02 56000 5.044e-04 3.220e+02 58000 3.962e-04 3.186e+02 60000 3.096e-04 3.151e+02 62000 2.407e-04 3.115e+02 64000 1.860e-04 3.080e+02 66000 1.429e-04 3.044e+02 68000 1.091e-04 3.007e+02 70000 8.281e-05 2.971e+02 72000 6.236e-05 2.934e+02 74000 4.637e-05 2.907e+02 76000 3.430e-05 2.880e+02 78000 2.523e-05 2.853e+02 80000 1.845e-05 2.825e+02 82000 1.341e-05 2.797e+02 84000 9.690e-06 2.769e+02 86000 6.955e-06 2.741e+02]; % Lookup Table 2: Aerodynamic Data MachLTAero = [0 0.4 0.8 0.9 1.0 1.2 1.4 1.6 1.8]; ClalphaLT = [3.44 3.44 3.44 3.58 4.44 3.44 3.01 2.86 2.44]; CD0LT = [0.013 0.013 0.013 0.014 0.031 0.041 0.039 0.036 0.035]; etaLT = [0.54 0.54 0.54 0.75 0.79 0.78 0.89 0.93 0.93]; % Lookup Table 3: Propulsion Data (Thrust as function of mach number and altitude) MachLT = [0; 0.2; 0.4; 0.6; 0.8; 1; 1.2; 1.4; 1.6; 1.8]; AltLT = 304.8*[0 5 10 15 20 25 30 40 50 70]; ThrustLT = 4448.2*[24.2 24.0 20.3 17.3 14.5 12.2 10.2 5.7 3.4 0.1; 28.0 24.6 21.1 18.1 15.2 12.8 10.7 6.5 3.9 0.2; 28.3 25.2 21.9 18.7 15.9 13.4 11.2 7.3 4.4 0.4; 30.8 27.2 23.8 20.5 17.3 14.7 12.3 8.1 4.9 0.8; 34.5 30.3 26.6 23.2 19.8 16.8 14.1 9.4 5.6 1.1; 37.9 34.3 30.4 26.8 23.3 19.8 16.8 11.2 6.8 1.4; 36.1 38.0 34.9 31.3 27.3 23.6 20.1 13.4 8.3 1.7; 36.1 36.6 38.5 36.1 31.6 28.1 24.2 16.2 10.0 2.2; 36.1 35.2 42.1 38.7 35.7 32.0 28.1 19.3 11.9 2.9; 36.1 33.8 45.7 41.3 39.8 34.6 31.1 21.7 13.3 3.1]; % Fitting of Aerodynamic data with piecewise splines Clalphadat = pchip(MachLTAero,ClalphaLT); CDdat = pchip(MachLTAero,CD0LT); etadat = pchip(MachLTAero,etaLT); Atomsrho=pchip(AtomsData(:,1),AtomsData(:,2)); Atomssos=pchip(AtomsData(:,1),AtomsData(:,3)); ``` -------------------------------- ### Specify phases and settings for multiphase problem Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBangBang2.html Calls the phase definition functions for each phase and assigns the corresponding settings. The settings are taken from the phase settings array at index 20. ```matlab [problem.phases{1},guess.phases{1}] = BangBangTwoPhase_Phase1(problem.mp, guess.mp); [problem.phases{2},guess.phases{2}] = BangBangTwoPhase_Phase2(problem.mp, guess.mp); phaseoptions{1}=problem.phases{1}.settings(20); phaseoptions{2}=problem.phases{2}.settings(20); ``` -------------------------------- ### Set initial state bounds in CommericalFlightProfile.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleCommericalFlight.html Sets the lower and upper bounds for the initial state vector, both equal to the initial conditions. ```matlab problem.states.x0l=[H0 x0 y0 Vtas0 gamma0 chi0 W0]; problem.states.x0u=[H0 x0 y0 Vtas0 gamma0 chi0 W0]; ``` -------------------------------- ### Configure Problem Definition in TwoLinkRobotArm.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleRobotArm.html Sets up function handles, time variables, state variables, control variables, and cost functions for the ICLOCS2 problem definition. ```matlab InternalDynamics=@TwoLinkRobotArm_Dynamics_Internal; SimDynamics=@TwoLinkRobotArm_Dynamics_Sim; ``` ```matlab problem.analyticDeriv.gradCost=[]; problem.analyticDeriv.hessianLagrangian=[]; problem.analyticDeriv.jacConst=[]; ``` ```matlab problem.settings=@settings_TwoLinkRobotArm; ``` ```matlab problem.time.t0_min=0; problem.time.t0_max=0; guess.t0=0; ``` ```matlab problem.time.tf_min=0.1; problem.time.tf_max=inf; guess.tf=3.1; ``` ```matlab problem.states.x0=[0 0 0 0]; ``` ```matlab problem.states.x0l=[0 0 0 0]; problem.states.x0u=[0 0 0 0]; ``` ```matlab problem.states.xl=[-inf -inf -inf -inf]; problem.states.xu=[inf inf inf inf]; ``` ```matlab problem.states.xErrorTol_local=[deg2rad(0.1) deg2rad(0.1) deg2rad(0.05) deg2rad(0.05)]; problem.states.xErrorTol_integral=[deg2rad(0.1) deg2rad(0.1) deg2rad(0.05) deg2rad(0.05)]; ``` ```matlab problem.states.xConstraintTol=[deg2rad(0.1) deg2rad(0.1) deg2rad(0.05) deg2rad(0.05)]; ``` ```matlab problem.states.xfl=[0 0 0.5 0.522]; problem.states.xfu=[0 0 0.5 0.522]; ``` ```matlab guess.states(:,1)=[0.1 0]; guess.states(:,2)=[0.1 0]; guess.states(:,3)=[0 0.5]; guess.states(:,4)=[0 0.522]; ``` ```matlab problem.inputs.ul=[-1 -1]; problem.inputs.uu=[1 1]; ``` ```matlab problem.inputs.u0l=[-1 -1]; problem.inputs.u0u=[1 1]; ``` ```matlab problem.inputs.uConstraintTol=[0.01 0.01]; ``` ```matlab guess.inputs(:,1)=[1 -1]; guess.inputs(:,2)=[1 -1]; ``` ```matlab stageCost = 0.01*(u(:,1).*u(:,1)+u(:,2).*u(:,2)); ``` ```matlab boundaryCost=tf; ``` -------------------------------- ### Set initial input bounds with problem.inputs.u0l/u0u Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblem.html Defines bounds on the first control action at the initial time. ```matlab problem.inputs.u0l=[u1_lowerbound(t0) ... um_lowerbound(t0)]; problem.inputs.u0u=[u1_upperbound(t0) ... um_upperbound(t0)]; ``` -------------------------------- ### Set static parameters in myProblem.m Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblemSingle.html Define the lower and upper bounds and initial guess for static parameters that are part of the optimization variables. ```matlab problem.parameters.pl=[p1_lowerbound ...]; problem.parameters.pu=[p1_upperbound ...]; guess.parameters=[p1_guess p2_guess ...]; ``` -------------------------------- ### Fetch problem definition in controller script Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedCLSim.html In the controller script (e.g., ICLOCS_NLPSolver_Phase1.m), fetch the problem definition for the MPC controller. This line is the replacement for the main file used in single-phase optimal control problems. ```matlab [problem,guess]=MyProblem_Phase1; ``` -------------------------------- ### Set initial guess for states in myProblem.m Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblemSingle.html Provide an initial guess of the state values at various times along the trajectory. If guess.time is not specified, guess.states can take a two-point guess corresponding to guess.t0 and guess.tf. ```matlab guess.time=[t0 ... tf]; guess.states(:,1)=[x1(t0) ... x1(tf)]; % ... guess.states(:,n)=[xn(t0) ... xn(tf)]; ``` -------------------------------- ### Transcribe and solve the multi-phase problem Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainMultiPhase.html Transcribes and solves the problem using the standard command solveMyProblem. The returned solution contains raw NLP solver output, continuous time solution, and error analysis results. ```matlab [solution,MRHistory]=solveMyProblem( problem,guess,options); ``` -------------------------------- ### Set initial time with problem.time.t0 Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedProblem.html Sets the initial time for the optimal control problem. The initial time is fixed at 0. ```matlab problem.time.t0=0; ``` -------------------------------- ### Define Scenario Parameters in CarParking.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleParallelParking.html Defines physical dimensions and constraints for the parking scenario and stores them in the auxdata structure. ```MATLAB % Scenario Parameters SL=5; SW=2; CL=3.5; l_front=0.839; l_axes=2.588; l_rear=0.657; b_width=1.771/2; phi_max=deg2rad(33); a_max=0.75; v_max=2; u1_max=0.5; curvature_dot_max=0.6; % Store data auxdata.SL=SL; auxdata.SW=SW; auxdata.CL=CL; auxdata.l_front=l_front; auxdata.l_axes=l_axes; auxdata.l_rear=l_rear; auxdata.b_width=b_width; ``` -------------------------------- ### Set analytic derivative function handles in BatchFermentor.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBatchFermentor.html Provides function handles for the optional analytic derivatives: cost gradient, Lagrangian Hessian, and constraint Jacobian. These files are optional but can improve solver performance. ```matlab problem.analyticDeriv.gradCost=@gradCost_BatchFermentor; problem.analyticDeriv.hessianLagrangian=@hessianLagrangian_BatchFermentor; problem.analyticDeriv.jacConst=@jacConst_BatchFermentor; ``` -------------------------------- ### Hypersensitive.m - Control Initial Action Bounds Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleHypersensitive.html Bounds on the first control action. ```matlab problem.inputs.u0l=-inf; problem.inputs.u0u=inf; ``` -------------------------------- ### Open-loop simulation with ODE solver and time step Source: http://www.ee.ic.ac.uk/ICLOCS/GetStartedMainSinglePhase.html Performs open-loop simulation with a specified ODE solver and integration time step. ```matlab [ tv, xv, uv ] = simulateSolution( problem, solution, 'ode113', 'ode113', 0.01 ); ``` -------------------------------- ### Hypersensitive.m - Initial Guess for State Trajectory Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleHypersensitive.html Optional but recommended initial guess for the state trajectory. ```matlab guess.states(:,1)=[0 0]; ``` -------------------------------- ### GoddardRocket_Phase1.m input variable bounds Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleGoddardRocket2.html Defines input bounds, control action bounds, and initial guesses for the first phase. ```matlab % Input bounds problem.inputs.ul=[T_max]; problem.inputs.uu=[T_max]; % Bounds on first control action problem.inputs.u0l=[T_max]; problem.inputs.u0u=[T_max]; % Input constraint error bounds problem.inputs.uConstraintTol=[0.1]; % Guess the input sequences with [u0 uf] guess.inputs(:,1)=[T_max T_max]; ``` -------------------------------- ### Set initial state bounds in BatchFermentor.m Source: http://www.ee.ic.ac.uk/ICLOCS/ExampleBatchFermentor.html Defines the lower and upper bounds on the initial state values. ```matlab problem.states.x0l=[1.5 0.0 0.0 7]; problem.states.x0u=[40 50 25 10]; ```