Matlab: Quadratic programming


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:


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).

Rules for Capitalization in Titles of Articles


MATLAB R2015b的Coder工具使用实例(一),转自


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




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




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



在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++代码。




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




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



MEX 配置为使用 ‘Microsoft Visual C++ 2008 Professional (C)’ 以进行 C 语言编译。
警告: MATLAB C 和 Fortran API 已更改,现可支持
包含 2^32-1 个以上元素的 MATLAB 变量。不久以后,
新的 API。您可以在以下网址找到相关详细信息:。

要选择不同的 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。您可以在以下网址找到相关详细信息:。



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














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



  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



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






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.