matlab(r2023a)添加工具箱TooLbox的方法-以GPOPS为例
•
编程语言
一、找到工具箱存放位置
首先我们需要找到工具箱的存放位置,点击这个设置路径可以看到

我们的matlab工具箱的存放位置
C:\Program Files\MATLAB\R2023a\toolbox\matlab
从资源管理器中打开这个位置,可以看到里面各种工具箱
二、放入工具箱
解压我们的工具箱gpops,可以看到里面有两个文件夹
将这两个文件夹直接放到上面的工具箱存放位置,可以复制也可以剪切。
三、添加文件
这时我们可以添加我们的工具箱了
点击添加并包含子文件,找到之前的那两个文件的位置,也就是
C:\Program Files\MATLAB\R2023a\toolbox\matlab\lib
C:\Program Files\MATLAB\R2023a\toolbox\matlab\nlp
将这两个文件添加并包含子文件。
然后就可以在matlab程序中用这两个工具箱了
四、测试GPOPS
刚刚我们添加的工具箱是GPOPS2的工具箱,现在可以测试一下这个工具箱
clc;clear;close all; tic; % 设置时间 t0 = 0; tf = 2; % 设置状态量初值 x10 = -2; x20 = 1; % 设置状态量边界条件 x1Min = -5; x1Max = 5; x2Min = -5; x2Max = 5; % 设置控制量边界条件 uMin = -1.5; uMax = 1.5; bounds.phase.initialtime.lower = t0; bounds.phase.initialtime.upper = t0; bounds.phase.finaltime.lower = tf; bounds.phase.finaltime.upper = tf; bounds.phase.initialstate.lower = [x10 x20]; bounds.phase.initialstate.upper = [x10 x20]; bounds.phase.state.lower = [x1Min x2Min]; bounds.phase.state.upper = [x1Max x2Max]; bounds.phase.finalstate.lower = [0 0]; bounds.phase.finalstate.upper = [0 0]; bounds.phase.control.lower = uMin; bounds.phase.control.upper = uMax; bounds.phase.integral.lower = 0; bounds.phase.integral.upper = 10000; guess.phase.time = [t0; tf]; guess.phase.state = [[x10; 0],[x20; 0]]; guess.phase.control = [1; uMin]; guess.phase.integral = 100; setup.name = 'Vehicle-Stopping-OCP'; setup.functions.continuous = @vsopcContinuous; setup.functions.endpoint = @vsopcEndpoint; setup.bounds = bounds; setup.guess = guess; setup.nlp.solver = 'snopt'; setup.derivatives.supplier = 'sparseCD'; setup.derivatives.derivativelevel = 'second'; setup.mesh.method = 'hp1'; setup.mesh.tolerance = 1e-6; setup.mesh.maxiteration = 45; setup.mesh.colpointsmax = 4; setup.mesh.colpointsmin = 10; setup.mesh.phase.fraction = 0.1*ones(1,10); setup.mesh.phase.colpoints = 4*ones(1,10); setup.method = 'RPMintegration'; output = gpops2(setup); solution = output.result.solution; toc; %% 函数模块部分 % ----------------------------------------------------------------------- % % ------------------------- BEGIN: vsopcContinuous.m -------------------- % % ----------------------------------------------------------------------- % function phaseout = vsopcContinuous(input) t = input.phase.time; % x1 = input.phase.state(:,1); x2 = input.phase.state(:,2); u = input.phase.control(:,1); dx1 = x2; dx2 = u; phaseout.dynamics = [dx1, dx2]; phaseout.integrand = 0.5*u.^2; end % ----------------------------------------------------------------------- % % -------------------------- END: vsopcContinuous.m --------------------- % % ----------------------------------------------------------------------- % % ----------------------------------------------------------------------- % % -------------------------- BEGIN: vsopcEndpoint.m --------------------- % % ----------------------------------------------------------------------- % function output = vsopcEndpoint(input) J = input.phase.integral; output.objective = J; end % ----------------------------------------------------------------------- % % --------------------------- END: vsopcEndpoint.m ---------------------- % % ----------------------------------------------------------------------- %
这里我们直接使用了别人的程序来进行测试,测试结果是正确的。
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/b653f10782.html
