ATLAB 程序
这一步是真正的拦路虎。首先,程序的每一部分将会被单独地检测,如果有可能的话,整个程序还要被检测一遍。在我们检测程序时,我们必须证明所有合法输入数据值都能够正常运行。用标准的输入值检测程序,看它是否产生了值。如果在一个程序中执行的算法包含了不同的分支,你必须检测每一个分支,以保证产生正确的答案。大程序在交付大众使用之前,必须经过一系列地检测(图2)。检测的第一步有时被称为单元检测(unittesting)。在单元检测过程中,程序的子程序将会被独立地检测以证明它的正确性。当单元检测结束之后,这个程序将进行一系列的组合,把独立的子程序联合产生出最后的程序。
程序第一步的联合通常只包括很少的子程序。通过组合这些子程序,经常用检查子程序或函数之间的联系。在一系列地组合过程中,越来越多的子程序被加了进来,直到整个程序的完成。在每一次组合的过程中,每一个错误都会被发现并在进行下一次组合之前纠正过来。
图 2 ,
Introduction to MATLAB
MATLAB (short for Matrix Laboratory) is a special-purpose computer program optimized to perform engineering and scientific calculations. It started life has a program designed to perform matrix mathematics, but over the years it has grown into a flexible computing system capable of solving essentially any technical problem.
The MATLAB program implements the MATLAB programming language and provides an extensive library of predefined functions that make technical programming tasks easier and more efficient. This book introduces the MATLAB language and shows how to use it to solve typical technical problems.
MATLAB is a huge program, with an incredibly rich variety of functions. Even the basic version of MATLAB without any toolkits is much richer than other technical programming languages. There are more than 1000 functions in the basic MATLAB product alone,and the toolkits extend this capability with many more functions in various specialties. This book makes no attempt to introduce the user to all of MALTLAB′s own tools to locate the correct function for a specific purpose from the enormous choice available.
Advantages of MATLAB
MATLAB has many advantages compared with conventional computer languages for technical problem solving. Among them are the following:
1. Ease of Use
MATLAB is an interpreted language, like many versions of Basic. Like Basic, it is very easy to use. The program can be used as a scratch pad to evaluate expressions typed at the command line, or it can be used to execute large prewritten programs. Programs may be easily written and modified with the built-in integrated development environment, and debugged with the MATLAB debugger. Because the language is so easy to use, it is ideal for educational use, and for the rapid prototyping of new programs.
Many program development tools are provided to make the program easy to use. a workspace browser, and extensive demos.
2. Platform independence
MATLAB is supported on many different computer systems, providing a large measure of platform independence. At the time of this writing, the language is supported on windows 9x/NT/2000 and many different versions of UNIX. Programs written on any platform will run on all of the other platforms, and data files written on any platform may be read transparently on any other platforms, AS a result,
Programs written in MATLAB can migrate to new platforms when the needs of the user change.
3. Predefined Functions
MATLAB comes complete with an extensive library of predefined functions that provide tested and prepackaged solutions to many basic technical tasks. For example, suppose that you are writing a program that must calculate the statistics associated with an input data set. In most languages, you would need to write your own subroutines or functions to implement calculations such as the arithmetic mean, standard deviation, median, and so forth. These and hundreds of other functions are built right into the MATLAB language, making your job much easier.
In addition to the large library of functions built into the basic MATLAB language, many special-purpose toolboxes are available to help solve complex problems in specific areas. For example, a user can buy standard toolboxes to solve problems in Signal Processing, Control Systems, Communications, Image Processing, and Neural Networks, among many others. There is also an extensive collection of free user-contributed MATLAB programs that are shared through the MATLAB Web site.
4. Device-Independent Plotting
Unlike most other computer languages, MATLAB has many integral plotting and imaging commands. The plots and images can be displayed on any graphical output device supported by the computer on which MATLAB is running. This capability makes MATLAB an outstanding tool for visualizing technical data.
5. Graphical User Interface
MATLAB includes tools that allow a programmer to interactively construct a graphical user interface (GUI) for his or her program. With this capability, the programmer can design sophisticated data analysis programs that can be operated by relatively inexperienced users.
6. MATLAB Compiler
MATLAB′s flexibility and platform independence is achieved by compiling MATLAB programs into a device-independence p-code, and then interpreting the p-code instructions at run time. This approach is similar to that used by Microsoft is Visual Basic language. Unfortunately, the resulting programs can sometimes execute slowly because the MATLAB code is interpreted rather than compiled. We will point out features that tend to slow program execution when we encounter them.
A separate MATLAB compiler is available. This compiler can compile a MATLAB program into a true executable that runs faster than the interpreted code. It is a great way to convert a prototype MATLAB program into an executable suitable for sale and distribution to users.
Disadvantages of MATLAB
MATLAB has two principal disadvantages. The first is that it is an interpreted language, and therefore can execute more slowly than compiled languages. This problem can be mitigated by properly structuring the MATLAB program and by the use of the MATLAB compiler to compile the final MATLAB program before distribution and general use.
The second disadvantage is cost: A full copy of MATLAB is 5 to 10 times more expensive than a conventional C or Fortran compiler. This relatively high cost is more than offset by the reduced time required for an engineer or scientist to create a working program, so MATLAB is cost-effective for businesses. However, it is too expensive for most individuals to consider purchasing. Fortunately, there is also an inexpensive Student Edition of MATLAB, which is a great tool for students wanting to learn the language. The Student Edition of MATLAB is essentially identical to the full edition.
With the introduction of branches and loops, our programs are going to become more complex, and it will get easier to make mistakes. To help avoid programming errors, we will introduce a formal program design procedure based on the technique known as top-down design. We will also introduce a common algorithm development tool known as pseudo code.
Introduction To Top-Down Design Techniques
Suppose that you are an engineer working in industry, and that you need to write a program to solve some problem. How do you begin?
When given anew problem, there is a natural tendency to sit down at a keyboard and start programming without “wasting” a lot of time thinking about the problem first. It is often possible to get away with this “on the fly” approach to programming for very small problems, such as many of the examples in this book. In the real world, however, problems are larger, and a programmer attempting this approach will become hopelessly bogged down. For larger problems, it pays to completely think out the problem and the approach you are going to take to it before writing a single line of code.
We introduce a formal program design process in this section, and then apply that process to every major application developed in the remainder of the book. For some of the simple examples that we will be doing, the design process will seem like overkill; however, as the problems that we solve get larger and larger, the process becomes more and more essential to successful programming.
When I was an undergraduate, one of my professors was fond of saying, “programming is easy. It is knowing what to program that is hard.” his point was forcefully driven home to me after I left university and began working in industry on larger scale software projects. I found that the most difficult port of my job was to understand the problem I was trying to solve. Once I really understood the problem, it became easy to break the problem apart into smaller, more easily manageable pieces with well-defined functions, and then to tackle those pieces one at a time.
Top-down design is the process of starting with a large task and breaking it down into smaller, more easily understandable pieces, which perform a portion of the desired task. Each subtask may in turn be subdivided into smaller subtasks if necessary. Once the program is divided into small pieces, each piece can be coded and tested independently. We do not attempt to combine the subtasks into a complete task until each of the subtasks has been verified to work properly by itself.
The concept of top-down design is the basis of our formal program design process. We will now introduce the details of the process, which is illustrated in figure 1 the steps involved are:
1. Clearly state the problem that you are trying to solve.
Programs are usually written to fill some perceived need, but that need may not be articulated clearly by the person requesting the program. For example, a user may ask for a program to solve a system of simultaneous linear equations. This request is not clear enough to allow a programmer to design a program to meet the need; he or she must first know much more about the problem to be solved. Is the system of equations to be solved real or complex? What is the maximum number of equations and unknown that the program must handle? Are there any symmetry in the equations that might be exploited to make the task easier? The program designer will have to talk with the user requesting the program, and the two of them will have come up with a clear statement of exactly what they are trying to accomplish. A clear statement of the problem will prevent misunderstandings, and it will also help the program designer to properly organize his or her thoughts. In the example we were describing, a proper statement of the problem might have been:
Figure 1
Design and write a program to solve a system of simultaneous linear equations having real coefficients and with up to 20 equations in 20 unknowns.
2. Define the inputs required by the program and the outputs to be produced by the program.
The inputs to the program and the outputs produced by the program must be specified so that the new program will properly fit into the overall processing scheme. In this example, the coefficients of the equations to be solved are probably in some pre-existing order, and our new program needs to be able to read them in that order. And our new program needs to be able to read them in that order. Similarly, it needs to produce the answers required by the programs that may follow it in the overall processing scheme, and to write out those answers in the format needed by the programs following it.
3. Design the algorithm that you intend to implement in the program.
An algorithm is a step-by-step procedure for finding the solution to a problem. It is at this stage in the process that top-down design techniques come into play. The designer looks for logical divisions within the problem, and divides it up into subtasks along those lines. This process is called decomposition. If the subtasks are large, the designer can break them up into even smaller sub-tasks. This process continues until the problem has been divided into many small pieces, each of which does a simple, clearly understandable job.
After the problem has been decomposed into small pieces, each piece is further refined through a process called stepwise refinement. In stepwise refinement, a designer starts with a general description of what the piece of code should do, and then defines the functions of the piece in greater and greater detail until they are specific enough to be turned into MATLAB statements. Stepwise refinement is usually done with pseudo code, which will be described in the next section.
It is often helpful to solve a simple example of the problem by hand during the algorithm development process. If the designer understands the steps that he or she went through in solving the problem by hand, then he or she will be better able to apply decomposition and stepwise refinement to the problem.
4. Turn the algorithm into MATLAB statements.
If the decomposition and refinement process was carried out properly, this step will be very simple. All the programmer will have to do is to replace pseudo code with the corresponding MATLAB statements on a one-for-one basis.
5. Test the resulting MATLAB program
This step is the real killer. The components of the program must first be tested individually, if possible, and then the program as a whole must be tested. When testing a program, we must verify that it works correctly for all legal input data sets. It is very common for a program to be written, tested with some standard data set, and released for use, only to find that it produces the wrong answers (or crashes) with a different input data set. If the algorithm implemented in a program includes different branches, we must test all of the possible branches to confirm that the program operates correctly under every possible circumstance.
Large programs typically go through a series of tests before they are released for general use (see Figure 2). The first stage of testing is sometimes called unit testing. During unit testing, the individual subtasks of the program are tested separately to confirm that they work correctly. After the unit testing is completed, the program goes through a series of builds, during which the individual subtasks are combined to produce the final program. The first build of the program typically includes only a few of the subtasks. It is used to check the interactions among those subtasks and the functions performed by the combinations of the subtasks. In successive builds, more and more subtasks are added, until the entire program is complete. Testing is performed on each build, and any errors(bugs) detected are corrected before moving on to the next build.
Figure 2
MATLAB 介绍
MATLAB (矩阵实验室的简称)是一种专业的计算机程序,用于工程科学的矩阵数学运算。但在以后的几年内,它逐渐发展为一种极其灵活的计算体系,用于解决各种重要的技术问题。
MATLAB 程序执行MATLAB 语言,并提供了一个极其广泛的预定义函数库,这样就使得技术工作变得简单高效。本书将介绍MATLAB 语言,并向大家展示如何运用它去解决经典的技术问题。
MATLAB 是一个庞大的程序,拥有难以置信的各种丰富的函数;即使基本版本的MATLAB 语言拥有的函数也比其他的工程编程语言要丰富的多。基本的MATLAB 语言已经拥有了超过1000 多个函数,而它的工具包带有更多的函数,由此扩展了它在许多专业领域的能力。本书无意将MATLAB 的所有函数介绍给大家,而是让大家掌握编写调试和优化程序的基本功,还有一些重要函数的子集。所以从大量可利用的函数中筛选出你所需要的函数就显得尤为重要。
MATLAB 的优点
MATLAB 语言相对于传统的科技编程语言有诸多的优点。主要包括:
1.易用性
MATLAB 是种解释型语言,就像各种版本的BASIC。和BASIC 一样,它简单易用程序可用作便笺簿求打在命令行处表达式的值,也可执行预先写好的大型程序。在MATLAB 集成开发环境下,程序可以方便的编写,修改和调试。这是因为这种语言极易使用,对于教育应用和快速建立新程序的原型,它是一个理想的工具。许多的编程工具使得 MATLAB 十分简单易用。这些工具包括:一个集成的编译/调试器,在线文件手册,工作台和扩展范例。
2.平台独立性
MATLAB 支持许多的操作系统,提供了大量的平台独立的措施。在本书编写的时侯, windows98/2000/NT 和许多版本的UNIX 系统都支持它。在一个平台上编写的程序,在其它平台上一样可以正常运行,在一个平台上编写的数据文件在其它平台上一样可以编译。因此用户可以根据需要把MATLAB 编写的程序移植到新平台。
3.预定义函数
MATLAB 带有一个极大的预定义函数库,它提供了许多已测试和打包过的基本工程问题的函数。例如,假设你正在编写一个程序,这个程序要求你必须计算与输入有关的统计量。在许多的语言中,你需要写出你所编数组的下标和执行计算所需要的函数,这些函数包括其数学意义,中值,标准误差等。像这样成百上千的函数已经在MATLAB 中编写好,所以让编程变得更加简单。
除了植入MATLAB 基本语言中的大量函数,还有许多专用工具箱,以帮助用户解决在具体领域的复杂问题。例如,用户可以购买标准的工具箱以解决在信号处理,控制系统,通信,图象处理,神经网络和其他许多领域的问题。
4.机制独立的画图
与其他语言不同,MATLAB 有许多的画图和图象处理命令。当MATLAB 运行时,这些标绘图和图片将会出现在这台电脑的图像输出设备中。此功能使得MATLAB 成为一个形象化技术数据的卓越工具。
5.用户图形界面
MATLAB 允许程序员为他们的程序建立一个交互式的用户图形界面。利用MATLAB 的这种功能,程序员可以设计出相对于无经验的用户可以操作的复杂的数据分析程序。
6.MATLAB 编译器
MATLAB 的灵活性和平台独立性是通过将MATLAB 代码编译成设备独立的P 代码,然后在运行时解释P 代码来实现的。这种方法与微软的VB 相类似。不幸的是,由于MATLAB 是解释性语言,而不是编译型语言,产生的程序执行速度慢。当我们遇到执行速度慢的程序时,我们将会指出其这一特性。
MATLAB 的缺点
MATLAB 有两个基本的缺点。
第一,它是解释型语言,其执行速度要比编译型语言慢得多。这个问题可以通过合理的MATLAB 结构得到缓解,也可以在发行广泛使用前编译出MATLAB 程序。
第二,他的费用较高。一个完全版MATLAB 编译器的大小是一个C 语言或Fortan 语言编译器的5 到10倍。但MATLAB 能够节省大量的时间在科技编程方面,故MATLAB 在商业编程过程中是节省成本的。尽管如此,相对于大多数考虑购买的人还是很昂贵的。幸运的是,它有一个价格便宜的学生专用版本,对学生来说它是学习MATLAB 语言的一个重要工具。学生版的MATLAB和完全版的MATLAB 是基本一致的。随着选择和循环介绍,我们的程序也将变得复杂,对于解决问题来说,将会变得简单。为了帮助大家避免在编程过程中出现大量的错误,我们将向大家介绍正式的编程步骤,即自上而下的编程方法。我们也会向大家介绍一些普通的算法开发工具即伪代码。
自上而下的编程方法简介
假设你是在工厂工作的工程师,为了解决某些问题,你要编写一个程序。你如何开始呢?当遇到一个新问题时,我们的心里会自然而然的产生这样的想法:马上坐在计算机前,开始编程,而不用浪费大量的时间思考我们所要解决的问题是什么?用这种不切实际的想法来编一些非常小的程序可能会成功。但在现实中,问题可能会非常的大,程序员再用这种方法编程将会陷入困境。对于一个大的程序来说,在编写代码之前你要通盘的思考你所要面临的问题和解决的方法。在本节中,我们将向大家介绍正式的编程设计步骤,然后应用这个步骤来编写本书所有的大的应用程序。对于我们所遇到一些简单的例子来说,这个步骤好像有些画蛇添足。但是当我们解决的问题变得越来越大的时侯,这个步骤将会变得异常重要。当我还没有毕业的时侯,一个教授喜欢说:“编程很简单,因为我知道在编程的过程的困难”。当我们离开学校,在工厂从事于大规模软件工程编写时,我深深地理解了它所说的话。我发现在工作中我遇到的大多数困难都是对所要解决问题的理解。一旦你真正理解了问题,你就会把这个问题分解成许多小的问题,更加易于管理的小块,然后逐一解决某一个小块。自上而下的编程方法是我们正规编程设计的基础。我们现在向大家介绍这些在图1
说明的步骤细节。步骤如下:
1.清晰地陈述你所要解决的问题
编写的程序大多数情况下要满足一些感觉上的需要,但这种需要不一定能够被人清晰地表达出来。例如,用户需要一个解线性方程组的表达式。像这样的要求就不够清楚,程序员就很难编出一个使他满意的程序。他必须弄清楚要有多少问题需要解决?在这些方程式中有没有对称的形式使我们的开发变得简单?程序设计者必须和使用者讨论所需的程序,他们必须要对完成的任务有一个精确细致的描述。对问题清晰的描述可以防止误解,并且能够帮助程序员合理的组织他的思想。上面的例子对问题合适的陈述应为:设计一个用于解决联立线性方程组的程序,这些方程中未知数的系数为实数,最多有20 个未知数。
2.定义程序所需的输入量和程序所产生的输出量
指定输入量和输出量,只有这样新的程序才能适应全过程计划。在这个例子中方程式的系数可能有其预先存在的顺序,我们的新程序必须能按照顺序读取它们。相似地,也需要产生出这个程序所要求的结果,即输出量,我们还要以一定的格式打印出来。
3.设计你的程序得以实现的算法
图 1
算法是指为某个问题找到答案一步接一步的程序。在这个阶段自上而下的编程方法发挥了作用。编程设计者开始对这个问题进行逻辑划分,把它逐步分解为一个又一个子工作。这个过程叫做分解(decomposition)。如果一些子工作还是比较大,设计者还可以把他它分解成更小的块。这个过程将会继续到问题被分解成许多简单且易理解的小块为止。在问题被分解成小块之后,每一个小块要被进一步的求精,这个过程叫做逐步求精(stepwise refinement)。在这个过程中,设计者开始于对本小块代码总括性的描述,然后开始一步一步地定义所需的函数,越来越具体,直到他能够转化为MATLAB 语句。逐步求精的过程中,我们要用到的伪代码将会在下节为大家介绍。在算法开发过程中,这个方法是非常有用的。如果设计者真正理解了解决问题这个些步骤,他将会对问题进行分解和逐步求精。
4.把算法转化为代码
如果分解和逐步求精的过程已经顺利完成,那么这一步将会异常地简单。所有程序员都会将伪代码一句一句地转化为合适地MATLAB 语句。
5.检测产生的M
全套毕业设计论文现成成品资料请咨询微信号:biyezuopin QQ:2922748026
返回首页
如转载请注明来源于www.biyezuopin.vip
|