Matlab: Quadratic programming

Syntax

x = quadprog(H,f)
x = quadprog(H,f,A,b)
x = quadprog(H,f,A,b,Aeq,beq)

 

\min \frac{1}{2}x^{T}H{}x + f^{T} x,\\ s.t. \begin{cases} A x \leq B \\ A_{eq} \cdot x = B_{eq} \end{cases}

 

H: Symmetric matrix of doubles. Represents the quadratic in the expression 1/2*x’*H*x + f’*x. (Obviously, H Should be the Hessian matrix of original function)
f: Vector of doubles. Represents the linear term in the expression 1/2*x’*H*x + f’*x
A: Matrix of doubles. Represents the linear coefficients in the constraints A*x ≤ b.
b: Vector of doubles. Represents the constant vector in the constraints A*x ≤ b.
Aeq: Matrix of doubles. Represents the linear coefficients in the constraints Aeq*x = beq.
beq: Vector of doubles. Represents the constant vector in the constraints Aeq*x = beq.

If we want to solve a linear system as shown below:

Px=q

where H=2P'*P, f=-2*P*q

 

 


 

Rules for Capitalization in Titles of Articles

If you have a look at the title of this article you will see that some letters are capitalized and some are not. Although the capitalization of titles can sometimes depend on the particular style of a writer, institution or publication, there are some general rules to keep in mind.

Style Guides

The rules for capitalizing titles not only of articles, but also books, papers, speeches, etc, can vary according to a particular style guide, such as Associated Press Stylebook (AP), Chicago Manual of Style, and MLA style. This is known as title case. While you will find similarities between each guide, it’s important to pay attention to their differences.

Style guide similarities:

  • In all three styles, always capitalize the first and last word of any title.
    • How to Land Your Dream Job
  • In all three styles, capitalize nouns, pronouns, verbs, adjectives, and adverbs.
    • Visiting Beautiful Ruins (noun)
    • As She Ran Away (pronoun)
    • The Importance of Learning Fast (verb)
    • The Poky Little Puppy (adjective)
    • She Quietly Waits (adverb)
  • In all three styles, do not capitalize articles, prepositions, or coordinating conjunctions.
    • To Catch a Thief (article)
    • One Year in Paris (preposition)
    • Magic and Daybreak (coordinating conjunction)

Style guide differences:

  • In the AP Stylebook, all words with three letters or less are lowercased. However, if any of those words are verbs (is, are, was, be), they are to be capitalized.
  • In the Chicago Manual of Style, all prepositions are lowercased, even the lengthier ones (between, among, throughout).
  • In MLA style, words with three letters or less are always lowercased.

So, which one should you choose? Well, it all depends if a certain style is required by your teacher, course, or subject/field. For example, MLA style is commonly used in the liberal arts or humanities. AP style is popularly used in journalism, Chicago is often used in business. A suggestion is to choose one style, or check to see what style is required by your teacher or editor, and stick to it.

The General Rules for Title Case

As we can see, there are some exceptions to the general rules for title case set forth by each style guide, but they mostly follow a similar pattern. We know to capitalize the first, last, and important words in a title. Important words include nouns, pronouns, verbs, adverbs, and more. So, generally, these parts of speech are capitalized in titles:

“Short” words, those with less than five letters, are lowercase in titles, unless they are the first or last words. Generally, we do not capitalize:

  • Articles – a, an, the
  • Coordinating Conjunctions (fewer than five letters) – and, but, or, for, nor, etc.
  • Prepositions (fewer than five letters) – on, at, to, from, by, etc.

When in doubt and you do not have a reference guide in front of you, here is one general rule recommended by The U.S. Government Printing Office Style Manual:

“Capitalize all words in titles of publications and documents, except a, an, the, at, by, for, in, of, on, to, up, and, as, but, or, and nor.”

What About Sentence Case?

Now that we know some of the ins and outs of title case, let’s take a look at sentence case. In sentence case, the title is written as if it is a sentence. This is considered a more casual style and is commonly used in newspapers and on the web.

Only the first word has a capital letter:

  • Budget wedding invitations
  • Best technology blogs

However, proper nouns within the title are also capitalized:

  • Top 10 things to do in Paris
  • Hiking at the Grand Canyon

Whether you’re writing in title case or sentence case, every style guide is just a little bit different. You might discover that some publications are moving toward sentence case. There are a couple reasons why writers choose this over title case.

First, one could argue that capitalized words slow down a reader’s ability to scan. A title written in sentence case could be perceived as having an uninterrupted flow. Next, some publications prefer this style simply because it’s more likely to preserve consistency. With sentence case, there’s no nitpicking over the capitalization of a three-letter preposition.

You might notice an overall trend toward this style. Many heavy hitters in the publishing industry use sentence case, including The Boston Globe, LA Times, and USA Today. However, if you pick up a copy of The New York Times, you’ll see they stick with Title Case.

Advanced Rules to Note

One of the beautiful complexities of the English language is that, for every rule you learn, there’s probably an exception. Here are some advanced rules for title capitalization:

Hyphenated Titles

Let’s take a look at the Chicago Manual of Style’s guidelines:

  • Capitalize the first element.
  • Capitalize subsequent elements unless they are articles, prepositions, or coordinating conjunctions (and, but, for, or, nor).
    • High-Quality Web Services
    • First-Rate U.S. Lawyers
    • Bed-and-Breakfast Options in Savannah
  • Capitalize the second element in a hyphenated spelled-out number.
    • Forty-Ninth Street Blues
  • Do not capitalize the second element if the first element is a prefix that could not stand alone by itself (anti or pre).
    • Anti-inflammatory Dieting

Open Compounds

An open compound comes to life when a modifying adjective is used in conjunction with a noun. This creates a new noun. Hopefully warning bells will signal in your mind, as nouns are almost always capitalized.

  • Salad Dressing Recipes
  • The Best Science Fiction and Fantasy of the Year

The First Word Following a Colon

Let’s take a look at both the Chicago and AP Style guidelines:

  • Capitalize the first word after a colon.
    • Feminine Poetry: Ten Women Writers from Around the World
  • Capitalize the first word after a colon if it begins an independent clause.
    • I know who you are: You are my friend
  • Do not capitalize the first word after a colon if the clause cannot stand alone.
    • I know who you are: nobody

Prepositions That Belong to a Phrasal Verb

Prepositions often find themselves on the ‘do not capitalize’ list. However, when a preposition becomes an important part of a phrasal verb, it does need to be capitalized.

  • How to Back Up a Computer
  • Turn Down the Heat to Save You Money

Following the Rules

If you are debating how to capitalize titles in research papers and articles, your professor or editor will most likely delegate a certain style. In that case, make sure you visit the handbook on that style guide’s website. There will be ample guidance and examples. Aside from that, there are a wealth of other resources and handy tools out there. As you craft your titles, pay careful attention not only to the type of word, but also the length and placement of each word.

Furthermore, no matter your personal preference, make sure you write the exact titles of books, newspapers, journals, etc. as they are written on the original document (even if they do not follow common capitalization rules).
Read more at http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html#0Wk75HWepmOBfkjd.99


Rules for Capitalization in Titles of Articles

Read more at http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html#0Wk75HWepmOBfkjd.99

MATLAB R2015b的Coder工具使用实例(一),转自http://blog.csdn.net/xiaoxuzaizi/article/details/50394866

版权声明:
1、这篇教程参考如下:

参考1:举例说明使用MATLAB Coder从MATLAB生成C/C++代码步骤

(PS:这篇参考用得是老版本的MATLAB上的Coder,步骤和我这个不同,有兴趣的可以参考)

2、这篇文章用的实例不会太简单,也不会太复杂,力求可以把问题说清楚。

3、本着尊重原创,大家转载的时候请注明出处,此教程编写不易啊~~

下面开始介绍:MATLAB R2015b的Coder工具使用实例(一)

一、背景

MATLAB的强大之处我就不多说了,语法简答,顾虑少,我们很多算法可以用MATLA来编写与验证。C++的威力我也不多说了,计算效率很高。

但是!!MATLAB计算效率低下,无法用于实际应用中,比如嵌入式系统。C++语法严格,顾虑极多,因此让许多工程人员头疼。美国MathWorks公司貌似意识到了这个问题,自MATLAB2011开始,有了一款附于MATLAB的强大神器——Coder,它的作用:可以将你的复杂的MATLAB代码近乎自动地转换成C\C++代码,完美程度达99%。至于为什么不是100%,下面的演示会体现这一点。

二、运行环境
1、win7_32位系统;(64位亦可)
2、Microsoft Visual Studio 2008;(安装路径:C:\Program Files\Microsoft Visual Studio 9.0)(其他版本亦可)
3、MATLAB R2015b。(安装路径:C:\Program Files\MATLAB\R2015b)(注意!!不推荐其他版本,就用这版!!如果以后有了新版本的MATLAB问世,那就尽量用最新版)

三、详细步骤

(1)

在MATLAB R2015b中,创建一个名为“test.m”的函数文件,代码如下

  1. function [A,B] = test( mark,num,array )%输入参数中,mark、num是标量,array是向量
  2. a=ones(1,5);
  3. b=zeros(1,5);
  4.     if mark==0
  5.         for i=1:num
  6.              if i==1
  7.                  A=a;
  8.                  B=b;
  9.              else
  10.                  A=[A,a];
  11.                  B=[B,b];
  12.              end
  13.         end
  14.     else
  15.         for i=1:num
  16.              if i==1
  17.                  A=a;
  18.                  B=b;
  19.              else
  20.                  A=[A,array];
  21.                  B=[B,array];
  22.              end
  23.         end
  24.     end
  25. end

代码实现的功能很清晰,我就不多说了。现在,我们来用MATLAB R2015b自带工具Coder3.0来将上面的代码转化成C++代码。

(2)新建一个脚本文件main.m,用来试运行上述代码【这一步一定要做,不能轻视】

脚本文件main.m中的代码如下

然后运行脚本文件main.m,在命令行窗口得到如下结果

A =

1 至 18 列

1 1 1 1 1 1 2 5 6 8 1 2 5 6 8 1 2 5

19 至 20 列

6 8

B =

1 至 18 列

0 0 0 0 0 1 2 5 6 8 1 2 5 6 8 1 2 5

19 至 20 列

6 8


结果没有问题。接下来开始真正的转换工作。

(3)

在命令行窗口输入

  1. mex -setup %”-“前有空格,请注意!

 

耐心等待,【出现】:

MEX 配置为使用 ‘Microsoft Visual C++ 2008 Professional (C)’ 以进行 C 语言编译。
警告: MATLAB C 和 Fortran API 已更改,现可支持
包含 2^32-1 个以上元素的 MATLAB 变量。不久以后,
您需要更新代码以利用
新的 API。您可以在以下网址找到相关详细信息:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html。

要选择不同的 C 编译器,请从以下选项中选择一种命令:
lcc-win32 mex -setup:’D:\Program Files\MATLAB\R2015b\bin\win32\mexopts\lcc-win32.xml’ C
Microsoft Visual C++ 2008 Professional (C) mex -setup:C:\Users\WangXu\AppData\Roaming\MathWorks\MATLAB\R2015b\mex_C_win32.xml C

要选择不同的语言,请从以下选项中选择一种命令:
mex -setup C++
mex -setup FORTRAN

【单击】“mex -setup C++ ”

【出现】:

MEX 配置为使用 ‘Microsoft Visual C++ 2008 Professional’ 以进行 C++ 语言编译。
警告: MATLAB C 和 Fortran API 已更改,现可支持
包含 2^32-1 个以上元素的 MATLAB 变量。不久以后,
您需要更新代码以利用
新的 API。您可以在以下网址找到相关详细信息:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html。

(4)

在命令行窗口输入

耐心等待,【出现】如下的MATLAB Coder欢迎界面。


然后点击上图中蓝色圆圈处,选择我们建立的test.m文件,得到如下图

再点击上图蓝色圈的“Next”,得到下图

然后点击上图中蓝色圆圈处,选择我们建立的main.m文件,得到如下图

然后点击上图中蓝色圆圈处,得到如下图

上图的红色框区就是用来设置输入参数mark、num以及array的属性的。例如array可以设置为固定长度、有限可变长度以及无限可变长度。如下图

(固定长度:5)

(有限可变长度,最长为5)

(无限可变长度)

具体的设置方法也非常简单,如下图的红色框区

在这里,我们设置为有限可变长度,最长为5。

接下来点击右下角的“Next”,得到如下图

然后点击上图中蓝色圆圈处,开始自动检查问题。【你的MATLAB运行没问题,不代表往C++代码转换的时候没问题,毕竟Coder工具还不成熟】

最终,确实检查出了问题。。。如下图

如上图的错误的描述——“Variable ‘A’ is not fully defined on some execution paths.”说明问题出在test.m文件的变量A上。

经过分析,大致是这么个原因:

变量A或者B没有预分配大小,这在C++中是不允许的,因此,稍微修改下test.m文件的代码,最终代码如下:

  1. function [A,B] = test( mark,num,array )%输入参数中,mark、num是标量,array是向量
  2. a=ones(1,5);
  3. b=zeros(1,5);
  4. % 为A B预分配个内存大小
  5. A=ones(1,5);
  6. B=zeros(1,5);
  7.     if mark==0
  8.         for i=1:num
  9.              if i==1
  10.                  A=a;
  11.                  B=b;
  12.              else
  13.                  A=[A,a];
  14.                  B=[B,b];
  15.              end
  16.         end
  17.     else
  18.         for i=1:num
  19.              if i==1
  20.                  A=a;
  21.                  B=b;
  22.              else
  23.                  A=[A,array];
  24.                  B=[B,array];
  25.              end
  26.         end
  27.     end
  28. end

修改完成后,从第(3)步开始,将上述步骤再来一遍,就发现没有问题了,如下图:

点击“Next”,出现下图,标红色框的地方进行如下图的设置

注意,还没设置完。点击上图的“More Settings”,得到如下,标红色框的地方进行如下图的设置,然后点击“Close”。

最后点击“Generate”,耐心等待,最后出现如下图,说明转换成功。

现在进入MATLAB的如下目录:

X:\XXX\codegen\lib\test

例如我的是:
E:\kuaipan\Work\CSDN\新建文件夹\codegen\lib\test

可以看到如下文件:

 


http://blog.csdn.net/xiaoxuzaizi/article/details/50394866

Don’t invert that matrix

There is hardly ever a good reason to invert a matrix.

What do you do if you need to solve Ax = b where A is an n x n matrix? Isn’t the solution A-1 b? Yes, theoretically. But that doesn’t mean you need to actually find A-1. Solving the equation Ax = b is faster than finding A-1. Books might write the problem as x = A-1 b, but that doesn’t mean they expect you to calculate it that way.

What if you have to solve Ax = b for a lot of different b‘s? Surely then it’s worthwhile to find A-1. No. The first time you solve Ax = b, you factor A and save that factorization. Then when you solve for the next b, the answer comes much faster. (Factorization takes O(n3) operations. But once the matrix is factored, solving Ax = b takes only O(n2) operations. Suppose n = 1,000. This says that once you’ve solved Ax = b for one b, the equation can be solved again for a new b 1,000 times faster than the first one. Buy one get one free.)

What if, against advice, you’ve computed A-1. Now you might as well use it, right? No, you’re still better off solving Ax = b than multiplying by A-1, even if the computation of A-1 came for free. Solving the system is more numerically accurate than the performing the matrix multiplication.

It is common in applications to solve Ax = b even though there’s not enough memory to store A-1. For example, suppose n = 1,000,000 for the matrix A but A has a special sparse structure — say it’s banded — so that all but a few million entries of A are zero.  Then A can easily be stored in memory and Ax = b can be solved very quickly. But in general A-1 would be dense. That is, nearly all of the 1,000,000,000,000 entries of the matrix would be non-zero.  Storing A requires megabytes of memory, but storing A-1would require terabytes of memory.

 


https://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/