Skip to content

Commit 045e791

Browse files
committed
Initial GitHub commit. Modifications listed below based on version 1.0 on the MATLAB File Exchange.
Fixes - Fixed control continuity constraints (should not have been present) Changes - PS_Fmatrix requires the derivative function to be vectorized now Features - Added capability to solve variable final time problems - Added moon lander example (variable horizon problem) - Added defaults options function (see the function for the options)
0 parents  commit 045e791

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2884
-0
lines changed

INSTALL_Basic_Pseudospectral.m

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
%--------------------------------------------------------------------------
2+
% INSTALL_Basic_Pseudospectral
3+
% This scripts helps you get this code up and running
4+
%--------------------------------------------------------------------------
5+
% Automatically adds project files to your MATLAB path, downloads the
6+
% required files, and opens an example.
7+
%--------------------------------------------------------------------------
8+
% Install script based on MFX Submission Install Utilities
9+
% https://github.com/danielrherber/mfx-submission-install-utilities
10+
% https://www.mathworks.com/matlabcentral/fileexchange/62651
11+
%--------------------------------------------------------------------------
12+
% Primary Contributor: Daniel R. Herber, Graduate Student, University of
13+
% Illinois at Urbana-Champaign
14+
% Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral
15+
%--------------------------------------------------------------------------
16+
function INSTALL_Basic_Pseudospectral
17+
18+
% add contents to path
19+
AddSubmissionContents(mfilename)
20+
21+
% download required web files
22+
RequiredWebFiles
23+
24+
% download required web zips
25+
RequiredWebZips
26+
27+
% add contents to path
28+
AddSubmissionContents(mfilename)
29+
30+
% open examples
31+
OpenThisFile('BD_main')
32+
33+
% close this file
34+
CloseThisFile(mfilename) % this will close this file
35+
36+
end
37+
%--------------------------------------------------------------------------
38+
function RequiredWebFiles
39+
disp('--- Obtaining required web files')
40+
41+
% initialize index
42+
ind = 0;
43+
44+
% initialize structure
45+
files = struct('url','','folder','');
46+
47+
% file 1
48+
ind = ind + 1; % increment
49+
files(ind).url = 'http://dmpeli.math.mcmaster.ca/Matlab/Math4Q3/Lecture2-1/LagrangeInter.m';
50+
files(ind).folder = 'LagrangeInter';
51+
52+
% file 2
53+
ind = ind + 1; % increment
54+
files(ind).url = 'http://www1.spms.ntu.edu.sg/~lilian/bookcodes/legen/lepoly.m';
55+
files(ind).folder = 'lepoly';
56+
57+
% obtain full function path
58+
full_fun_path = which(mfilename('fullpath'));
59+
outputdir = fullfile(fileparts(full_fun_path),'include');
60+
61+
% download
62+
DownloadWebFiles(files,outputdir)
63+
64+
disp(' ')
65+
end
66+
%--------------------------------------------------------------------------
67+
function RequiredWebZips
68+
disp('--- Obtaining required web zips')
69+
70+
% initialize index
71+
ind = 0;
72+
73+
% initialize structure
74+
zips = struct('url','','folder','','test','');
75+
76+
% zip 1
77+
ind = ind + 1; % increment
78+
zips(ind).url = 'https://github.com/altmany/export_fig/archive/master.zip';
79+
zips(ind).folder = 'MFX 23629';
80+
zips(ind).test = 'export_fig';
81+
82+
% zip 2
83+
ind = ind + 1; % increment
84+
zips(ind).url = 'http://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/40397/versions/7/download/zip/mfoldername_v2.zip';
85+
zips(ind).folder = 'MFX 40397';
86+
zips(ind).test = 'mfoldername';
87+
88+
% obtain full function path
89+
full_fun_path = which(mfilename('fullpath'));
90+
outputdir = fullfile(fileparts(full_fun_path),'include');
91+
92+
% download and unzip
93+
DownloadWebZips(zips,outputdir)
94+
95+
disp(' ')
96+
end
97+
%--------------------------------------------------------------------------
98+
function AddSubmissionContents(name)
99+
disp('--- Adding submission contents to path')
100+
disp(' ')
101+
102+
% current file
103+
fullfuncdir = which(name);
104+
105+
% current folder
106+
submissiondir = fullfile(fileparts(fullfuncdir));
107+
108+
% add folders and subfolders to path
109+
addpath(genpath(submissiondir))
110+
end
111+
%--------------------------------------------------------------------------
112+
function CloseThisFile(name)
113+
disp(['--- Closing ', name])
114+
disp(' ')
115+
116+
% get editor information
117+
h = matlab.desktop.editor.getAll;
118+
119+
% go through all open files in the editor
120+
for k = 1:numel(h)
121+
% check if this is the file
122+
if ~isempty(strfind(h(k).Filename,name))
123+
% close this file
124+
h(k).close
125+
end
126+
end
127+
end
128+
%--------------------------------------------------------------------------
129+
function OpenThisFile(name)
130+
disp(['--- Opening ', name])
131+
132+
try
133+
% open the file
134+
open(name);
135+
catch % error
136+
disp(['Could not open ', name])
137+
end
138+
139+
disp(' ')
140+
end
141+
%--------------------------------------------------------------------------
142+
function DownloadWebFiles(files,outputdir)
143+
144+
% store the current directory
145+
olddir = pwd;
146+
147+
% create a folder for outputdir
148+
if ~exist(outputdir, 'dir')
149+
mkdir(outputdir); % create the folder
150+
else
151+
addpath(genpath(outputdir)); % add folders and subfolders to path
152+
end
153+
154+
% change to the output directory
155+
cd(outputdir)
156+
157+
% go through each file
158+
for k = 1:length(files)
159+
160+
% get data
161+
url = files(k).url;
162+
folder = files(k).folder;
163+
[~,nameurl,exturl] = fileparts(url);
164+
name = [nameurl,exturl];
165+
166+
% first check if the test file is in the path
167+
if exist(name,'file') == 0
168+
169+
try
170+
% download file
171+
outfilename = websave(name,url);
172+
173+
% create a folder utilizing name as the foldername name
174+
if ~exist(fullfile(outputdir,folder), 'dir')
175+
mkdir(fullfile(outputdir,folder));
176+
end
177+
178+
% move the file
179+
movefile(outfilename,fullfile(outputdir,folder))
180+
181+
% output to the command window
182+
disp(['Downloaded ',folder,'/',name])
183+
184+
catch % failed to download
185+
% output to the command window
186+
disp(['Failed to download ',folder,'/',name])
187+
188+
% remove the html file
189+
delete([name,'.html'])
190+
end
191+
192+
else
193+
% output to the command window
194+
disp(['Already available ',name])
195+
end
196+
end
197+
198+
% change back to the original directory
199+
cd(olddir)
200+
end
201+
%--------------------------------------------------------------------------
202+
function DownloadWebZips(zips,outputdir)
203+
204+
% store the current directory
205+
olddir = pwd;
206+
207+
% create a folder for outputdir
208+
if ~exist(outputdir, 'dir')
209+
mkdir(outputdir); % create the folder
210+
else
211+
addpath(genpath(outputdir)); % add folders and subfolders to path
212+
end
213+
214+
% change to the output directory
215+
cd(outputdir)
216+
217+
% go through each zip
218+
for k = 1:length(zips)
219+
220+
% get data
221+
url = zips(k).url;
222+
folder = zips(k).folder;
223+
test = zips(k).test;
224+
225+
% first check if the test file is in the path
226+
if exist(test,'file') == 0
227+
228+
try
229+
% download zip file
230+
zipname = websave(folder,url);
231+
232+
% save location
233+
outputdirname = fullfile(outputdir,folder);
234+
235+
% create a folder utilizing name as the foldername name
236+
if ~exist(outputdirname, 'dir')
237+
mkdir(outputdirname);
238+
end
239+
240+
% unzip the zip
241+
unzip(zipname,outputdirname);
242+
243+
% delete the zip file
244+
delete([folder,'.zip'])
245+
246+
% output to the command window
247+
disp(['Downloaded and unzipped ',folder])
248+
249+
catch % failed to download
250+
% output to the command window
251+
disp(['Failed to download ',folder])
252+
253+
% remove the html file
254+
delete([folder,'.html'])
255+
256+
end
257+
258+
else
259+
% output to the command window
260+
disp(['Already available ',folder])
261+
end
262+
end
263+
264+
% change back to the original directory
265+
cd(olddir)
266+
end

License

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2015, Daniel Herber
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## README (basic-multiple-interval-pseudospectral)
2+
3+
[![GitHub release](https://img.shields.io/github/release/danielrherber/basic-multiple-interval-pseudospectral.svg)](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/releases/latest)
4+
[![](https://img.shields.io/badge/language-matlab-EF963C.svg)](https://www.mathworks.com/products/matlab.html)
5+
[![](https://img.shields.io/github/issues-raw/danielrherber/basic-multiple-interval-pseudospectral.svg)](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/issues)
6+
[![GitHub contributors](https://img.shields.io/github/contributors/danielrherber/basic-multiple-interval-pseudospectral.svg)](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/graphs/contributors)
7+
8+
[![license](https://img.shields.io/github/license/danielrherber/basic-multiple-interval-pseudospectral.svg)](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/blob/master/License)
9+
10+
This project implements multiple-interval pseudospectral methods to solve optimal control problems.
11+
12+
![readme_image.svg](http://www.danielherber.com/img/projects/basic-multiple-interval-pseudospectral/readme_image.svg)
13+
14+
---
15+
### Install
16+
- Download the [project files](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/archive/master.zip)
17+
- Run [INSTALL_Basic_Pseudospectral.m](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/blob/master/INSTALL_Basic_Pseudospectral.m) in the MATLAB Command Window *(automatically adds project files to your MATLAB path, downloads the required files, and opens an example)*
18+
```matlab
19+
INSTALL_Basic_Pseudospectral
20+
```
21+
- See [BD_main.m](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/blob/master/examples/BD_main.m) to run the Bryson-Denham example
22+
```matlab
23+
open BD_main
24+
```
25+
- See the technical report [[PDF]](http://systemdesign.illinois.edu/publications/Her15a.pdf) for the theory and case study results
26+
27+
### Citation
28+
The code is complementary material for the following publication:
29+
- DR Herber. **Basic Implementation of Multiple-Interval Pseudospectral Methods to Solve Optimal Control Problems.** Technical report, Engineering System Design Lab, UIUC-ESDL-2015-01, Urbana, IL, USA, Jun 2015. [[PDF]](http://systemdesign.illinois.edu/publications/Her15a.pdf)
30+
31+
### Description
32+
The two numerical schemes are used: the Legendre pseudospectral method with LGL nodes and the Chebyshev pseudospectral method with CGL nodes. The results from the case studies using the Bryson-Denham problem demonstrate the effect of user's choice in mesh parameters and little difference between the two numerical pseudospectral schemes. The solution procedure is independent of Bryson-Denham problem the test so other optimal control problems can be solved with the accompanying code.
33+
34+
The main purpose of this submission is to provide a reference for the basic implementation of multiple-interval pseudospectral methods. Paired with the technical report of the same name, I hope to help bring this advanced method for solving optimal control problems to a broader audience (especially in the classroom).
35+
36+
### External Includes
37+
See [INSTALL_Basic_Pseudospectral.m](https://github.com/danielrherber/basic-multiple-interval-pseudospectral/blob/master/INSTALL_Basic_Pseudospectral.m) for more information
38+
- MATLAB File Exchange Submission IDs (**23629**, **40397**)
39+
- Code from Lloyd N. Trefethen. **Spectral Methods in MATLAB**, SIAM, 2000. [[URL]](https://people.maths.ox.ac.uk/trefethen/spectral.html)
40+
- Code from J. Shen, T. Tang, and L. Wang. **Spectral Methods: Algorithms, Analysis and Applications**, Springer, 2011. [[URL]](http://www.ntu.edu.sg/home/lilian/book.htm)
41+
42+
---
43+
### General Information
44+
45+
#### Contributors
46+
- [Daniel R. Herber](https://github.com/danielrherber)
47+
48+
#### Project Links
49+
- [https://github.com/danielrherber/basic-multiple-interval-pseudospectral](https://github.com/danielrherber/basic-multiple-interval-pseudospectral)
50+
- [http://www.mathworks.com/matlabcentral/fileexchange/51104](http://www.mathworks.com/matlabcentral/fileexchange/51104)

examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!.gitignore
2+
_private/

examples/bryson-denham/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!.gitignore
2+
saved/

examples/bryson-denham/BD_boundary.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
%--------------------------------------------------------------------------
2+
% BD_boundary.m
3+
% Bryson-Denham boundary constraint function
4+
%--------------------------------------------------------------------------
5+
% b = BD_boundary(X,U,p)
6+
% X: state
7+
% U: control
8+
% p: parameter structure
9+
% b: boundary constraint vector
10+
%--------------------------------------------------------------------------
11+
% Primary Contributor: Daniel R. Herber, Graduate Student, University of
12+
% Illinois at Urbana-Champaign
13+
% Link: https://github.com/danielrherber/basic-multiple-interval-pseudospectral
14+
%--------------------------------------------------------------------------
15+
function b = BD_boundary(X,U,p)
16+
% initialize boundary constraint
17+
b = [];
18+
19+
% position boundary constraints
20+
b = [b;X(1,1)-p.prob.p0;X(end,1)-p.prob.pf];
21+
22+
% velocity boundary constraints
23+
b = [b;X(1,2)-p.prob.v0;X(end,2)-p.prob.vf];
24+
end

0 commit comments

Comments
 (0)